From 704de3da801918372660289078741d1159de0a71 Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Mon, 25 Aug 2025 11:59:48 +0200 Subject: [PATCH 1/2] fix(mf): use unresolved request to match shared module --- crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs index 0d243d60e5fb..f04cd39cfc24 100644 --- a/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs +++ b/crates/rspack_plugin_mf/src/sharing/provide_shared_plugin.rs @@ -242,7 +242,10 @@ async fn normal_module_factory_module( { return Ok(()); } - let request = &create_data.raw_request; + let dependency = data.dependencies[0] + .as_module_dependency() + .expect("should be module dependency"); + let request = dependency.request(); { let match_provides = self.match_provides.read().await; if let Some(config) = match_provides.get(request) { From 8163e7972c238d030a01eaa54d8cfd1baf0014c3 Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Mon, 13 Oct 2025 15:58:50 +0200 Subject: [PATCH 2/2] test: ensure that the provide plugin is compatible with custom resolvers --- .../index.js | 13 +++++++++++ .../node_modules/x/index.js | 1 + .../node_modules/x/package.json | 3 +++ .../node_modules/y/index.js | 1 + .../node_modules/y/package.json | 3 +++ .../rspack.config.js | 23 +++++++++++++++++++ 6 files changed, 44 insertions(+) create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/index.js create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/index.js create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/package.json create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/index.js create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/package.json create mode 100644 tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/rspack.config.js diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/index.js b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/index.js new file mode 100644 index 000000000000..b49589ce7eaf --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/index.js @@ -0,0 +1,13 @@ +import x from "x"; +import y from "y"; + +it("should work", () => { + expect(x).toBe(42); + expect(y).toBe(24); +}) + +it("should add provided modules to the share scope - no matter the resolver", async () => { + await __webpack_init_sharing__("default"); + expect(Object.keys(__webpack_share_scopes__.default)).toContain("x"); + expect(Object.keys(__webpack_share_scopes__.default)).toContain("y"); +}); diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/index.js b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/index.js new file mode 100644 index 000000000000..888cae37af95 --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/index.js @@ -0,0 +1 @@ +module.exports = 42; diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/package.json b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/package.json new file mode 100644 index 000000000000..1587a669681c --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/x/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/index.js b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/index.js new file mode 100644 index 000000000000..1997295ec9e9 --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/index.js @@ -0,0 +1 @@ +module.exports = 24; diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/package.json b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/package.json new file mode 100644 index 000000000000..1587a669681c --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/node_modules/y/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/rspack.config.js b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/rspack.config.js new file mode 100644 index 000000000000..14b9592e6f80 --- /dev/null +++ b/tests/rspack-test/configCases/sharing/provide-shared-with-custom-resolver/rspack.config.js @@ -0,0 +1,23 @@ +// eslint-disable-next-line node/no-unpublished-require +const { ProvideSharedPlugin } = require("@rspack/core").sharing; + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + plugins: [ + new ProvideSharedPlugin({ + provides: ["x", "y"] + }), + function (compiler) { + compiler.hooks.thisCompilation.tap( + "customResolver", + (compilation, { normalModuleFactory }) => { + normalModuleFactory.hooks.resolve.tap("customResolver", (data) => { + if (data.request === "x") { + data.request = require.resolve(data.request); + } + }); + } + ) + } + ] +};