Skip to content

Commit b96b353

Browse files
authored
fix: should handle name convention of css compose (#12202)
* fix: should handle name convention of css compose * fix: should handle name convention of css compose
1 parent 848f76a commit b96b353

File tree

7 files changed

+120
-59
lines changed

7 files changed

+120
-59
lines changed

crates/rspack_plugin_css/src/parser_and_generator/mod.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -391,28 +391,40 @@ impl ParserAndGenerator for CssParserAndGenerator {
391391
)));
392392
}
393393

394+
let convention = self
395+
.convention
396+
.as_ref()
397+
.expect("should have local_ident_name for module_type css/auto or css/module");
394398
let exports = self.exports.get_or_insert_default();
395399
for name in names {
396400
for local_class in local_classes.iter() {
397-
if let Some(existing) = exports.get(name.as_str())
398-
&& from.is_none()
401+
let convention_names = export_locals_convention(&name, convention);
402+
let convention_local_class = export_locals_convention(local_class, convention);
403+
404+
for (name, local_class) in convention_names
405+
.into_iter()
406+
.zip(convention_local_class.into_iter())
399407
{
400-
let existing = existing.clone();
401-
exports
402-
.get_mut(local_class.as_str())
403-
.expect("composes local class must already added to exports")
404-
.extend(existing);
405-
} else {
406-
exports
407-
.get_mut(local_class.as_str())
408-
.expect("composes local class must already added to exports")
409-
.insert(CssExport {
410-
ident: name.to_string(),
411-
from: from
412-
.filter(|f| *f != "global")
413-
.map(|f| f.trim_matches(|c| c == '\'' || c == '"').to_string()),
414-
id: dep_id,
415-
});
408+
if let Some(existing) = exports.get(name.as_str())
409+
&& from.is_none()
410+
{
411+
let existing = existing.clone();
412+
exports
413+
.get_mut(local_class.as_str())
414+
.expect("composes local class must already added to exports")
415+
.extend(existing);
416+
} else {
417+
exports
418+
.get_mut(local_class.as_str())
419+
.expect("composes local class must already added to exports")
420+
.insert(CssExport {
421+
ident: name.to_string(),
422+
from: from
423+
.filter(|f| *f != "global")
424+
.map(|f| f.trim_matches(|c| c == '\'' || c == '"').to_string()),
425+
id: dep_id,
426+
});
427+
}
416428
}
417429
}
418430
}

