Skip to content

Commit f3f12b8

Browse files
authored
feat(pack): rewrite style-loader in Rust (#2658)
1 parent f34fe9c commit f3f12b8

40 files changed

Lines changed: 12067 additions & 10982 deletions

AGENTS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ The `next.js/` directory is a **git submodule** (`utooland/next.js`) providing T
3434
| `@utoo/pack-cli` | CLI for bundler (`up`/`utoopack` commands) |
3535
| `@utoo/pack-shared` | Shared types and utilities |
3636
| `@utoo/web` | Browser-compatible toolchain via WASM |
37-
| `@utoo/style-loader` | Style loading support |
3837

3938
### Key Data Flow
4039

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ sha1 = "0.10"
8989
sha2 = "0.10"
9090
url = "2.2.2"
9191
urlencoding = "2.1.2"
92+
lightningcss = "1.0.0-alpha.70"
9293
# Dependencies from nextjs, should keep the same rev!
9394
## Dependencies for common cache and task. See https://github.com/utooland/next.js/blob/canary/turbopack/crates/turbopack/architecture.md
9495
turbo-bincode = { path = "./next.js/turbopack/crates/turbo-bincode" }

crates/pack-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dunce = { workspace = true }
1515
either = { workspace = true, features = ["serde"] }
1616
futures = { workspace = true }
1717
indoc = { workspace = true }
18+
lightningcss = { workspace = true }
1819
mime_guess = "2.0.4"
1920
pathdiff = { workspace = true }
2021
qstring = "0.7.2"

packages/style-loader/src/runtime/injectStylesIntoLinkTag.js renamed to crates/pack-core/js/src/inline_css/injectStylesIntoLinkTag.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Injects styles into the DOM using <link> tags.
3+
* This is a Rust-native reimplementation of webpack's style-loader.
4+
* @see https://webpack.js.org/loaders/style-loader/
5+
*/
6+
17
const getTarget = (() => {
28
const memo = {};
39

packages/style-loader/src/runtime/injectStylesIntoStyleTag.js renamed to crates/pack-core/js/src/inline_css/injectStylesIntoStyleTag.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Injects styles into the DOM using <style> tags.
3+
* This is a Rust-native reimplementation of webpack's style-loader.
4+
* @see https://webpack.js.org/loaders/style-loader/
5+
*/
6+
17
const isOldIE = (function isOldIE() {
28
let memo;
39

crates/pack-core/src/client/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ pub async fn get_client_module_options_context(
223223
.await?;
224224
let target_browsers = env.runtime_versions();
225225

226-
let mut client_rules = get_client_transforms_rules(config).await?;
227-
let mut foreign_client_rules = get_client_transforms_rules(config).await?;
226+
let mut client_rules = get_client_transforms_rules(config, mode).await?;
227+
let mut foreign_client_rules = get_client_transforms_rules(config, mode).await?;
228228

229229
// Ignore .d.ts files - they are TypeScript declaration files and should not be bundled
230230
let ignore_dts_rule = ModuleRule::new(

crates/pack-core/src/client/transforms.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ use turbopack::module_options::ModuleRule;
44

55
use crate::{
66
config::Config,
7+
mode::Mode,
78
shared::transforms::{
8-
get_image_rule, get_wasm_rule, modularize_imports::get_modularize_imports_rule,
9+
get_image_rule, get_inline_css_rule, get_wasm_rule, inline_css::module::InjectType,
10+
modularize_imports::get_modularize_imports_rule,
911
},
1012
};
1113

12-
pub async fn get_client_transforms_rules(config: Vc<Config>) -> Result<Vec<ModuleRule>> {
14+
pub async fn get_client_transforms_rules(
15+
config: Vc<Config>,
16+
mode: Vc<Mode>,
17+
) -> Result<Vec<ModuleRule>> {
1318
let mut rules = vec![];
1419

1520
let optimization_config = config.optimization().await?;
@@ -33,5 +38,19 @@ pub async fn get_client_transforms_rules(config: Vc<Config>) -> Result<Vec<Modul
3338
rules.push(get_wasm_rule().await?);
3439
}
3540

41+
if let Some(inline_css_options) = &*config.inline_css().await?
42+
&& let Some(obj) = inline_css_options.as_object()
43+
{
44+
let insert = obj.get("insert").and_then(|v| v.as_str()).unwrap_or("head");
45+
let inject_type = obj
46+
.get("injectType")
47+
.and_then(|v| v.as_str())
48+
.map(InjectType::from_str)
49+
.unwrap_or(InjectType::Style);
50+
51+
let minify = *config.minify(mode).await?;
52+
rules.push(get_inline_css_rule(insert.into(), inject_type, minify).await?);
53+
}
54+
3655
Ok(rules)
3756
}

crates/pack-core/src/import_map.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ use turbopack_node::execution_context::ExecutionContext;
1313

1414
use crate::{config::Config, embed_js, util::convert_to_project_relative};
1515

16-
pub const UTOO_STYLE_LOADER: &str = "@utoo/style-loader";
17-
1816
pub fn mdx_import_source_file() -> RcStr {
1917
unreachable!()
2018
}
@@ -39,10 +37,6 @@ pub async fn insert_shared_aliases(
3937
pack_path: &FileSystemPath,
4038
) -> Result<()> {
4139
import_map.insert_singleton_alias("@swc/helpers", pack_path.join("node_modules/@swc/helpers")?);
42-
import_map.insert_singleton_alias(
43-
UTOO_STYLE_LOADER,
44-
pack_path.join(&format!("node_modules/{UTOO_STYLE_LOADER}"))?,
45-
);
4640
// import_map.insert_singleton_alias("styled-jsx", pack_package.clone());
4741
import_map.insert_singleton_alias("react", project_path.clone());
4842
import_map.insert_singleton_alias("react-dom", project_path.clone());
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// Inline CSS module — a Rust-native reimplementation of webpack's style-loader
2+
/// that injects CSS into the DOM at runtime via `<style>` or `<link>` tags.
3+
///
4+
/// Reference: <https://webpack.js.org/loaders/style-loader/>
5+
pub(crate) mod module;
6+
pub(crate) mod source_asset;
7+
8+
pub use module::InlineCssModuleType;

0 commit comments

Comments
 (0)