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

Vite fails to resolve assets from packages when specified as new URL('asset-file-name', import.meta.url) in dev mode #19310

Closed
7 tasks done
fs-eire opened this issue Jan 28, 2025 · 2 comments

Comments

@fs-eire
Copy link

fs-eire commented Jan 28, 2025

Describe the bug

Vite will fail to resolve assets from packages in dev mode, when it's specified as the following format:

new URL('asset-file-name', import.meta.url)

Reproduction

https://github.com/fs-eire/vite-bug-repro-asset-import-meta-url

Steps to reproduce

Please follow the README in the reproduction steps in https://github.com/fs-eire/vite-bug-repro-asset-import-meta-url

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (24) x64 Intel(R) Core(TM) i9-10920X CPU @ 3.50GHz
    Memory: 33.46 GB / 63.72 GB
  Binaries:
    Node: 22.12.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.9.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (131.0.2903.70)
    Internet Explorer: 11.0.26100.1882
  npmPackages:
    vite: ^6.0.5 => 6.0.11

Used Package Manager

npm

Logs

Please refer to the reproduction repo for more details.

The bug only exists in dev mode. In production mode, it works fine.

Validations

@fs-eire
Copy link
Author

fs-eire commented Jan 28, 2025

Possible reason from my investigation:

Considering the code like this:
new URL("test.wasm", import.meta.url)

In dev mode, when a JavaScript file contains such code, the asset-import-meta-url plugin will rewrite the "test.wasm" part into an absolute path.

  • This works for JavaScript files in the repo
    (eg. the code from a JavaScript file in /src/a/b/c/x.js will be rewritten as new URL("/src/a/b/c/test.wasm", import.meta.url))
  • This does not work for JavaScript files from packages
    (eg. the code from a JavaScript file in /node_modules/test-wasm-library/index.js will be rewritten as new URL("/node_modules/.vite/deps/test.wasm", import.meta.url))

"/node_modules/.vite/deps/test.wasm" does not exist, so the page fails to load. The correct path should be "/node_modules/test-wasm-library/test.wasm"

@hi-ogawa
Copy link
Collaborator

Closing as a duplicate of #8427. You may try optimizeDeps.exclude as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants