feat: add context option support for VirtualUrlPlugin#20449
feat: add context option support for VirtualUrlPlugin#20449alexander-akait merged 4 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: ad7b477 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
This PR is packaged and the instant preview is available (214f361). Install it locally:
npm i -D webpack@https://pkg.pr.new/webpack@214f361
yarn add -D webpack@https://pkg.pr.new/webpack@214f361
pnpm add -D webpack@https://pkg.pr.new/webpack@214f361 |
7026dca to
9fe896a
Compare
| resourceData.context = virtualConfig.context; | ||
| } else { | ||
| resourceData.context = compiler.context; | ||
| } |
There was a problem hiding this comment.
let's allow only string context, by default - compiler.context, otherwise virtualConfig.context
There was a problem hiding this comment.
I’m curious why this isn’t true by default 🤔, like auto, where we automatically set the context for virtual modules. Since the virtual module ID and the context value are almost the same, it feels like setting the same value twice. I’d prefer to minimize users’ exposure to the context option unless there’s a very specific use case.
There was a problem hiding this comment.
I agree, that is why I think we don't need support function and true type, only string, also I don't think we need to check on undefined we by default use compiler.context
There was a problem hiding this comment.
Makes sense. For now, I’ve kept only the default value "auto". This avoids requiring users to configure the context option and also prevents configs from being cluttered with repeated "auto" values, like { mod1: { context: "auto", source() {} }, mod2: { context: "auto", source() {} }, mod3: { context: "auto", source() {} } }.
There was a problem hiding this comment.
I am afraid that it is a breaking change, auto is the new behavior, old virtual modules can be broken, right?
Do I understand correctly this feature, if I have /path/to/src/components/button.js, context will be path/to/src/components/?
There was a problem hiding this comment.
This won’t affect existing virtual modules. Previously, virtual module paths like "virtual:routes" were only used as unique IDs, and their context was always fixed to compiler.context. With context: "auto", for "virtual:routes" the computed context is still compiler.context, so nothing changes.
Now we’d like to extend virtual module paths so they’re not just IDs, but virtual paths with context information — for example, virtual:src/components/button.js. In that case, the context would be resolved to something like /path/to/src/components/button.js.
There was a problem hiding this comment.
@xiaoxiaojx Got it, I see the default value should be auto, but I don't see this in our code
There was a problem hiding this comment.
@xiaoxiaojx If I understand correctly, I agree with @alexander-akait that this is a breaking change, because if anyone was already using slashes in their virtual module ID, the context will now be different.
I think that's a reasonable breaking change, though :)
9fe896a to
f9153b5
Compare
Merging this PR will degrade performance by 22.78%
Performance Changes
Comparing |
f9153b5 to
0b2874a
Compare
| modules, | ||
| ...(scheme !== undefined && { scheme }) | ||
| }; | ||
| validate(options); |
There was a problem hiding this comment.
Let's move validate under the new hook for such things
| const context = | ||
| typeof virtualConfig.context === "string" | ||
| ? virtualConfig.context | ||
| : "auto"; |
There was a problem hiding this comment.
There is an interesting question, change this potentially can break existing code for developers which already use it (right?). So we should decide it is a fix (and our current behavior is wrong and it should be auto by default) or do not change it by default on auto, what do you think?
There was a problem hiding this comment.
Yeah, the default "auto" might not be the best choice. It was originally set to avoid having to configure the context option for every module. I recently did a small refactor here and added a fallback options to provide default settings.
ecaee86 to
7cd5b93
Compare
Summary
Add
contextoption support for VirtualUrlPluginWhat kind of change does this PR introduce?
feat
Did you add tests for your changes?
Yes
Does this PR introduce a breaking change?
No
If relevant, what needs to be documented once your changes are merged or what have you already documented?
Nothing