Skip to content

Commit caf150f

Browse files
fix: improve locals generation for scoped packages
1 parent 0722733 commit caf150f

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

src/utils.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,17 @@ function normalizePath(file) {
9696
return path.sep === "\\" ? file.replace(/\\/g, "/") : file;
9797
}
9898

99+
// filename reserved characters + control characters + dots + `@` for scoped packages
99100
// eslint-disable-next-line no-control-regex
100-
const filenameReservedRegex = /[<>:"/\\|?*]/g;
101-
// eslint-disable-next-line no-control-regex
102-
const reControlChars = /[\u0000-\u001f\u0080-\u009f]/g;
101+
const unallowedCharacters = /[<>:"/\\|?*\u0000-\u001f\u0080-\u009f.@]/g;
103102

104103
function escapeLocalIdent(localident) {
105104
// TODO simplify in the next major release
106105
return escape(
107106
localident
108107
// For `[hash]` placeholder
109108
.replace(/^((-?[0-9])|--)/, "_$1")
110-
.replace(filenameReservedRegex, "-")
111-
.replace(reControlChars, "-")
112-
.replace(/\./g, "-")
109+
.replace(unallowedCharacters, "-")
113110
);
114111
}
115112

test/__snapshots__/modules-option.test.js.snap

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4338,25 +4338,27 @@ exports[`"modules" option should work with \`@\` character in scoped packages: m
43384338
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../../../src/runtime/api.js\\";
43394339
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
43404340
// Module
4341-
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-issue-1223-\\\\\\\\@foo-bar--myClass {\\\\n color: red;\\\\n}\\", \\"\\"]);
4341+
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-issue-1223--foo-bar--myClass {\\\\n color: red;\\\\n}\\", \\"\\"]);
43424342
// Exports
43434343
___CSS_LOADER_EXPORT___.locals = {
4344-
\\"myClass\\": \\"modules-issue-1223-@foo-bar--myClass\\"
4344+
\\"myClass\\": \\"modules-issue-1223--foo-bar--myClass\\"
43454345
};
43464346
export default ___CSS_LOADER_EXPORT___;
43474347
"
43484348
`;
43494349

4350-
exports[`"modules" option should work with \`@\` character in scoped packages: result 1`] = `
4351-
Array [
4352-
Array [
4353-
"./modules/issue-1223/@foo/bar/index.module.css",
4354-
".modules-issue-1223-\\\\@foo-bar--myClass {
4355-
color: red;
4356-
}",
4357-
"",
4358-
],
4359-
]
4350+
exports[`"modules" option should work with \`@\` character in scoped packages: module 2`] = `
4351+
"// Imports
4352+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../../../../src/runtime/api.js\\";
4353+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(function(i){return i[1]});
4354+
// Module
4355+
___CSS_LOADER_EXPORT___.push([module.id, \\".modules-issue-1223-foo-bar--myClass {\\\\n color: red;\\\\n}\\", \\"\\"]);
4356+
// Exports
4357+
___CSS_LOADER_EXPORT___.locals = {
4358+
\\"myClass\\": \\"modules-issue-1223-foo-bar--myClass\\"
4359+
};
4360+
export default ___CSS_LOADER_EXPORT___;
4361+
"
43604362
`;
43614363

43624364
exports[`"modules" option should work with \`@\` character in scoped packages: warnings 1`] = `Array []`;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.myClass {
2+
color: red;
3+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import css from './@foo/bar/index.module.css';
1+
import css1 from './@foo/bar/index.module.css';
2+
import css2 from './foo/bar/index.module.css';
23

3-
__export__ = css;
4+
__export__ = [css1, css2];
45

5-
export default css;
6+
export default [css1, css2];

test/modules-option.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,9 +1484,13 @@ describe('"modules" option', () => {
14841484
expect(
14851485
getModuleSource("./modules/issue-1223/@foo/bar/index.module.css", stats)
14861486
).toMatchSnapshot("module");
1487-
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
1488-
"result"
1489-
);
1487+
expect(
1488+
getModuleSource("./modules/issue-1223/foo/bar/index.module.css", stats)
1489+
).toMatchSnapshot("module");
1490+
// TODO uncomment after drop webpack v4
1491+
// expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
1492+
// "result"
1493+
// );
14901494
expect(getWarnings(stats)).toMatchSnapshot("warnings");
14911495
expect(getErrors(stats)).toMatchSnapshot("errors");
14921496
});

0 commit comments

Comments
 (0)