Skip to content

Commit 76757ef

Browse files
fix: keep order of @imports with the webpackIgnore comment (#1600)
1 parent 4b41689 commit 76757ef

6 files changed

+111
-19
lines changed

src/plugins/postcss-import-parser.js

+24-9
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

+36
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`] = `

test/__snapshots__/loader.test.js.snap

+25-10
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,11 @@ var ___CSS_LOADER_URL_IMPORT_3___ = new URL("./fonts/Roboto-Regular.ttf", import
25582558
var ___CSS_LOADER_URL_IMPORT_4___ = new URL("./fonts/Roboto-Regular.svg", import.meta.url);
25592559
var ___CSS_LOADER_URL_IMPORT_5___ = new URL("./fonts/Roboto-Regular.eot", import.meta.url);
25602560
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
2561+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(./basic.css);"]);
2562+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(./imported.css);"]);
2563+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(./simple.css);"]);
2564+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(./simple.css);"]);
2565+
___CSS_LOADER_EXPORT___.push([module.id, "@import url(./simple.css);"]);
25612566
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
25622567
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
25632568
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
@@ -2572,11 +2577,6 @@ var ___CSS_LOADER_URL_REPLACEMENT_6___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_
25722577
var ___CSS_LOADER_URL_REPLACEMENT_7___ = ___CSS_LOADER_GET_URL_IMPORT___(___CSS_LOADER_URL_IMPORT_0___, { needQuotes: true });
25732578
// Module
25742579
___CSS_LOADER_EXPORT___.push([module.id, \`/* webpackIgnore: true */
2575-
@import url(./basic.css);
2576-
@import /* webpackIgnore: true */ url(./imported.css);
2577-
@import /* webpackIgnore: false */ /* webpackIgnore: true */ url(./simple.css);
2578-
@import /* webpackIgnore: false */ /* webpackIgnore: true */ /* webpackIgnore: true */ url(./simple.css);
2579-
@import /* webpackIgnore: false */ /* webpackIgnore: false */ /* webpackIgnore: true */ url(./simple.css);
25802580
25812581
/** Resolved **/
25822582
/** Resolved **/
@@ -2830,6 +2830,26 @@ export default ___CSS_LOADER_EXPORT___;
28302830
28312831
exports[`loader should work with webpackIgnore comment: result 1`] = `
28322832
[
2833+
[
2834+
"./webpackIgnore.css",
2835+
"@import url(./basic.css);",
2836+
],
2837+
[
2838+
"./webpackIgnore.css",
2839+
"@import url(./imported.css);",
2840+
],
2841+
[
2842+
"./webpackIgnore.css",
2843+
"@import url(./simple.css);",
2844+
],
2845+
[
2846+
"./webpackIgnore.css",
2847+
"@import url(./simple.css);",
2848+
],
2849+
[
2850+
"./webpackIgnore.css",
2851+
"@import url(./simple.css);",
2852+
],
28332853
[
28342854
"../../src/index.js??ruleSet[1].rules[0].use[0]!./simple.css",
28352855
".some-class {
@@ -2865,11 +2885,6 @@ exports[`loader should work with webpackIgnore comment: result 1`] = `
28652885
[
28662886
"./webpackIgnore.css",
28672887
"/* webpackIgnore: true */
2868-
@import url(./basic.css);
2869-
@import /* webpackIgnore: true */ url(./imported.css);
2870-
@import /* webpackIgnore: false */ /* webpackIgnore: true */ url(./simple.css);
2871-
@import /* webpackIgnore: false */ /* webpackIgnore: true */ /* webpackIgnore: true */ url(./simple.css);
2872-
@import /* webpackIgnore: false */ /* webpackIgnore: false */ /* webpackIgnore: true */ url(./simple.css);
28732888
28742889
/** Resolved **/
28752890
/** Resolved **/
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+
}
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

+14
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)