Skip to content

Commit 28859a4

Browse files
authored
feat: support [folder] template string in localIdentName option (#11356)
* feat: support [folder] template strings in localIdentName option * chore: add tests
1 parent 396e59a commit 28859a4

File tree

6 files changed

+109
-9
lines changed

6 files changed

+109
-9
lines changed

crates/rspack_plugin_css/src/utils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{
22
borrow::Cow,
33
fmt::Write,
44
hash::Hasher,
5+
path::Path,
56
sync::{Arc, LazyLock},
67
};
78

@@ -85,6 +86,11 @@ impl<'a> LocalIdentOptions<'a> {
8586
)),
8687
local,
8788
unique_name: &output.unique_name,
89+
folder: Path::new(&self.relative_resource)
90+
.parent()
91+
.and_then(|p| p.file_name())
92+
.and_then(|s| s.to_str())
93+
.unwrap_or(""),
8894
}
8995
.render_local_ident_name(self.local_name_ident)
9096
.await
@@ -95,6 +101,7 @@ struct LocalIdentNameRenderOptions<'a> {
95101
path_data: PathData<'a>,
96102
local: &'a str,
97103
unique_name: &'a str,
104+
folder: &'a str,
98105
}
99106

100107
impl LocalIdentNameRenderOptions<'_> {
@@ -108,6 +115,7 @@ impl LocalIdentNameRenderOptions<'_> {
108115
Ok(
109116
s.cow_replace("[uniqueName]", self.unique_name)
110117
.cow_replace("[local]", self.local)
118+
.cow_replace("[folder]", self.folder)
111119
.into_owned(),
112120
)
113121
}

tests/webpack-test/__snapshots__/ConfigTestCases.basictest.js.snap

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,28 @@ Object {
530530
`;
531531

532532
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 8`] = `
533+
Object {
534+
"btn--info_is-disabled_1": "-btn--info_is-disabled_1",
535+
"btn-info_is-disabled": "-btn-info_is-disabled",
536+
"foo": "bar",
537+
"foo_bar": "-foo_bar",
538+
"my-btn-info_is-disabled": "value",
539+
"simple": "-simple",
540+
}
541+
`;
542+
543+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 9`] = `
544+
Object {
545+
"btn--info_is-disabled_1": "nested2-btn--info_is-disabled_1",
546+
"btn-info_is-disabled": "nested2-btn-info_is-disabled",
547+
"foo": "bar",
548+
"foo_bar": "nested2-foo_bar",
549+
"my-btn-info_is-disabled": "value",
550+
"simple": "nested2-simple",
551+
}
552+
`;
553+
554+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 10`] = `
533555
Object {
534556
"btn--info_is-disabled_1": "./style.module.less__btn--info_is-disabled_1",
535557
"btn-info_is-disabled": "./style.module.less__btn-info_is-disabled",
@@ -540,7 +562,7 @@ Object {
540562
}
541563
`;
542564

543-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 9`] = `
565+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 11`] = `
544566
Object {
545567
"btn--info_is-disabled_1": "_style_module_css-btn--info_is-disabled_1",
546568
"btn-info_is-disabled": "_style_module_css-btn-info_is-disabled",
@@ -551,7 +573,7 @@ Object {
551573
}
552574
`;
553575

554-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 10`] = `
576+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 12`] = `
555577
Object {
556578
"btn--info_is-disabled_1": "feb1ff69382c6202",
557579
"btn-info_is-disabled": "f9df66ace1f48520",
@@ -562,7 +584,7 @@ Object {
562584
}
563585
`;
564586

565-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 11`] = `
587+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 13`] = `
566588
Object {
567589
"btn--info_is-disabled_1": "_6fc78548db75122e-btn--info_is-disabled_1",
568590
"btn-info_is-disabled": "_6fc78548db75122e-btn-info_is-disabled",
@@ -573,7 +595,7 @@ Object {
573595
}
574596
`;
575597

576-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 12`] = `
598+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 14`] = `
577599
Object {
578600
"btn--info_is-disabled_1": "./style.module__btn--info_is-disabled_1",
579601
"btn-info_is-disabled": "./style.module__btn-info_is-disabled",
@@ -584,7 +606,7 @@ Object {
584606
}
585607
`;
586608

587-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 13`] = `
609+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 15`] = `
588610
Object {
589611
"btn--info_is-disabled_1": "./style.module.css__btn--info_is-disabled_1",
590612
"btn-info_is-disabled": "./style.module.css__btn-info_is-disabled",
@@ -595,7 +617,7 @@ Object {
595617
}
596618
`;
597619

598-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 14`] = `
620+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 16`] = `
599621
Object {
600622
"btn--info_is-disabled_1": "./style.module.css?q#f__btn--info_is-disabled_1",
601623
"btn-info_is-disabled": "./style.module.css?q#f__btn-info_is-disabled",
@@ -606,7 +628,7 @@ Object {
606628
}
607629
`;
608630

