Skip to content

Commit 838b74b

Browse files
ryansolidclaude
andauthored
feat(compiler): export the scoped-CSS engine from the Rust core, independent of TSRX (#4289)
* feat(compiler): export the TSRX scoped-CSS engine from the Rust core Expose `solidjs_compiler::style` (behind the default `tsrx` feature): `compile_style`, `compile_style_with_class_map_selectors` and their input and output types. The engine already has no dependency on the TSRX frontend, so another frontend can describe a `<style>` block's elements as `Element` values and get the same stylesheet and hash a `.tsrx` file gets. Documents `StyleLocation`'s line/column bases, since they feed the hash. Additive: the TSRX route and the Node `transform()` are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01ViozQ3kVSofMUQ3fwpyq5o * refactor(compiler): move the scoped-CSS engine out of the TSRX frontend `src/tsrx/style/` becomes `src/style/`, exported as `solidjs_compiler::style` in every build: the engine never depended on TSRX, and a frontend that does not use TSRX should not need the `tsrx` feature to reach it. The TSRX projection and lowering now import it from `crate::style`. No behaviour change; the engine's unit tests now also run without the `tsrx` feature. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01ViozQ3kVSofMUQ3fwpyq5o --------- Co-authored-by: Claude <[email protected]>
1 parent 87934c1 commit 838b74b

13 files changed

Lines changed: 174 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/compiler": patch
3+
---
4+
5+
The Rust compiler core exports TSRX's scoped-CSS engine as `solidjs_compiler::style`, moved from `src/tsrx/style/` to `src/style/` so it is available without the `tsrx` feature: `compile_style`, `compile_style_with_class_map_selectors`, and their input and output types (`StyleInput`, `StyleLocation`, `StyleKind`, `Element`, `ElementKind`, `ElementChild`, `Attribute`, `AttributeValue`, `StyleOutput`, `ClassMapEntry`, `StyleError`). The engine already had no dependency on the TSRX frontend; exporting it lets another frontend (one that does not use TSRX at all) scope a `<style>` block over its own elements and get the same stylesheet and hash a `.tsrx` file gets. Additive and Rust-only: the TSRX route and the Node `transform()` are unchanged.

‎packages/compiler/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ let virtual_tsx =
201201

202202
`CompileOptions::default()` uses `module_name: "@solidjs/web"` and the same control-flow `built_ins` as the Babel plugin. Build with `--no-default-features` when embedding without the Node-API adapter.
203203

204+
TSRX's scoped-CSS engine is exported as `solidjs_compiler::style` for other frontends, in every build (it does not need the `tsrx` feature). It has no dependency on TSRX syntax or Oxc: describe the elements a `<style>` block governs as `Element` values, and `compile_style` returns the scoped stylesheet, its hash, the class map and the ids of the elements to annotate, the same bytes a `.tsrx` file with that stylesheet at that `StyleLocation` gets.
205+
206+
```rust
207+
use solidjs_compiler::style::{compile_style, Element, ElementKind, StyleInput, StyleKind, StyleLocation};
208+
209+
let elements = [Element {
210+
id: 1,
211+
kind: ElementKind::Native("div".into()),
212+
attributes: Vec::new(),
213+
has_spread: false,
214+
children: Vec::new(),
215+
}];
216+
let output = compile_style(StyleInput {
217+
css: "div { color: red }",
218+
location: StyleLocation { filename: "view.rx", line: 3, column: 4 },
219+
elements: &elements,
220+
kind: StyleKind::Block,
221+
minify: false,
222+
})?;
223+
// output.css, output.hash, output.class_map, output.scoped_elements
224+
```
225+
204226
The unstable Rust typecheck projection reports embedded ranges in authored UTF-8 bytes. The N-API adapter converts those ranges to UTF-16 code units for JavaScript tooling.
205227

206228
> **Stability:** the Rust API is unstable while the compiler is pre-1.0. Options, output, and error types may change in any release — pin an exact revision when embedding it.

‎packages/compiler/src/lib.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//!
88
//! # Stability
99
//!
10-
//! The Rust API (`compile`, [`CompileOptions`], [`CompileOutput`],
10+
//! The Rust API (`compile`, [`style`], [`CompileOptions`], [`CompileOutput`],
1111
//! [`CompileError`]) is **unstable**. The compiler is pre-1.0 and under
1212
//! active development; this surface carries no semver commitment and may
1313
//! change shape in any release while the compiler churns. Pin an exact
@@ -31,6 +31,7 @@ mod shared;
3131
#[cfg(feature = "node")]
3232
mod source_names;
3333
mod ssr;
34+
pub mod style;
3435
#[cfg(feature = "tsrx")]
3536
mod tsrx;
3637
mod universal;
File renamed without changes.
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
//! Self-contained TSRX scoped-CSS semantics.
1+
//! Scoped-CSS semantics: TSRX's `<style>` engine, shared by any frontend.
22
//!
3-
//! This module deliberately has no dependency on the TSRX/Oxc frontend. The
4-
//! frontend only needs to translate its template into [`Element`] values.
3+
//! This module deliberately has no dependency on the TSRX/Oxc frontend or on
4+
//! the `tsrx` feature. A frontend describes the elements a `<style>` block
5+
//! governs as [`Element`] values; [`compile_style`] returns the scoped
6+
//! stylesheet, its hash, the class map, and which elements to annotate with
7+
//! the hash: the same bytes a `.tsrx` file with that stylesheet at that
8+
//! [`StyleLocation`] gets.
59
610
use std::collections::{BTreeMap, BTreeSet};
711
use std::fmt;
812

13+
/// Where the `<style>` element starts in its module. Part of the scope hash,
14+
/// so the same stylesheet at the same position hashes the same everywhere.
915
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1016
pub struct StyleLocation<'a> {
1117
pub filename: &'a str,
18+
/// 1-based line of the `<style` opening tag.
1219
pub line: u32,
20+
/// 0-based column of the `<style` opening tag, in UTF-16 code units.
1321
pub column: u32,
1422
}
1523

File renamed without changes.
File renamed without changes.

‎packages/compiler/src/tsrx/lower.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ use super::{
1919
AuthoredSpan, CodeBlock, ControlFlow, ForLoop, IfChain, SolidTsrxModule, TemplateBlock,
2020
TemplateSite, Try as SemanticTry,
2121
},
22-
style::ClassMapEntry,
2322
style_projection::{
2423
RefSetup, StyleAction, StyleProjection, decode_json_string, is_callback_ref,
2524
is_direct_ref_target,
2625
},
2726
tape::Node,
2827
};
28+
use crate::style::ClassMapEntry;
2929
use crate::{error::CompileError, shared::ast_builder::AstBuilder};
3030

3131
pub(crate) struct DirectLowered<'a> {

0 commit comments

Comments
 (0)