Skip to content

Commit

Permalink
fix: keep order of @imports with the webpackIgnore comment
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed May 22, 2024
1 parent e006f66 commit f33eab0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
WEBPACK_IGNORE_COMMENT_REGEXP,
} from "../utils";

function parseNode(atRule, key, options) {
// Convert only top-level @import
if (atRule.parent.type !== "root") {
return;
}

function isIgnoredAfterName(atRule) {
if (
atRule.raws &&
atRule.raws.afterName &&
Expand All @@ -25,20 +20,35 @@ function parseNode(atRule, key, options) {
.match(WEBPACK_IGNORE_COMMENT_REGEXP);

if (matched && matched[2] === "true") {
return;
return true;
}
}

return false;
}

function isIgnoredPrevNode(atRule) {
const prevNode = atRule.prev();

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

if (matched && matched[2] === "true") {
return;
return true;
}
}

return false;
}

function parseNode(atRule, key, options) {
// Convert only top-level @import
if (atRule.parent.type !== "root") {
return;
}

const isIgnored = isIgnoredAfterName(atRule) || isIgnoredPrevNode(atRule);

// Nodes do not exists - `@import url('http://') :root {}`
if (atRule.nodes) {
const error = new Error(
Expand Down Expand Up @@ -97,7 +107,12 @@ function parseNode(atRule, key, options) {

url = normalizeUrl(url, isStringValue);

const { requestable, needResolve } = isURLRequestable(url, options);
let requestable = false;
let needResolve = false;

if (!isIgnored) {
({ requestable, needResolve } = isURLRequestable(url, options));
}

let prefix;

Expand Down
36 changes: 36 additions & 0 deletions test/__snapshots__/import-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`"import" option should jeep order of imports with 'webpackIgnore': errors 1`] = `[]`;

exports[`"import" option should jeep order of imports with 'webpackIgnore': module 1`] = `
"// Imports
import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from "../../../src/runtime/noSourceMaps.js";
import ___CSS_LOADER_API_IMPORT___ from "../../../src/runtime/api.js";
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from "-!../../../src/index.js??ruleSet[1].rules[0].use[0]!./test.css";
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___);
___CSS_LOADER_EXPORT___.push([module.id, "@import url(/assets/themes.css);"]);
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
// Module
___CSS_LOADER_EXPORT___.push([module.id, \`/*! /* webpackIgnore: true */
body {
background: red;
}
\`, ""]);
// Exports
export default ___CSS_LOADER_EXPORT___;
"
`;

exports[`"import" option should jeep order of imports with 'webpackIgnore': result 1`] = `
"@import url(/assets/themes.css);.test {
a: a;
}
/*! /* webpackIgnore: true */
body {
background: red;
}
"
`;

exports[`"import" option should jeep order of imports with 'webpackIgnore': warnings 1`] = `[]`;

exports[`"import" option should keep original order: errors 1`] = `[]`;

exports[`"import" option should keep original order: module 1`] = `
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/import/webpackIgnore-order.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*! /* webpackIgnore: true */
@import url("/assets/themes.css");
@import "~test";

body {
background: red;
}
5 changes: 5 additions & 0 deletions test/fixtures/import/webpackIgnore-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import css from './webpackIgnore-order.css';

__export__ = css.toString();

export default css;
14 changes: 14 additions & 0 deletions test/import-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,18 @@ describe('"import" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should jeep order of imports with 'webpackIgnore'", async () => {
const compiler = getCompiler("./import/webpackIgnore-order.js");
const stats = await compile(compiler);

expect(
getModuleSource("./import/webpackIgnore-order.css", stats),
).toMatchSnapshot("module");
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
"result",
);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});

0 comments on commit f33eab0

Please sign in to comment.