Skip to content

Commit 145c01c

Browse files
committed
test(css): enable webpack css module cases
1 parent 3e2682e commit 145c01c

91 files changed

Lines changed: 27655 additions & 1333 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ cow-utils = { version = "0.1.3", default-features = false }
3737

3838
criterion = { package = "codspeed-criterion-compat", default-features = false, version = "4.7.0", features = ["async_tokio"] }
3939

40-
css-module-lexer = { version = "0.1.0", default-features = false }
40+
css-module-lexer = { git = "https://github.com/intellild/css-module-lexer.git", rev = "0ae2137f78a0aabebf0c1ea076f936078e66892a", default-features = false }
4141
dashmap = { version = "6.2.1", default-features = false }
4242
derive_more = { version = "2.1.1", default-features = false }
4343
dunce = { version = "1.0.5", default-features = false }

crates/rspack_core/src/compilation/build_module_graph/module_executor/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod execute;
55
mod module_tracker;
66
mod overwrite;
77

8-
use rspack_collections::{Identifier, IdentifierDashMap, IdentifierDashSet};
8+
use rspack_collections::{Identifier, IdentifierDashMap, IdentifierDashSet, IdentifierSet};
99
use rspack_error::Result;
1010
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
1111
use tokio::{
@@ -45,6 +45,7 @@ pub struct ModuleExecutor {
4545
module_assets: IdentifierDashMap<HashMap<String, CompilationAsset>>,
4646
code_generated_modules: IdentifierDashSet,
4747
pub executed_runtime_modules: IdentifierDashMap<ExecutedRuntimeModule>,
48+
force_build_modules: IdentifierSet,
4849
}
4950

5051
impl Default for ModuleExecutor {
@@ -58,16 +59,46 @@ impl Default for ModuleExecutor {
5859
module_assets: Default::default(),
5960
code_generated_modules: Default::default(),
6061
executed_runtime_modules: Default::default(),
62+
force_build_modules: Default::default(),
6163
}
6264
}
6365
}
6466