609-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 15`] = `
631+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 17`] = `
610632
Object {
611633
"btn--info_is-disabled_1": "-_style_module_css_uniqueName-id-contenthash-[contenthash]",
612634
"btn-info_is-disabled": "-_style_module_css_uniqueName-id-contenthash-[contenthash]",
@@ -617,7 +639,29 @@ Object {
617639
}
618640
`;
619641

620-
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 16`] = `
642+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 18`] = `
643+
Object {
644+
"btn--info_is-disabled_1": "-btn--info_is-disabled_1",
645+
"btn-info_is-disabled": "-btn-info_is-disabled",
646+
"foo": "bar",
647+
"foo_bar": "-foo_bar",
648+
"my-btn-info_is-disabled": "value",
649+
"simple": "-simple",
650+
}
651+
`;
652+
653+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 19`] = `
654+
Object {
655+
"btn--info_is-disabled_1": "nested2-btn--info_is-disabled_1",
656+
"btn-info_is-disabled": "nested2-btn-info_is-disabled",
657+
"foo": "bar",
658+
"foo_bar": "nested2-foo_bar",
659+
"my-btn-info_is-disabled": "value",
660+
"simple": "nested2-simple",
661+
}
662+
`;
663+
664+
exports[`ConfigTestCases css local-ident-name exported tests should have correct local ident for css export locals 20`] = `
621665
Object {
622666
"btn--info_is-disabled_1": "./style.module.less__btn--info_is-disabled_1",
623667
"btn-info_is-disabled": "./style.module.less__btn-info_is-disabled",

tests/webpack-test/configCases/css/local-ident-name/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@ it("should have correct local ident for css export locals", (done) => {
77
import("./style.module.css?file-local"),
88
import("./style.module.css?q#f"),
99
import("./style.module.css?uniqueName-id-contenthash"),
10+
import("./style.module.css?folder-local"),
11+
import("./nested1/nested2/style.module.css?folder-local"),
1012
import("./style.module.less"),
11-
]).then(([idLocal, hash, hashLocal, pathNameLocal, fileLocal, queryFragment, uniqueNameIdContenthash, less]) => {
13+
]).then(([
14+
idLocal,
15+
hash,
16+
hashLocal,
17+
pathNameLocal,
18+
fileLocal,
19+
queryFragment,
20+
uniqueNameIdContenthash,
21+
folderLocalRoot,
22+
folderLocal,
23+
less
24+
]) => {
1225
expect(idLocal).toMatchSnapshot();
1326
expect(hash).toMatchSnapshot();
1427
expect(hashLocal).toMatchSnapshot();
1528
expect(pathNameLocal).toMatchSnapshot();
1629
expect(fileLocal).toMatchSnapshot();
1730
expect(queryFragment).toMatchSnapshot();
1831
expect(uniqueNameIdContenthash).toMatchSnapshot();
32+
expect(folderLocalRoot).toMatchSnapshot();
33+
expect(folderLocal).toMatchSnapshot();
1934
expect(less).toMatchSnapshot();
2035
done()
2136
}).catch(done)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.btn-info_is-disabled {
2+
color: blue;
3+
}
4+
5+
.btn--info_is-disabled_1 {
6+
color: blue;
7+
}
8+
9+
.simple {
10+
color: red;
11+
}
12+
13+
a {
14+
color: yellow;
15+
}
16+
17+
:export {
18+
foo: bar;
19+
my-btn-info_is-disabled: value;
20+
}
21+
22+
.foo_bar {
23+
--color-red: red;
24+
color: var(--color-red);
25+
}

tests/webpack-test/configCases/css/local-ident-name/test.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = {
88
`style_module_css_file-local.bundle${i}.js`,
99
`style_module_css_q_f.bundle${i}.js`,
1010
`style_module_css_uniqueName-id-contenthash.bundle${i}.js`,
11+
`style_module_css_folder-local.bundle${i}.js`,
12+
`nested1_nested2_style_module_css_folder-local.bundle${i}.js`,
1113
`style_module_less.bundle${i}.js`,
1214
`bundle${i}.js`
1315
];

tests/webpack-test/configCases/css/local-ident-name/webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const common = {
5050
generator: {
5151
localIdentName: "[uniqueName]-[id]-[contenthash]"
5252
}
53+
},
54+
{
55+
resourceQuery: /\?folder-local$/,
56+
generator: {
57+
localIdentName: "[folder]-[local]"
58+
}
5359
}
5460
]
5561
}

0 commit comments

Comments
 (0)