Skip to content

Commit b4c5ed5

Browse files
authored
refactor: css dependencies (#2404)
1 parent a43a849 commit b4c5ed5

File tree

18 files changed

+664
-361
lines changed

18 files changed

+664
-361
lines changed

.changeset/cold-geckos-jog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@rspack/binding": patch
3+
"@rspack/core": patch
4+
---
5+
6+
fix: css url rewrite for complex http url within ~

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/node_binding/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.

crates/rspack_core/src/dependency/code_generatable.rs

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* Some code is modified based on
33
* https://github.com/vercel/turbo/blob/a1947f64443fb98e5c3e10bca6ef9eafd278bd21/crates/turbopack-ecmascript/src/code_gen.rs
4+
* https://github.com/vercel/turbo/blob/a1947f64443fb98e5c3e10bca6ef9eafd278bd21/crates/turbopack-css/src/code_gen.rs
45
* MPL-2.0 Licensed
5-
* Author Alex Kirszenberg
6+
* Author Alex Kirszenberg, ForsakenHarmony
67
* Copyright (c)
78
* https://github.com/vercel/turbo/blob/a1947f64443fb98e5c3e10bca6ef9eafd278bd21/LICENSE#L1
89
*/
@@ -215,7 +216,7 @@ macro_rules! create_javascript_visitor {
215216
visit_mut_program: T,
216217
}
217218

218-
impl<T: Fn(&mut swc_core::ecma::ast::Program) + Send + Sync> $crate::code_gen::VisitorFactory
219+
impl<T: Fn(&mut swc_core::ecma::ast::Program) + Send + Sync> $crate::JavaScriptVisitorBuilder
219220
for Box<Visitor<T>>
220221
{
221222
fn create<'a>(&'a self) -> Box<dyn swc_core::ecma::visit::VisitMut + Send + Sync + 'a> {
@@ -232,10 +233,12 @@ macro_rules! create_javascript_visitor {
232233
}
233234

234235
(
235-
Vec::new(),
236-
box box Visitor {
237-
visit_mut_program: move |$arg: &mut swc_core::ecma::ast::Program| $b,
238-
} as Box<dyn $crate::code_gen::VisitorFactory>,
236+
$crate::CodeGeneratableAstPath::JavaScript(Vec::new()),
237+
$crate::CodeGeneratableVisitorBuilder::JavaScript(
238+
box box Visitor {
239+
visit_mut_program: move |$arg: &mut swc_core::ecma::ast::Program| $b,
240+
} as Box<dyn $crate::JavaScriptVisitorBuilder>,
241+
),
239242
)
240243
}};
241244
}
@@ -251,3 +254,87 @@ pub fn javascript_path_to(
251254
path.to_vec()
252255
}
253256
}
257+
258+
#[macro_export]
259+
macro_rules! create_css_visitor {
260+
(exact $ast_path:expr, $name:ident($arg:ident: &mut $ty:ident) $b:block) => {
261+
$crate::create_css_visitor!(__ $ast_path.to_vec(), $name($arg: &mut $ty) $b)
262+
};
263+
($ast_path:expr, $name:ident($arg:ident: &mut $ty:ident) $b:block) => {
264+
$crate::create_css_visitor!(__ $crate::css_path_to(&$ast_path, |n| {
265+
matches!(n, swc_core::css::visit::AstParentKind::$ty(_))
266+
}), $name($arg: &mut $ty) $b)
267+
};
268+
(__ $ast_path:expr, $name:ident($arg:ident: &mut $ty:ident) $b:block) => {{
269+
struct Visitor<T: Fn(&mut swc_core::css::ast::$ty) + Send + Sync> {
270+
$name: T,
271+
}
272+
273+
impl<T: Fn(&mut swc_core::css::ast::$ty) + Send + Sync> $crate::CssVisitorBuilder
274+
for Box<Visitor<T>>
275+
{
276+
fn create<'a>(&'a self) -> Box<dyn swc_core::css::visit::VisitMut + Send + Sync + 'a> {
277+
Box::new(&**self)
278+
}
279+
}
280+
281+
impl<'a, T: Fn(&mut swc_core::css::ast::$ty) + Send + Sync> swc_core::css::visit::VisitMut
282+
for &'a Visitor<T>
283+
{
284+
fn $name(&mut self, $arg: &mut swc_core::css::ast::$ty) {
285+
(self.$name)($arg);
286+
}
287+
}
288+
289+
(
290+
$crate::CodeGeneratableAstPath::from($ast_path),
291+
$crate::CodeGeneratableVisitorBuilder::Css(
292+
box box Visitor {
293+
$name: move |$arg: &mut swc_core::css::ast::$ty| $b,
294+
} as Box<dyn $crate::CssVisitorBuilder>
295+
),
296+
)
297+
}};
298+
(visit_mut_stylesheet($arg:ident: &mut Stylesheet) $b:block) => {{
299+
struct Visitor<T: Fn(&mut swc_core::css::ast::Stylesheet) + Send + Sync> {
300+
visit_mut_stylesheet: T,
301+
}
302+
303+
impl<T: Fn(&mut swc_core::css::ast::Stylesheet) + Send + Sync> $crate::CssVisitorBuilder
304+
for Box<Visitor<T>>
305+
{
306+
fn create<'a>(&'a self) -> Box<dyn swc_core::css::visit::VisitMut + Send + Sync + 'a> {
307+
Box::new(&**self)
308+
}
309+
}
310+
311+
impl<'a, T: Fn(&mut swc_core::css::ast::Stylesheet) + Send + Sync> swc_core::css::visit::VisitMut
312+
for &'a Visitor<T>
313+
{
314+
fn visit_mut_stylesheet(&mut self, $arg: &mut swc_core::css::ast::Stylesheet) {
315+
(self.visit_mut_stylesheet)($arg);
316+
}
317+
}
318+
319+
(
320+
$crate::CodeGeneratableAstPath::Css(Vec::new()),
321+
$crate::CodeGeneratableVisitorBuilder::Css(
322+
box box Visitor {
323+
visit_mut_stylesheet: move |$arg: &mut swc_core::css::ast::Stylesheet| $b,
324+
} as Box<dyn $crate::CssVisitorBuilder>
325+
),
326+
)
327+
}};
328+
}
329+
330+
pub fn css_path_to(
331+
path: &[swc_core::css::visit::AstParentKind],
332+
f: impl FnMut(&swc_core::css::visit::AstParentKind) -> bool,
333+
) -> Vec<swc_core::css::visit::AstParentKind> {
334+
if let Some(pos) = path.iter().rev().position(f) {
335+
let index = path.len() - pos - 1;
336+
path[..index].to_vec()
337+
} else {
338+
path.to_vec()
339+
}
340+
}

