diff --git a/lib/WebpackOptionsApply.js b/lib/WebpackOptionsApply.js index c6d59400001..96a485f7400 100644 --- a/lib/WebpackOptionsApply.js +++ b/lib/WebpackOptionsApply.js @@ -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); diff --git a/test/configCases/css/external-in-node/index.js b/test/configCases/css/external-in-node/index.js new file mode 100644 index 00000000000..526b3c0a8b2 --- /dev/null +++ b/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); +}); diff --git a/test/configCases/css/external-in-node/webpack.config.js b/test/configCases/css/external-in-node/webpack.config.js new file mode 100644 index 00000000000..87766dc8dae --- /dev/null +++ b/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 + } +};