tests/rspack-test/configCases/clean/ignore-hook/rspack.config.js

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ const readDir = require("../enabled/readdir");
77
/** @type {import("@rspack/core").Configuration} */
88
module.exports = {
99
output: {
10-
clean: true
10+
clean: {
11+
keep: asset => {
12+
if (/[/\\]ignored[/\\]dir[/\\]/.test(asset)) return true;
13+
if (asset.includes(`ignored/too`)) return true;
14+
return false;
15+
}
16+
}
1117
},
1218
plugins: [
1319
compiler => {
1420
let once = true;
1521
compiler.hooks.thisCompilation.tap("Test", compilation => {
16-
webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap(
17-
"Test",
18-
asset => {
19-
if (/[/\\]ignored[/\\]dir[/\\]/.test(asset)) return true;
20-
if (asset.includes(`ignored/too`)) return true;
21-
}
22-
);
2322
compilation.hooks.processAssets.tap("Test", assets => {
2423
if (once) {
2524
const outputPath = compilation.getPath(compiler.outputPath, {});
@@ -49,38 +48,38 @@ module.exports = {
4948
compiler.hooks.afterEmit.tap("Test", compilation => {
5049
const outputPath = compilation.getPath(compiler.outputPath, {});
5150
expect(readDir(outputPath)).toMatchInlineSnapshot(`
52-
Object {
53-
"directories": Array [
54-
"this",
55-
"this/is",
56-
"this/is/ignored",
57-
"this/is/ignored/too",
58-
"this/is/ignored/too/dir",
59-
"this/is/ignored/too/dir/that",
60-
"this/is/ignored/too/dir/that/should",
61-
"this/is/ignored/too/dir/that/should/not",
62-
"this/is/ignored/too/dir/that/should/not/be",
63-
"this/is/ignored/too/dir/that/should/not/be/removed",
64-
"this/is/ignored/dir",
65-
"this/is/ignored/dir/that",
66-
"this/is/ignored/dir/that/should",
67-
"this/is/ignored/dir/that/should/not",
68-
"this/is/ignored/dir/that/should/not/be",
69-
"this/is/ignored/dir/that/should/not/be/removed",
70-
"this/dir",
71-
"this/dir/should",
72-
"this/dir/should/not",
73-
"this/dir/should/not/be",
74-
"this/dir/should/not/be/removed",
75-
],
76-
"files": Array [
77-
"this/is/ignored/too/dir/that/should/not/be/removed/file.ext",
78-
"this/is/ignored/dir/that/should/not/be/removed/file.ext",
79-
"this/dir/should/not/be/removed/file.ext",
80-
"bundle0.js",
81-
],
82-
}
83-
`);
51+
Object {
52+
directories: Array [
53+
this,
54+
this/is,
55+
this/is/ignored,
56+
this/is/ignored/too,
57+
this/is/ignored/too/dir,
58+
this/is/ignored/too/dir/that,
59+
this/is/ignored/too/dir/that/should,
60+
this/is/ignored/too/dir/that/should/not,
61+
this/is/ignored/too/dir/that/should/not/be,
62+
this/is/ignored/too/dir/that/should/not/be/removed,
63+
this/is/ignored/dir,
64+
this/is/ignored/dir/that,
65+
this/is/ignored/dir/that/should,
66+
this/is/ignored/dir/that/should/not,
67+
this/is/ignored/dir/that/should/not/be,
68+
this/is/ignored/dir/that/should/not/be/removed,
69+
this/dir,
70+
this/dir/should,
71+
this/dir/should/not,
72+
this/dir/should/not/be,
73+
this/dir/should/not/be/removed,
74+
],
75+
files: Array [
76+
this/is/ignored/too/dir/that/should/not/be/removed/file.ext,
77+
this/is/ignored/dir/that/should/not/be/removed/file.ext,
78+
this/dir/should/not/be/removed/file.ext,
79+
bundle0.js,
80+
],
81+
}
82+
`);
8483
});
8584
}
8685
]

tests/rspack-test/configCases/clean/ignore-hook/test.filter.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as styles from "./style.module.css";
2+
3+
export function styles() {
4+
return styles;
5+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require("path");
2+
module.exports = {
3+
output: {
4+
filename: "main.js",
5+
library: {
6+
type: "commonjs-static",
7+
},
8+
},
9+
module: {
10+
generator: {
11+
"css/auto": {
12+
exportsConvention: "camel-case",
13+
},
14+
},
15+
},
16+
experiments: {
17+
css: true,
18+
},
19+
plugins: [{
20+
apply(compiler) {
21+
compiler.hooks.done.tap("Test", stats => {
22+
const { styles } = require(path.join(compiler.options.output.path, "main.js"));
23+
expect(typeof styles["first-class"]).toBe("string");
24+
expect(styles["first-class"].split(" ").length).toBe(1);
25+
expect(typeof styles.firstClass).toBe("string");
26+
expect(styles.firstClass.split(" ").length).toBe(1);
27+
28+
expect(typeof styles["second-class"]).toBe("string");
29+
expect(styles["second-class"].split(" ").length).toBe(2);
30+
expect(typeof styles.secondClass).toBe("string");
31+
expect(styles.secondClass.split(" ").length).toBe(2);
32+
});
33+
}
34+
}]
35+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.first-class {
2+
color: white;
3+
}
4+
5+
.second-class {
6+
composes: first-class;
7+
background: blue;
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
noTests: true
3+
};

0 commit comments

Comments
 (0)