Skip to content

Commit

Permalink
Fix Vite build bundling error about EISDIR on `new URL('.', import.me…
Browse files Browse the repository at this point in the history
…ta.url)` (#637)

Hi!
Vite shows EISDIR error at `new URL('.', import.meta.url)` expression on build.
Actually checking import.meta.url is not enough to fix this problem.
The problem is when bundler tries to build the code in production build, in compile time it has no idea about how the expression `new URL('.', import.meta.url)` should be resolved in runtime. And it tries to read all the file and bake it inline in base64 form. And in case of `new URL('.', ...)` it tries to read the dir what is impossible and throws compile-time error EISDIR. 

 So I created a workaround code which gives the same output in Vite dev and Node.js environments:

```js
const filePathname = new URL(import.meta.url).pathname
const folderPathname = filePathname.substring(0, filePathname.lastIndexOf('/')+1)
```

actually does literally the same as 

```js
new URL('.', import.meta.url)
```

Also closes polkadot-js/extension#1018
  • Loading branch information
fend25 committed Mar 18, 2022
1 parent 63ea7ae commit 84aac71
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion packages/dev/scripts/polkadot-dev-build-ts.mjs
Expand Up @@ -147,7 +147,7 @@ function tweakPackageInfo (buildDir) {
// Hack around some bundler issues, in this case Vite which has import.meta.url
// as undefined in production contexts (and subsequently makes URL fail)
// See https://github.com/vitejs/vite/issues/5558
const esmDirname = "(import.meta && import.meta.url) ? new URL('.', import.meta.url).pathname : 'auto'";
const esmDirname = "(import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto'";
const cjsDirname = "typeof __dirname === 'string' ? __dirname : 'auto'";

['js', 'cjs'].forEach((ext) => {
Expand Down

0 comments on commit 84aac71

Please sign in to comment.