webpack: webpack tries to resolve `new URL('./', import.meta.url)` to a module and fails or succeeds (both are wrong)
Bug report
What is the current behavior?
Webpack’s resolution of new URL(..., import.meta.url) does something unexpected when the target is a directory, which doesn’t correspond to what either the browser or Node will do. For comparison, try this in $BROWSER_OF_YOUR_CHOICE:
<pre id="output">
</pre>
<script type="module">
const pre = document.getElementById("output");
pre.innerHTML += new URL("./", import.meta.url).href;
</script>
or in Node, create index.mjs and run it:
console.log(new URL("./", import.meta.url).href);
In both cases you should see something like:
file:///home/dhd/work/webpack-wasm-test/importmeta/
Webpack does … something else. See below.
If the current behavior is a bug, please provide the steps to reproduce.
Given the index.mjs file above, run:
webpack ./index.mjs
It will fail with an error like this:
ERROR in ./index.mjs 1:12-42
Module not found: Error: Can't resolve './' in '/home/dhd/work/importmeta'
resolve './' in '/home/dhd/work/importmeta'
Parsed request is a directory
No description file found in /home/dhd/work/importmeta or above
No description file found in /home/dhd/work or above
as directory
existing directory /home/dhd/work/importmeta
No description file found in /home/dhd/work/importmeta or above
using path: /home/dhd/work/importmeta/index
No description file found in /home/dhd/work/importmeta or above
no extension
/home/dhd/work/importmeta/index doesn't exist
(if there is a package.json somewhere in a parent directory it will output some other stuff about that, but it will still fail in the same way)
I can make it “work” if I initialize the current directory as a module:
npm init
npm install -D webpack-cli webpack
touch index.js
npx webpack ./index.mjs
This is not what I want, because it will resolve the new URL above to the module in the current directory, then bundle whatever it can find in that module. All I wanted was a URL that pointed to the current directory! (and a Pepsi)
See the example here: https://github.com/dhdaines/webpack-wasm-test/tree/main/importmeta
What is the expected behavior?
Webpack should just pass new URL('./', import.meta.url) through unchanged. It should not try to resolve a directory as a module.
Ideally it should also pass through unchanged any other instance of new URL(..., import.meta.url) where the target can’t be resolved as an asset, since the user might create them after the fact.
Other relevant information: webpack version: ^5.76.3 => 5.76.3 Node.js version: 14.18.2 Operating System: Linux 5.19 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 5
- Comments: 23 (11 by maintainers)
Just replace
new URL('./', import.meta.url)withnew URL('./', Object(import.meta).url)if you don’t want webpack to touch it.Sure! Thanks for the pointer to the relevant code, it would have taken me a lot longer to find that 😉 I’ll make a PR…
@hai-x We already have it https://webpack.js.org/configuration/module/#moduleparserjavascripturl 😄 I just think we should not handle
new URL('.', import.meta.url)andnew URL('./', import.meta.url), mostly they used to resolve current URL, even we will implement bundling JS files usingnew URL(...)(technically it is possible, but in this case it will be necessary to generate additional runtime), I still think it is very rare and by default we should not doing it, maybe later when we will implement double bundling like I describe above (and under the option)Oh! Indeed, that ought to work, thanks!
Thanks! I haven’t had the time to look at this in the last couple weeks, but should have some today, I’ll let you know!