question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for moduleInfo.meta in resolveId hook still not equivalent in vite dev and vite build

See original GitHub issue

Describe 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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
joaotavoracommented, Feb 8, 2022

Please open a PR and we can get Evan’s feedback

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?

1reaction
aleclarsoncommented, Feb 7, 2022

Please open a PR and we can get Evan’s feedback

Read more comments on GitHub >

github_iconTop Results From Across the Web

rollup.js
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library...
Read more >
Command Line Interface - Vite
Start Vite dev server in the current directory. Will enter the watch mode in development environment and run mode in CI automatically.
Read more >
Source - GitHub
resolveId ()`: use `this.resolve()` - The `resolveAssetUrl` plugin hook has been removed: use `resolveFileUrl` - Rollup no longer passes `assetReferenceId` ...
Read more >
rollup | Yarn - Package Manager
... when config file change is detected in watch mode (@lukastaegert); #4347: Create a shallow copy when returning meta from resolveId (@lukastaegert) ...
Read more >
Introduction - rollup.js
Rollup configuration files are optional, but they are powerful and convenient and thus recommended. A config file is an ES module that exports...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found