Skip to content
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

feat: support moduleInfo.meta in dev server #5465

Merged
merged 7 commits into from Nov 10, 2021

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Oct 28, 2021

Description

Overall, this PR improves parity of Vite's dev server with Rollup via support for its meta object (see here for Rollup's explanation of meta).

  • The transform, load, and resolveId hooks can now define a meta object in their returned object, which will be merged into the meta object stored on the related ModuleNode.

  • Synchronize getModuleInfo with Vite's module graph

    • Previously, Vite would create the module info on-demand, even if the module didn't exist.
    • Accessing a property of ModuleInfo not 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?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

…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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@patak-dev
Copy link
Member

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

@patak-dev patak-dev requested a review from antfu October 28, 2021 18:15
@patak-dev patak-dev added the p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) label Nov 3, 2021
export type ResolvedUrl = [
url: string,
resolvedId: string,
meta: object | null | undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non-blocking: Is there a better type then object

@patak-dev patak-dev merged commit f6d08c7 into vitejs:main Nov 10, 2021
@Shinigami92 Shinigami92 added this to the 2.7 milestone Nov 25, 2021
@joaotavora
Copy link

Hello @aleclarson, @patak-dev I'm trying to upgrade to the latest Vite and I'm debugging a problem related to returning {id: ..., meta} in my resolveId hook. I was hopeful it would finally work after your changes, but it doesn't :-(

For reasons that I'd rather not go into, we're import ing things ouside the project root (using server.fs.strict = false) and the call to resolveId, is coming from normalizeUrl in importAnalysis.ts:

...
const resolved = await this.resolve(url, importerFile)
...

reading on, it seems that the meta field in resolved is just discarded as it used to be.

Can it be that you "missed a spot" in your changes, or am I mis-reading this?

@joaotavora
Copy link

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, getModuleInfo used to synthesize an empty meta object that I could hackily write to and it would have the same behaviour. Now that's gone.

@joaotavora
Copy link

I've filed an issue, #6766

joaotavora added a commit to joaotavora/vite that referenced this pull request Feb 8, 2022
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.
joaotavora added a commit to joaotavora/vite that referenced this pull request Feb 16, 2022
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.
@aleclarson aleclarson deleted the fix/rollup-meta branch February 25, 2022 20:03
aleclarson pushed a commit to aleclarson/vite that referenced this pull request Mar 26, 2022
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.
aleclarson added a commit to aleclarson/vite that referenced this pull request Oct 20, 2022
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
aleclarson added a commit to aleclarson/vite that referenced this pull request Oct 20, 2022
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
aleclarson added a commit to aleclarson/vite that referenced this pull request Nov 16, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants