Support for moduleInfo.meta in resolveId hook still not equivalent in vite dev and vite build
See original GitHub issueDescribe the bug
In https://github.com/vitejs/vite/pull/5465 support for moduleInfo.meta
is added to the dev server. This is a very welcome change that I look forward to using. Unforunately, behavioral parity between vite build
and vite dev
is not quite there yet. In the attached reproduction recipe the former succeeds while the latter fails. By “failing” I mean the transform
hook is never called and doesn’t inject an important transformation into foo.js
. Here are relevant parts of the files.
//main.js
import fn from './foo.js?xoption=21'
document.querySelector('#app').innerHTML = `<h1>Hello Vite: ${fn()}</h1>`
//foo.js
export default function () {return 21 + window.THE_X;}
//vite.config.js
import path from 'path';
export default {
plugins: [
{
enforce: 'pre',
async resolveId(id) {
let [file, query] = id.split('?');
const probe = query && query.match(/xoption=([^&]+)/)
const x = probe && Number(probe[1])
if (x) return {id: path.resolve(file), meta: {x}}
},
async transform(blob, id) {
const x = this.getModuleInfo(id)?.meta?.x
if (x) return `window.THE_X = ${x}\n` + blob;
}
}
]
}
To be clear, this particular solution “never” worked with Vite before. However, before #5465, a workaround/hack (not shown) could be devised, because getModuleInfo
called in he resolveId
hook would return a persistent empty object that could be used to fill in the shortcomings that #5465 hopes to address. Ironically, that workaround is now not possible anymore.
If this is accepted as a bug, I’d be happy to provide a PR to fix this.
Reproduction
https://github.com/joaotavora/bug-vite-return-meta-from-resolve-id
System Info
System:
OS: Linux 5.15 Arch Linux
CPU: (4) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
Memory: 3.21 GB / 15.39 GB
Container: Yes
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.9.0 - ~/.nvm/versions/node/v16.9.0/bin/node
Yarn: 1.22.11 - ~/.nvm/versions/node/v16.9.0/bin/yarn
npm: 7.21.1 - ~/.nvm/versions/node/v16.9.0/bin/npm
Browsers:
Chromium: 96.0.4664.45
npmPackages:
vite: ^2.7.2 => 2.8.0-beta.5
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (3 by maintainers)
Top GitHub Comments
Done. See. https://github.com/vitejs/vite/pull/6811
Shouldn’t this issue be reopened so that that that PR (or any other PR…) can subsequentely close it?
Please open a PR and we can get Evan’s feedback