feat: support moduleInfo.meta in dev server#5465
Conversation
…which allows the use of `moduleInfo.meta` for plugin metadata, and `getModuleInfo` will return null if the module graph has no record of the given module ID. Other properties of ModuleInfo will throw when accessed, so incompatible plugins fail in a non-cryptic manner.
Handle a `meta` property being returned from load, transform, and resolveId plugin hooks.
This provides an efficient heuristic for knowing if a module is virtual.
| const container = await createPluginContainer(config, watcher) | ||
| const moduleGraph = new ModuleGraph(container) | ||
| const moduleGraph: ModuleGraph = new ModuleGraph((url) => | ||
| container.resolveId(url) |
There was a problem hiding this comment.
This creates a tight coupling between ModuleGraph and createPluginContainer, but I don't see any way to avoid that. And it's probably not a big deal.
|
This looks good to me but we should at least add some basic tests to avoid future regressions. If we get the tests, I think we could merge this one during the beta |
This reverts commit 9ee56ab.
The `resolveId` method stops on first truthy result, so merging is never needed.
| export type ResolvedUrl = [ | ||
| url: string, | ||
| resolvedId: string, | ||
| meta: object | null | undefined |
There was a problem hiding this comment.
non-blocking: Is there a better type then object
|
Hello @aleclarson, @patak-dev I'm trying to upgrade to the latest Vite and I'm debugging a problem related to returning For reasons that I'd rather not go into, we're ...
const resolved = await this.resolve(url, importerFile)
...reading on, it seems that the Can it be that you "missed a spot" in your changes, or am I mis-reading this? |
|
Looks like this happens even if importees are indeed inside the project root. Wonder if this case was considered. I'll try to come up with a repro later, but it would be great to have your thoughts on this, i.e. how this was intented to work. For now, I'm seeing a regression becasue, even though returning {meta} didn't work like in Rollup and build, |
|
I've filed an issue, #6766 |
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour and fixes vitejs#6766. Fix: vitejs#6766 * packages/vite/src/node/importGlob.ts (ResolvedUrl): Import it. (transformImportGlob): Use it. * packages/vite/src/node/plugins/importAnalysis.ts (ResolvedUrl): Import it. (normalizeUrl): Use it. Always return meta. (transform): Ensure moduleGraph is correctly built.
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour and fixes vitejs#6766. Fix: vitejs#6766 * packages/vite/src/node/importGlob.ts (ResolvedUrl): Import it. (transformImportGlob): Use it. * packages/vite/src/node/plugins/importAnalysis.ts (ResolvedUrl): Import it. (normalizeUrl): Use it. Always return meta. (transform): Ensure moduleGraph is correctly built.
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour and fixes vitejs#6766. Fix: vitejs#6766 * packages/vite/src/node/importGlob.ts (ResolvedUrl): Import it. (transformImportGlob): Use it. * packages/vite/src/node/plugins/importAnalysis.ts (ResolvedUrl): Import it. (normalizeUrl): Use it. Always return meta. (transform): Ensure moduleGraph is correctly built.
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour. Fixes vitejs#6766
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour. Fixes vitejs#6766
This commit improves the parity of behaviour between 'vite build' and 'vite dev' when Rollup plugins retuning non-null 'meta' properties from resolveId() hooks are used. If one takes the Rollup behaviour to be any kind of reference in these cases, this is fixing a small bug in Vite, which was in a very large part already fixed by PR vitejs#5465. Here's an explanation of the faulty behaviour: The normalizeUrl() helper calls user plugins's resolveId() twice. - Once with the URL string containing a query suffix. Here it _ignores_ the meta reply. - Again, with the URL no longer containing said suffix. Here it doesn't ignore the meta reply. It is stored in the moduleGraph node. As can be seen, if the user's plugin meta reply depends on the query suffix being passed in the imported URL, that meta reply won't be registered correctly in the module graph and is not retrievable via getModuleInfo() later on. This change makes it so that the first call doesn't ignore the meta reply. This makes Vite's dev server match Rollup's plugin-calling behaviour. Fixes vitejs#6766
Description
Overall, this PR improves parity of Vite's dev server with Rollup via support for its
metaobject (see here for Rollup's explanation ofmeta).The
transform,load, andresolveIdhooks can now define ametaobject in their returned object, which will be merged into themetaobject stored on the relatedModuleNode.Synchronize
getModuleInfowith Vite's module graphModuleInfonot supported by Vite will now throw a runtime error, so the offending plugin doesn't fail silently.What is the purpose of this pull request?