-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Invalid module URLs leading to HTTP 404/504 produced when using new URL(…, import.meta.url)
inside 3rd party module
#10839
Comments
new URL(…, import.meta.url)
inside 3rd party modulenew URL(…, import.meta.url)
inside 3rd party module
(minimized)I don't think that resolving modules via bare import specifiers is a good idea. It's incompatible with the spec and for that reason I also think #7837 is a problem:// file: /home/project/main.js
new URL('utils/index.js', import.meta.url) This should resolve to In first party code that might be acceptable, but if the transform is applied to modules, it can break 3rd party code that is valid JS. |
(minimized)Potential options I can think of:
new URL('npm:utils/index.js', import.meta.url)
new URL('utils/index.js')
And if a module needs to resolve an absolute URL/path to a file within the same module, it can always use a valid URL (no bare imports, file extension), no transforms needed. const runtimeUrl = new URL('./my-worker.js', import.meta.url) |
Whether or not it's spec compliant is another (important, but still another) problem. The main issue here is that the same piece of code produces different outputs, depending on file location (inside or outside node_modules). |
(minimized)Hmm, maybe I'm off here, but from my understanding Vite expects dependencies to be standard, runnable JavaScript and doesn't do non-standard transforms on dep code (see also: JSX) – different output of code outside and inside `node_modules` is expected. So to me this sounds like a feature request and not a bug report.Extending the #7837 feature in its current form to dependency code would not only allow dependencies to ship code that is bundler-specific and works in no runtime (okay, maybe?), but is non-deterministic and can lead to breakage (not okay imo). However, I do think that there's a case for |
Sorry, didn't notice that most of this stuff already works and it's just pre-bundling that breaks things! Minimized the respective comments :) I think Not sure if But |
You can't use Vite specific features within third-party packages. Vite will ignore them which causes the invalid URLs. If you want to process them by Vite, you need to add the package to |
They aren't though! While Vite-specific imports and module resolution for |
Vite does break it during prebundling because the library would be in a single file, and relative paths that were in |
Just encountered this myself - have library that uses |
Yeah, rewriting the path was what I was thinking about. You need to change the URL base to the package directory in // node_modules/some-package/workers/init.js
let conf = new URL('./config.txt', import.meta.url) ↓ // after pre-bundling
let conf = new URL('./config.txt', '@fs/home/path/to/node_modules/some-package/workers/') |
Is there a workaround I could use to solve this problem? |
Using Relative paths break ( |
Thanks, after your comment and doing some digging, I came accross this esbuild plugin: @chialab/esbuild-plugin-meta-url |
Describe the bug
Getting module URL in our own code like so:
works fine, but if that very same line is placed in
node_modules/module-1/index.js
, it results in an invalid URL being generated, requesting which results in 404 Not found or 504 Gateway timeout error.Interestingly, "module-2", "module-2/index.js" and "module-2/other.js" work, but "module-2/index" and "module-2/other" do not.
"module-2/index" and "module-2/other" also produce invalid URLs in our own code, but that's a different story.
Reproduction
https://github.com/wojtekmaj/vite-missing-deps-bug
Steps to reproduce
yarn
yarn copy-packages
(to replace symlinks in node_modules with actual modules, since they come from internal workspace)yarn dev
System Info
System: OS: macOS 13.0 CPU: (8) arm64 Apple M1 Pro Memory: 457.75 MB / 16.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 18.12.0 - /usr/local/bin/node Yarn: 3.2.4 - /usr/local/bin/yarn npm: 8.19.2 - /usr/local/bin/npm Browsers: Chrome: 107.0.5304.87 Firefox: 106.0.3 Safari: 16.1 npmPackages: vite: ^3.2.3 => 3.2.3
Used Package Manager
yarn
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: