From 8d3a2303663090cf3f86131ff59159175af94cd3 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 14 Apr 2021 19:48:22 +0200 Subject: [PATCH] fix passing publicPath to `this.importModule` --- lib/dependencies/LoaderPlugin.js | 7 ++++++- .../loader-import-module/css/index.js | 2 +- .../loader-import-module/css/loader.js | 3 ++- .../loader-import-module/css/webpack.config.js | 18 +++++++++++++++--- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/dependencies/LoaderPlugin.js b/lib/dependencies/LoaderPlugin.js index 9386bb3efb7..7f4e3abe316 100644 --- a/lib/dependencies/LoaderPlugin.js +++ b/lib/dependencies/LoaderPlugin.js @@ -31,6 +31,7 @@ const LoaderImportDependency = require("./LoaderImportDependency"); /** * @typedef {Object} ImportModuleOptions * @property {string=} layer the target layer + * @property {string=} publicPath the target public path */ class LoaderPlugin { @@ -199,7 +200,11 @@ class LoaderPlugin { } compilation.executeModule( referencedModule, - {}, + { + entryOptions: { + publicPath: options.publicPath + } + }, (err, result) => { if (err) return callback(err); for (const d of result.fileDependencies) { diff --git a/test/configCases/loader-import-module/css/index.js b/test/configCases/loader-import-module/css/index.js index dcb60527595..9d5739c3972 100644 --- a/test/configCases/loader-import-module/css/index.js +++ b/test/configCases/loader-import-module/css/index.js @@ -10,6 +10,6 @@ it("should be able to use build-time code", () => { 'body { background: url("/public/assets/file.png?1"); color: #f00; }' ); expect(otherStylesheet).toBe( - 'body { background: url("/public/assets/file.jpg"); color: #0f0; }' + 'body { background: url("/other/assets/file.jpg"); color: #0f0; }' ); }); diff --git a/test/configCases/loader-import-module/css/loader.js b/test/configCases/loader-import-module/css/loader.js index aee770b1325..d9b5022ab4a 100644 --- a/test/configCases/loader-import-module/css/loader.js +++ b/test/configCases/loader-import-module/css/loader.js @@ -1,6 +1,7 @@ exports.pitch = async function (remaining) { const result = await this.importModule( - this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining + this.resourcePath + ".webpack[javascript/auto]" + "!=!" + remaining, + this.getOptions() ); return result.default || result; }; diff --git a/test/configCases/loader-import-module/css/webpack.config.js b/test/configCases/loader-import-module/css/webpack.config.js index dbe33ba563b..5b615633247 100644 --- a/test/configCases/loader-import-module/css/webpack.config.js +++ b/test/configCases/loader-import-module/css/webpack.config.js @@ -16,9 +16,21 @@ module.exports = { }, rules: [ { - test: /stylesheet\.js$/, - use: "./loader", - type: "asset/source" + oneOf: [ + { + test: /other-stylesheet\.js$/, + loader: "./loader", + options: { + publicPath: "/other/" + }, + type: "asset/source" + }, + { + test: /stylesheet\.js$/, + use: "./loader", + type: "asset/source" + } + ] }, { test: /\.jpg$/,