-
-
Notifications
You must be signed in to change notification settings - Fork 738
fix(mf): use unresolved request to match shared module #11478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| }); | ||
| } | ||
| ) | ||
| } | ||
| ] | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better way is use
data.requesthere, should also solve the issue sincedependency.requestis assigned fromdata.requestUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 I tried to do that but it seems that
data.requestcontains the mutated resolved path instead of the original request (since this change?). I could not find another practical way of retrieving it except extracting it fromdependency[0].requestwhich does contain the pristine specifier.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, have you tried webpack that they use the unresolved request or the changed request (changed by
NormalModuleFactory.hooks.afterResolve)? If I remember correctly they will use the changed request. So at this point I'd like to keep the consistent with webpackThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh there is another issue about ConsumeSharedPlugin not using the changed request #11437 (your PR is about ProvideSharedPlugin not using original request)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see this is more complicated than expected.
Oh, I can double check 👍.
What worries me is that if the logic is kept like this, it would make it very complicated to use a custom resolver - like with packages having subpath "exports" fields - because I'm not sure resolving all these paths ahead of time when generating the MF config + using the
importfield would be straightforward.Especially with packages that only export deep subpaths, without root exports. (
"exports": {"." { … } })I'm wondering, what would be the expected outcome in this case? The consumer would load the module from the resolved path and assume that it is not provided by a producer?
In addition to this PR changes, I tried modifying the request to use the resolved path
&data.requestin the factorize hook here and it seems to solve both issues but I'm not fully sure that I get all the implications yet.I'll try to dig into it and get a better understanding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious how you use the custom resolver that resolves paths ahead of time. If you resolve paths before compile, you can use the resolved paths to generate a MF plugin (configured with resolved paths instead of the original paths) and apply to the compiler
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this is precisely the problem, I don't think that I can easily resolve all the possible subpaths for shared modules before compilation because I have no knowledge of the imports in the codebase. For simple cases it works, but as soon as I try to share a module exporting only subpaths it breaks (and the trailing slash syntax -
module/- does not work when using an absolute path fallback).For instance, for these kind of packages…
{ "name": "package", "main": "index.js" }…I cannot guess what the imports will look like in the provider code…
…and it becomes very hard to generate the MF config:
Whereas this config used to work just fine with
v1.4.6: