Description
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)