From e2616fcd5d47928be5c66ce50ef6cf0feb5e5c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20MANCA?= Date: Fri, 13 May 2022 09:53:05 +0200 Subject: [PATCH] fix(css): add node support for external @import --- lib/WebpackOptionsApply.js | 22 +++++++++++++++++++ .../configCases/css/external-in-node/index.js | 6 +++++ .../css/external-in-node/webpack.config.js | 11 ++++++++++ 3 files changed, 39 insertions(+) create mode 100644 test/configCases/css/external-in-node/index.js create mode 100644 test/configCases/css/external-in-node/webpack.config.js 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 + } +};