Skip to content

Commit

Permalink
Merge pull request #15818 from noreiller/fix-css-external-in-node
Browse files Browse the repository at this point in the history
fix(css): add node support for external @import
  • Loading branch information
sokra committed Nov 9, 2022
2 parents aa560ad + e2616fc commit fcccd19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/WebpackOptionsApply.js
Expand Up @@ -160,6 +160,28 @@ class WebpackOptionsApply extends OptionsApply {
}
: /^(\/\/|https?:\/\/|std:)/
).apply(compiler);
} else if (options.externalsPresets.node) {
if (options.experiments.css) {
//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
const ExternalsPlugin = require("./ExternalsPlugin");
new ExternalsPlugin(
"module",
({ request, dependencyType }, callback) => {
if (dependencyType === "url") {
if (/^(\/\/|https?:\/\/)/.test(request))
return callback(null, `asset ${request}`);
} else if (dependencyType === "css-import") {
if (/^(\/\/|https?:\/\/)/.test(request))
return callback(null, `css-import ${request}`);
} else if (/^(\/\/|https?:\/\/|std:)/.test(request)) {
if (/^\.css(\?|$)/.test(request))
return callback(null, `css-import ${request}`);
return callback(null, `module ${request}`);
}
callback();
}
).apply(compiler);
}
}

new ChunkPrefetchPreloadPlugin().apply(compiler);
Expand Down
6 changes: 6 additions & 0 deletions test/configCases/css/external-in-node/index.js
@@ -0,0 +1,6 @@
it("should import an external css", done => {
import("../external/style.css").then(x => {
expect(x).toEqual(nsObj({}));
done();
}, done);
});
11 changes: 11 additions & 0 deletions test/configCases/css/external-in-node/webpack.config.js
@@ -0,0 +1,11 @@
const path = require("path");

/** @type {import("../../../../").Configuration} */
module.exports = {
context: path.join(__dirname, "../external"),
entry: "../external-in-node/index.js",
target: "node",
experiments: {
css: true
}
};

0 comments on commit fcccd19

Please sign in to comment.