From 84aac71a9f4fce91583150804362f9ca46b7f48c Mon Sep 17 00:00:00 2001 From: Alex Saft Date: Fri, 18 Mar 2022 17:53:29 +0300 Subject: [PATCH] Fix Vite build bundling error about EISDIR on `new URL('.', import.meta.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 https://github.com/polkadot-js/extension/issues/1018 --- packages/dev/scripts/polkadot-dev-build-ts.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dev/scripts/polkadot-dev-build-ts.mjs b/packages/dev/scripts/polkadot-dev-build-ts.mjs index 53ac1434..1b4256e9 100755 --- a/packages/dev/scripts/polkadot-dev-build-ts.mjs +++ b/packages/dev/scripts/polkadot-dev-build-ts.mjs @@ -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) => {