crates/rspack_core/src/dependency/css/mod.rs

Lines changed: 0 additions & 4 deletions
This file was deleted.

crates/rspack_core/src/dependency/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ pub use common_js_require_context_dependency::*;
1111
pub use const_dependency::ConstDependency;
1212
pub use import_context_dependency::*;
1313
mod require_context_dependency;
14-
pub use require_context_dependency::RequireContextDependency;
15-
mod css;
1614
use std::{any::Any, fmt::Debug, hash::Hash};
1715

18-
pub use css::*;
1916
use dyn_clone::{clone_trait_object, DynClone};
17+
pub use require_context_dependency::RequireContextDependency;
2018

2119
use crate::{
2220
AsAny, ContextMode, ContextOptions, DynEq, DynHash, ErrorSpan, ModuleGraph, ModuleIdentifier,
@@ -49,6 +47,8 @@ pub enum DependencyType {
4947
CssUrl,
5048
// css @import
5149
CssImport,
50+
// css modules compose
51+
CssCompose,
5252
// context element
5353
ContextElement,
5454
// import context
@@ -67,6 +67,7 @@ pub enum DependencyCategory {
6767
CommonJS,
6868
Url,
6969
CssImport,
70+
CssCompose,
7071
}
7172

7273
pub trait Dependency:

crates/rspack_plugin_css/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ indexmap = { workspace = true }
2222
itertools = { workspace = true }
2323
linked_hash_set = { workspace = true }
2424
once_cell = { workspace = true }
25+
paste = { workspace = true }
2526
preset_env_base = { workspace = true }
2627
rayon = { workspace = true }
2728
regex = { workspace = true }

0 commit comments

Comments
 (0)