6567
impl ModuleExecutor {
68+
pub fn is_active(&self) -> bool {
69+
self.event_sender.is_some()
70+
}
71+
72+
pub fn force_rebuild_imports_from_origins(&mut self, origins: &IdentifierSet) {
73+
if origins.is_empty() {
74+
return;
75+
}
76+
let Some(make_artifact) = self.make_artifact.try_read() else {
77+
return;
78+
};
79+
let module_graph = make_artifact.get_module_graph();
80+
self.force_build_modules.extend(
81+
self
82+
.entries
83+
.iter()
84+
.filter(|(meta, _)| origins.contains(&meta.origin_module_identifier))
85+
.filter_map(|(_, dep_id)| {
86+
module_graph
87+
.module_identifier_by_dependency_id(dep_id)
88+
.copied()
89+
}),
90+
);
91+
}
92+
6693
pub async fn before_build_module_graph(&mut self, compilation: &Compilation) -> Result<()> {
6794
let mut make_artifact = self.make_artifact.steal();
6895
let mut exports_info_artifact = self.exports_info_artifact.steal();
6996
let mut params = Vec::with_capacity(5);
7097
params.push(UpdateParam::CheckNeedBuild);
98+
let force_build_modules = std::mem::take(&mut self.force_build_modules);
99+
if !force_build_modules.is_empty() {
100+
params.push(UpdateParam::ForceBuildModules(force_build_modules));
101+
}
71102
if !compilation.modified_files.is_empty() {
72103
params.push(UpdateParam::ModifiedFiles(
73104
compilation.modified_files.clone(),

crates/rspack_core/src/compilation/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,17 @@ impl Compilation {
10581058
exports_info_artifact: &mut ExportsInfoArtifact,
10591059
f: impl Fn(Vec<&BoxModule>) -> T,
10601060
) -> Result<T> {
1061+
let mut should_stop_module_executor = false;
1062+
if let Some(module_executor) = &mut self.module_executor
1063+
&& !module_executor.is_active()
1064+
{
1065+
let mut module_executor = std::mem::take(module_executor);
1066+
module_executor.force_rebuild_imports_from_origins(&module_identifiers);
1067+
module_executor.before_build_module_graph(self).await?;
1068+
self.module_executor = Some(module_executor);
1069+
should_stop_module_executor = true;
1070+
}
1071+
10611072
let artifact = self.build_module_graph_artifact.steal();
10621073

10631074
// https://github.com/webpack/webpack/blob/19ca74127f7668aaf60d59f4af8fcaee7924541a/lib/Compilation.js#L2462C21-L2462C25
@@ -1073,6 +1084,12 @@ impl Compilation {
10731084
*exports_info_artifact = updated_exports_info_artifact;
10741085
self.build_module_graph_artifact = artifact.into();
10751086

1087+
if should_stop_module_executor && let Some(module_executor) = &mut self.module_executor {
1088+
let mut module_executor = std::mem::take(module_executor);
1089+
module_executor.after_build_module_graph(self).await?;
1090+
self.module_executor = Some(module_executor);
1091+
}
1092+
10761093
let module_graph = self.get_module_graph();
10771094
Ok(f(module_identifiers
10781095
.into_iter()

crates/rspack_core/src/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub struct MinifyError(pub Error);
145145
impl From<MinifyError> for Error {
146146
fn from(value: MinifyError) -> Error {
147147
let mut error = rspack_error::error!("Chunk minification failed:");
148+
error.severity = value.0.severity;
148149
error.code = if value.0.is_warn() {
149150
Some("ChunkMinificationWarning".into())
150151
} else {

crates/rspack_loader_lightningcss/src/lib.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cow_utils::CowUtils;
88
use derive_more::Debug;
99
pub use lightningcss;
1010
use lightningcss::{
11+
error::{ParserError, SelectorError},
1112
printer::{PrinterOptions, PseudoClasses},
1213
stylesheet::{MinifyOptions, ParserFlags, ParserOptions, StyleSheet},
1314
targets::{Features, Targets},
@@ -21,7 +22,7 @@ use rspack_core::{
2122
SourceMapSourceOptions, encode_mappings,
2223
},
2324
};
24-
use rspack_error::{Result, ToStringResultToRspackResultExt};
25+
use rspack_error::{Diagnostic, Result, ToStringResultToRspackResultExt};
2526
use rspack_loader_runner::Identifier;
2627
use tokio::sync::Mutex;
2728

@@ -99,31 +100,25 @@ impl LightningCssLoader {
99100
flags: parser_flags,
100101
};
101102
let stylesheet = StyleSheet::parse(&content_str, option.clone()).to_rspack_result()?;
102-
// FIXME: Disable the warnings for now, cause it cause too much positive-negative warnings,
103-
// enable when we have a better way to handle it.
104-
105-
// if let Some(warnings) = warnings {
106-
// #[allow(clippy::unwrap_used)]
107-
// let warnings = warnings.read().unwrap();
108-
// for warning in warnings.iter() {
109-
// if matches!(
110-
// warning.kind,
111-
// lightningcss::error::ParserError::SelectorError(
112-
// lightningcss::error::SelectorError::UnsupportedPseudoClass(_)
113-
// ) | lightningcss::error::ParserError::SelectorError(
114-
// lightningcss::error::SelectorError::UnsupportedPseudoElement(_)
115-
// )
116-
// ) {
117-
// // ignore parsing errors on pseudo class from lightningcss-loader
118-
// // to allow pseudo class in CSS modules and Vue.
119-
// continue;
120-
// }
121-
// loader_context.emit_diagnostic(Diagnostic::warn(
122-
// "builtin:lightningcss-loader".to_string(),
123-
// format!("LightningCSS parse warning: {}", warning),
124-
// ));
125-
// }
126-
// }
103+
if let Some(warnings) = warnings {
104+
#[allow(clippy::unwrap_used)]
105+
let warnings = warnings.read().unwrap();
106+
for warning in warnings.iter() {
107+
if matches!(
108+
warning.kind,
109+
ParserError::SelectorError(
110+
SelectorError::UnsupportedPseudoClass(_) | SelectorError::UnsupportedPseudoElement(_)
111+
)
112+
) {
113+
// Keep CSS modules and framework pseudo selectors usable.
114+
continue;
115+
}
116+
loader_context.emit_diagnostic(Diagnostic::warn(
117+
"builtin:lightningcss-loader".to_string(),
118+
format!("LightningCSS parse warning: {warning}"),
119+
));
120+
}
121+
}
127122

128123
let mut stylesheet = to_static(
129124
stylesheet,

crates/rspack_plugin_css/src/parser_and_generator/generator.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,13 @@ impl<'a, 'g> CssModuleGenerator<'a, 'g> {
772772
for CssExport {
773773
ident,
774774
from,
775-
id: _,
775+
id,
776776
orig_name: _,
777777
} in elements
778778
{
779779
let part = match from {
780780
None => self.render_local_css_export(ident),
781-
Some(from_name) => self.render_standard_css_reexport(ident, from_name),
781+
Some(from_name) => self.render_standard_css_reexport(ident, from_name, id.as_ref()),
782782
};
783783
push_joined(&mut content, &part, " + \" \" + ");
784784
}
@@ -791,27 +791,48 @@ impl<'a, 'g> CssModuleGenerator<'a, 'g> {
791791
json_stringify_str(&ident)
792792
}
793793

794-
fn render_standard_css_reexport(&mut self, ident: &str, from_name: &str) -> String {
794+
fn render_standard_css_reexport(
795+
&mut self,
796+
ident: &str,
797+
from_name: &str,
798+
id: Option<&DependencyId>,
799+
) -> String {
795800
let compilation = self.generate_context.compilation;
796801
let module_graph = compilation.get_module_graph();
797-
let from = self
798-
.module
799-
.get_dependencies()
800-
.iter()
801-
.find_map(|id| {
802-
let dependency = module_graph.dependency_by_id(id);
803-
let request = dependency_request(dependency);
804-
if let Some(request) = request
805-
&& request == from_name
806-
{
807-
return module_graph.module_graph_module_by_dependency_id(id);
808-
}
809-
None
802+
let find_target_module =
803+
|dep_id: &DependencyId| module_graph.get_module_by_dependency_id(dep_id);
804+
let from = id
805+
.and_then(find_target_module)
806+
.or_else(|| {
807+
self.module.get_dependencies().iter().find_map(|id| {
808+
let dependency = module_graph.dependency_by_id(id);
809+
let request = dependency_request(dependency);
810+
if let Some(request) = request
811+
&& request == from_name
812+
{
813+
return find_target_module(id);
814+
}
815+
None
816+
})
810817
})
811-
.expect("should have css from module");
818+
.unwrap_or_else(|| {
819+
let dependency_requests = self
820+
.module
821+
.get_dependencies()
822+
.iter()
823+
.filter_map(|id| {
824+
let dependency = module_graph.dependency_by_id(id);
825+
dependency_request(dependency)
826+
})
827+
.collect::<Vec<_>>();
828+
panic!(
829+
"should have css from module: ident={ident}, from={from_name}, id={id:?}, dependency_requests={dependency_requests:?}"
830+
);
831+
});
812832

813-
let from_used_name = self.stringified_used_export_name(from.module_identifier, ident, true);
814-
self.render_require_property_access(from.module_identifier, &from_used_name)
833+
let from_identifier = from.identifier();
834+
let from_used_name = self.stringified_used_export_name(from_identifier, ident, true);
835+
self.render_require_property_access(from_identifier, &from_used_name)
815836
}
816837

817838
fn render_concat_export_content<'b>(

0 commit comments

Comments
 (0)