Skip to content
  • Sponsor webpack-contrib/css-loader

  • Notifications You must be signed in to change notification settings
  • Fork 609
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f33eab0

Browse files
committedMay 22, 2024
fix: keep order of @imports with the webpackIgnore comment
1 parent e006f66 commit f33eab0

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed
 

‎src/plugins/postcss-import-parser.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
WEBPACK_IGNORE_COMMENT_REGEXP,
99
} from "../utils";
1010

11-
function parseNode(atRule, key, options) {
12-
// Convert only top-level @import
13-
if (atRule.parent.type !== "root") {
14-
return;
15-
}
16-
11+
function isIgnoredAfterName(atRule) {
1712
if (
1813
atRule.raws &&
1914
atRule.raws.afterName &&
@@ -25,20 +20,35 @@ function parseNode(atRule, key, options) {
2520
.match(WEBPACK_IGNORE_COMMENT_REGEXP);
2621

2722
if (matched && matched[2] === "true") {
28-
return;
23+
return true;
2924
}
3025
}
3126

27+
return false;
28+
}
29+
30+
function isIgnoredPrevNode(atRule) {
3231
const prevNode = atRule.prev();
3332

3433
if (prevNode && prevNode.type === "comment") {
3534
const matched = prevNode.text.match(WEBPACK_IGNORE_COMMENT_REGEXP);
3635

3736
if (matched && matched[2] === "true") {
38-
return;
37+
return true;
3938
}
4039
}
4140

41+
return false;
42+
}
43+
44+
function parseNode(atRule, key, options) {
45+
// Convert only top-level @import
46+
if (atRule.parent.type !== "root") {
47+
return;
48+
}
49+
50+
const isIgnored = isIgnoredAfterName(atRule) || isIgnoredPrevNode(atRule);
51+
4252
// Nodes do not exists - `@import url('http://') :root {}`
4353
if (atRule.nodes) {
4454
const error = new Error(
@@ -97,7 +107,12 @@ function parseNode(atRule, key, options) {
97107

98108
url = normalizeUrl(url, isStringValue);
99109

100-
const { requestable, needResolve } = isURLRequestable(url, options);
110+
let requestable = false;
111+
let needResolve = false;
112+
113+
if (!isIgnored) {
114+
({ requestable, needResolve } = isURLRequestable(url, options));
115+
}
101116

102117
let prefix;
103118

‎test/__snapshots__/import-option.test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`"import" option should jeep order of imports with 'webpackIgnore': errors 1`] = `[]`;
4+
5+
exports[`"import" option should jeep order of imports with 'webpackIgnore': module 1`] = `
6+
"// Imports
7+
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from "../../../src/runtime/noSourceMaps.js";
8+
import ___CSS_LOADER_API_IMPORT___ from "../../../src/runtime/api.js";
9+
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from "-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./test.css";
10+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
11+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(/assets/themes.css);"]);
12+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
13+
// Module
14+
___CSS_LOADER_EXPORT___.push([module.id, \`/*! /* webpackIgnore: true */
15+
16+
body {
17+
background: red;
18+
}
19+
\`, ""]);
20+
// Exports
21+
export default ___CSS_LOADER_EXPORT___;
22+
"
23+
`;
24+
25+
exports[`"import" option should jeep order of imports with 'webpackIgnore': result 1`] = `
26+
"@import url(/assets/themes.css);.test {
27+
a: a;
28+
}
29+
/*! /* webpackIgnore: true */
30+
31+
body {
32+
background: red;
33+
}
34+
"
35+
`;
36+
37+
exports[`"import" option should jeep order of imports with 'webpackIgnore': warnings 1`] = `[]`;
38+
339
exports[`"import" option should keep original order: errors 1`] = `[]`;
440

541
exports[`"import" option should keep original order: module 1`] = `
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*! /* webpackIgnore: true */
2+
@import url("/assets/themes.css");
3+
@import "~test";
4+
5+
body {
6+
background: red;
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './webpackIgnore-order.css';
2+
3+
__export__ = css.toString();
4+
5+
export default css;

‎test/import-option.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,18 @@ describe('"import" option', () => {
588588
expect(getWarnings(stats)).toMatchSnapshot("warnings");
589589
expect(getErrors(stats)).toMatchSnapshot("errors");
590590
});
591+
592+
it("should jeep order of imports with 'webpackIgnore'", async () => {
593+
const compiler = getCompiler("./import/webpackIgnore-order.js");
594+
const stats = await compile(compiler);
595+
596+
expect(
597+
getModuleSource("./import/webpackIgnore-order.css", stats),
598+
).toMatchSnapshot("module");
599+
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
600+
"result",
601+
);
602+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
603+
expect(getErrors(stats)).toMatchSnapshot("errors");
604+
});
591605
});

0 commit comments

Comments
 (0)
Please sign in to comment.