vite: `new URL(foo, import.meta.url)` doesn't work when dependency was optimized
Describe the bug
Now, i use wasm-pack for build WASM file and WASM with JavaScript glue layer like this。
The glue layer code contains below code
if (typeof input === 'undefined') {
165 | input = new URL('color_thief_wasm_bg.wasm', import.meta.url);
166 | }
In Vite, after esbuild optimize third party, import.meta.url cannot be optimize succeed.
Ref Vite Docs, even if set build.target to es2020 is still the problem
After preprocess
node_modules/color-thief-wasm-web/color_thief_wasm.js will be transform to node_modules/.vite/deps/color_thief_wasm.js which load .wasm file by import.meta.url receive incorrect path is node_modules/.vite/deps/xxx.wasm finally, the correct path is node_modules/color-thief-wasm-web/xxx.wasm
One of the solutions is set optimizeDeps.exclude to exclude third party module like this
Reproduction
https://stackblitz.com/edit/vite-rkrfzu?file=src%2FApp.vue,vite.config.js
System Info
vite@2.9.9
Used Package Manager
npm
Logs
Failed to construct 'URL': Invalid URL
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.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 33
- Comments: 19 (5 by maintainers)
Commits related to this issue
- Use worse wrapper for __filename and __dirname import.meta.url is not supported in prominent build tools yet, so we use a different approach: https://github.com/vitejs/vite/issues/8427 https://github... — committed to atjn/usable-gun by atjn 3 years ago
- don't optimize fsrs-browser https://github.com/vitejs/vite/issues/8427 — committed to AlexErrant/Pentive by AlexErrant 3 years ago
- justify optimizeDeps.exclude https://github.com/vitejs/vite/issues/8427 — committed to ishiko732/fsrs-browser by AlexErrant 3 years ago
- [js/web] use the recommended workaround for Vite (#23531) ### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in https://github.com/vit... — committed to microsoft/onnxruntime by fs-eire 2 years ago
- [js/web] use the recommended workaround for Vite (#23531) ### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in https://github.com/vit... — committed to microsoft/onnxruntime by fs-eire 2 years ago
- [js/web] use the recommended workaround for Vite (#23531) ### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in https://github.com/vit... — committed to microsoft/onnxruntime by fs-eire 2 years ago
- docs(parser-wasm): fix browser usage and add vite example (#9775) This was reported on discord https://discord.com/channels/1079625926024900739/1117322804291964931/1349844856855461968. There's one g... — committed to oxc-project/oxc by hi-ogawa 2 years ago
- [js/web] use the recommended workaround for Vite (#23531) ### Description After some investigation and debug, I decided to follow the recommended workaround as suggested in https://github.com/vit... — committed to microsoft/onnxruntime by fs-eire 2 years ago
Got a similar issue:
Results in
With the error
self is not defined.Leaving some notes about the current status of this issue.
import.meta.urlis https://github.com/evanw/esbuild/issues/1492optimizeDeps.excludeworkaround works when just startingvite. But not forvite buildunfortunately, the wasm file gets excluded. Was there another workaround for the build side?OK, adding
assetsInclude: ["**/argon2id/dist/*.wasm"]worked.Is this the same issue on esbuild?
In short:
import.meta.urlis not transformed so, when modules are bundled into a single location, the expected.urlhas changed.Input
Output
which prints
full/dist/index.jsinstead of/full/dist/foo/bar.jsAn ugly practice is move
wasm packageto current project rather thannode_modulesfor get correct path