Skip to content

Commit 0dc1659

Browse files
refactor: logic
1 parent 811988a commit 0dc1659

5 files changed

Lines changed: 49 additions & 51 deletions

File tree

lib/css/CssModulesPlugin.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -699,42 +699,7 @@ class CssModulesPlugin {
699699
if (!modules) return [];
700700

701701
/** @type {Module[]} */
702-
const modulesList = [];
703-
704-
/** @type {string | undefined} */
705-
let charset;
706-
707-
for (const module of modules) {
708-
if (
709-
typeof (/** @type {BuildInfo} */ (module.buildInfo).charset) !==
710-
"undefined"
711-
) {
712-
if (
713-
typeof charset !== "undefined" &&
714-
charset !== /** @type {BuildInfo} */ (module.buildInfo).charset
715-
) {
716-
const err = new WebpackError(
717-
`Conflicting @charset at-rules detected: the module ${module.readableIdentifier(
718-
compilation.requestShortener
719-
)} (in chunk ${chunk.name || chunk.id}) specifies "${
720-
/** @type {BuildInfo} */ (module.buildInfo).charset
721-
}", but "${charset}" was expected, all modules must use the same character set`
722-
);
723-
724-
err.chunk = chunk;
725-
err.module = module;
726-
err.hideStack = true;
727-
728-
compilation.warnings.push(err);
729-
}
730-
731-
if (typeof charset === "undefined") {
732-
charset = /** @type {BuildInfo} */ (module.buildInfo).charset;
733-
}
734-
}
735-
736-
modulesList.push(module);
737-
}
702+
const modulesList = [...modules];
738703

739704
// Get ordered list of modules per chunk group
740705
// Lists are in reverse order to allow to use Array.pop()
@@ -878,6 +843,9 @@ class CssModulesPlugin {
878843
* @returns {CssModule[]} ordered css modules
879844
*/
880845
getOrderedChunkCssModules(chunk, chunkGraph, compilation) {
846+
/** @type {string | undefined} */
847+
let charset;
848+
881849
return /** @type {CssModule[]} */ ([
882850
...this.getModulesInOrder(
883851
chunk,
@@ -896,7 +864,37 @@ class CssModulesPlugin {
896864
compareModulesByFullName(compilation.compiler)
897865
),
898866
compilation
899-
)
867+
).map((module) => {
868+
if (
869+
typeof (/** @type {BuildInfo} */ (module.buildInfo).charset) !==
870+
"undefined"
871+
) {
872+
if (
873+
typeof charset !== "undefined" &&
874+
charset !== /** @type {BuildInfo} */ (module.buildInfo).charset
875+
) {
876+
const err = new WebpackError(
877+
`Conflicting @charset at-rules detected: the module ${module.readableIdentifier(
878+
compilation.requestShortener
879+
)} (in chunk ${chunk.name || chunk.id}) specifies "${
880+
/** @type {BuildInfo} */ (module.buildInfo).charset
881+
}", but "${charset}" was expected, all modules must use the same character set`
882+
);
883+
884+
err.chunk = chunk;
885+
err.module = module;
886+
err.hideStack = true;
887+
888+
compilation.warnings.push(err);
889+
}
890+
891+
if (typeof charset === "undefined") {
892+
charset = /** @type {BuildInfo} */ (module.buildInfo).charset;
893+
}
894+
}
895+
896+
return module;
897+
})
900898
]);
901899
}
902900

lib/css/CssParser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,15 +2360,15 @@ class CssParser extends Parser {
23602360
const value = walkCssTokens.eatString(input, end);
23612361

23622362
if (!value) {
2363-
return end;
2363+
return atRuleEnd;
23642364
}
23652365

23662366
/** @type {BuildInfo} */
23672367
(module.buildInfo).charset = input
23682368
.slice(value[0] + 1, value[1] - 1)
23692369
.toUpperCase();
23702370

2371-
return end;
2371+
return atRuleEnd;
23722372
}
23732373
case "@import": {
23742374
if (!this.options.import) {
@@ -2383,7 +2383,7 @@ class CssParser extends Parser {
23832383
start,
23842384
end
23852385
);
2386-
return end;
2386+
return eatUntilSemi(input, end);
23872387
}
23882388

23892389
return processAtImport(input, start, end);

test/configCases/css/charset/__snapshots__/ConfigCacheTest.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigCacheTestCases css charset exported tests should work handle \`@charset\` at-rule 1`] = `
3+
exports[`ConfigCacheTestCases css charset exported tests should handle \`@charset\` at-rule 1`] = `
44
Array [
55
"@charset \\"UTF-8\\";
66
/*!***********************************************!*\\\\
@@ -48,7 +48,7 @@ Array [
4848
]
4949
`;
5050

