Skip to content

Commit 290957a

Browse files
authored
fix: should not tree shaking used ident accidentally in composes (#9161)
fix: should not remove ident in composes
1 parent 9cbeb80 commit 290957a

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

crates/rspack_plugin_css/src/parser_and_generator/mod.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rspack_error::{
2424
miette::Diagnostic, IntoTWithDiagnosticArray, Result, RspackSeverity, TWithDiagnosticArray,
2525
};
2626
use rspack_util::ext::DynHash;
27-
use rustc_hash::FxHashSet;
27+
use rustc_hash::{FxHashMap, FxHashSet};
2828

2929
use crate::{
3030
dependency::CssSelfReferenceLocalIdentDependency,
@@ -87,6 +87,7 @@ pub struct CssParserAndGenerator {
8787
pub es_module: bool,
8888
#[cacheable(with=AsOption<AsMap<AsCacheable, AsVec>>)]
8989
pub exports: Option<CssExports>,
90+
pub local_names: Option<FxHashMap<String, String>>,
9091
pub hot: bool,
9192
}
9293

@@ -243,6 +244,10 @@ impl ParserAndGenerator for CssParserAndGenerator {
243244
},
244245
);
245246
}
247+
248+
let local_names = self.local_names.get_or_insert_default();
249+
local_names.insert(name.into_owned(), local_ident.clone());
250+
246251
dependencies.push(Box::new(CssLocalIdentDependency::new(
247252
local_ident,
248253
convention_names,
@@ -315,6 +320,10 @@ impl ParserAndGenerator for CssParserAndGenerator {
315320
},
316321
);
317322
}
323+
324+
let local_names = self.local_names.get_or_insert_default();
325+
local_names.insert(name.into_owned(), local_ident.clone());
326+
318327
dependencies.push(Box::new(CssLocalIdentDependency::new(
319328
local_ident.clone(),
320329
convention_names,
@@ -355,6 +364,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
355364
vec![],
356365
)));
357366
}
367+
358368
let exports = self.exports.get_or_insert_default();
359369
for name in names {
360370
for local_class in local_classes.iter() {
@@ -494,8 +504,16 @@ impl ParserAndGenerator for CssParserAndGenerator {
494504
let mut concate_source = ConcatSource::default();
495505
if let Some(ref exports) = self.exports {
496506
let mg = generate_context.compilation.get_module_graph();
497-
let unused_exports =
498-
get_unused_local_ident(exports, module.identifier(), generate_context.runtime, &mg);
507+
let unused_exports = get_unused_local_ident(
508+
exports,
509+
self
510+
.local_names
511+
.as_ref()
512+
.expect("local names must be set when self.exports is set"),
513+
module.identifier(),
514+
generate_context.runtime,
515+
&mg,
516+
);
499517
generate_context.data.insert(unused_exports);
500518
let exports =
501519
get_used_exports(exports, module.identifier(), generate_context.runtime, &mg);
@@ -522,8 +540,16 @@ impl ParserAndGenerator for CssParserAndGenerator {
522540
("", "", "")
523541
};
524542
if let Some(exports) = &self.exports {
525-
let unused_exports =
526-
get_unused_local_ident(exports, module.identifier(), generate_context.runtime, &mg);
543+
let unused_exports = get_unused_local_ident(
544+
exports,
545+
self
546+
.local_names
547+
.as_ref()
548+
.expect("local names must be set when self.exports is set"),
549+
module.identifier(),
550+
generate_context.runtime,
551+
&mg,
552+
);
527553
generate_context.data.insert(unused_exports);
528554

529555
let exports =
@@ -625,6 +651,7 @@ pub struct CodeGenerationDataUnusedLocalIdent {
625651

626652
fn get_unused_local_ident(
627653
exports: &CssExports,
654+
local_names: &FxHashMap<String, String>,
628655
identifier: ModuleIdentifier,
629656
runtime: Option<&RuntimeSpec>,
630657
mg: &ModuleGraph,
@@ -641,11 +668,7 @@ fn get_unused_local_ident(
641668
false
642669
}
643670
})
644-
.flat_map(|(_, exports)| {
645-
exports
646-
.iter()
647-
.map(|export| unescape(&export.ident).into_owned())
648-
})
671+
.filter_map(|(export_name, _)| local_names.get(export_name).cloned())
649672
.collect(),
650673
}
651674
}

crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ impl Plugin for CssPlugin {
425425
.expect("should have CssGeneratorOptions");
426426
Box::new(CssParserAndGenerator {
427427
exports: None,
428+
local_names: None,
428429
convention: None,
429430
local_ident_name: None,
430431
exports_only: g.exports_only.expect("should have exports_only"),
@@ -445,6 +446,7 @@ impl Plugin for CssPlugin {
445446
.expect("should have CssModuleGeneratorOptions");
446447
Box::new(CssParserAndGenerator {
447448
exports: None,
449+
local_names: None,
448450
convention: Some(
449451
g.exports_convention
450452
.expect("should have exports_convention"),
@@ -472,6 +474,7 @@ impl Plugin for CssPlugin {
472474
.expect("should have CssAutoGeneratorOptions");
473475
Box::new(CssParserAndGenerator {
474476
exports: None,
477+
local_names: None,
475478
convention: Some(
476479
g.exports_convention
477480
.expect("should have exports_convention"),

packages/rspack-test-tools/tests/configCases/css/minify-lightning-css-remove-unused-local-idents/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ it("should remove unused local idents", async () => {
44
const fs = __non_webpack_require__("fs");
55
const path = __non_webpack_require__("path");
66
expect(styles.a).toBe("./style.module-a");
7-
expect(styles['local/used']).toBe("./style.module-local/used");
7+
expect(styles["local/used"]).toBe("./style.module-local/used");
8+
expect(styles["composed-local"]).toBe("./style.module-composed-local");
89

910
if (!EXPORTS_ONLY) {
1011
const css = await fs.promises.readFile(path.resolve(__dirname, "./bundle0.css"), "utf-8");
1112
expect(css).not.toContain(".module-b")
1213
expect(css).toContain("local\\/used")
1314
expect(css).not.toContain("local\\/unused")
15+
expect(css).toContain("composed-local")
1416
}
1517
})

packages/rspack-test-tools/tests/configCases/css/minify-lightning-css-remove-unused-local-idents/style.module.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@
1313
.local\/unused {
1414
color: blue
1515
}
16+
17+
.compose {
18+
composes: composed-local;
19+
}
20+
21+
.composed-local {
22+
background-color: green;
23+
}

0 commit comments

Comments
 (0)