51-
exports[`ConfigCacheTestCases css charset exported tests should work handle \`@charset\` at-rule 2`] = `
51+
exports[`ConfigCacheTestCases css charset exported tests should handle \`@charset\` at-rule 2`] = `
5252
"/*!*******************************************************!*\\\\
5353
!*** css ./import-nested.text.css (exportType: text) ***!
5454
\\\\*******************************************************/
@@ -118,7 +118,7 @@ exports[`ConfigCacheTestCases css charset exported tests should work handle \`@c
118118
"
119119
`;
120120

121-
exports[`ConfigCacheTestCases css charset exported tests should work handle \`@charset\` at-rule 3`] = `
121+
exports[`ConfigCacheTestCases css charset exported tests should handle \`@charset\` at-rule 3`] = `
122122
"/*!*******************************************************!*\\\\
123123
!*** css ./import-nested.text.css (exportType: text) ***!
124124
\\\\*******************************************************/
@@ -150,7 +150,7 @@ exports[`ConfigCacheTestCases css charset exported tests should work handle \`@c
150150
"
151151
`;
152152

153-
exports[`ConfigCacheTestCases css charset exported tests should work handle \`@charset\` at-rule 4`] = `
153+
exports[`ConfigCacheTestCases css charset exported tests should handle \`@charset\` at-rule 4`] = `
154154
"@charset \\"UTF-8\\";
155155
/*!*******************************************************!*\\\\
156156
!*** css ./import-nested.text.css (exportType: text) ***!
@@ -193,7 +193,7 @@ exports[`ConfigCacheTestCases css charset exported tests should work handle \`@c
193193
"
194194
`;
195195

196-
exports[`ConfigCacheTestCases css charset exported tests should work handle \`@charset\` at-rule 5`] = `
196+
exports[`ConfigCacheTestCases css charset exported tests should handle \`@charset\` at-rule 5`] = `
197197
Array [
198198
"/*!****************************************************!*\\\\
199199
!*** css ./styles-6.style.css (exportType: style) ***!

test/configCases/css/charset/__snapshots__/ConfigTest.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigTestCases css charset exported tests should work handle \`@charset\` at-rule 1`] = `
3+
exports[`ConfigTestCases css charset exported tests should handle \`@charset\` at-rule 1`] = `
44
Array [
55
"@charset \\"UTF-8\\";
66
/*!***********************************************!*\\\\
@@ -48,7 +48,7 @@ Array [
4848
]
4949
`;
5050

51-
exports[`ConfigTestCases css charset exported tests should work handle \`@charset\` at-rule 2`] = `
51+
exports[`ConfigTestCases css charset exported tests should handle \`@charset\` at-rule 2`] = `
5252
"/*!*******************************************************!*\\\\
5353
!*** css ./import-nested.text.css (exportType: text) ***!
5454
\\\\*******************************************************/
@@ -118,7 +118,7 @@ exports[`ConfigTestCases css charset exported tests should work handle \`@charse
118118
"
119119
`;
120120

121-
exports[`ConfigTestCases css charset exported tests should work handle \`@charset\` at-rule 3`] = `
121+
exports[`ConfigTestCases css charset exported tests should handle \`@charset\` at-rule 3`] = `
122122
"/*!*******************************************************!*\\\\
123123
!*** css ./import-nested.text.css (exportType: text) ***!
124124
\\\\*******************************************************/
@@ -150,7 +150,7 @@ exports[`ConfigTestCases css charset exported tests should work handle \`@charse
150150
"
151151
`;
152152

153-
exports[`ConfigTestCases css charset exported tests should work handle \`@charset\` at-rule 4`] = `
153+
exports[`ConfigTestCases css charset exported tests should handle \`@charset\` at-rule 4`] = `
154154
"@charset \\"UTF-8\\";
155155
/*!*******************************************************!*\\\\
156156
!*** css ./import-nested.text.css (exportType: text) ***!
@@ -193,7 +193,7 @@ exports[`ConfigTestCases css charset exported tests should work handle \`@charse
193193
"
194194
`;
195195

196-
exports[`ConfigTestCases css charset exported tests should work handle \`@charset\` at-rule 5`] = `
196+
exports[`ConfigTestCases css charset exported tests should handle \`@charset\` at-rule 5`] = `
197197
Array [
198198
"/*!****************************************************!*\\\\
199199
!*** css ./styles-6.style.css (exportType: style) ***!

test/configCases/css/charset/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import textImport from "./import.text.css";
66
import styleSheet from "./styles-5.css-style-sheet.css";
77
import "./styles-6.style.css";
88

9-
it("should work handle `@charset` at-rule", () => {
9+
it("should handle `@charset` at-rule", () => {
1010
const links = document.getElementsByTagName("link");
1111
const css1 = [];
1212

0 commit comments

Comments
 (0)