Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Failed to construct 'URL': Invalid URL (vite 3) #9543

Closed
7 tasks done
caoxiemeihao opened this issue Aug 5, 2022 · 1 comment 路 Fixed by #9381
Closed
7 tasks done

Uncaught TypeError: Failed to construct 'URL': Invalid URL (vite 3) #9543

caoxiemeihao opened this issue Aug 5, 2022 · 1 comment 路 Fixed by #9381

Comments

@caoxiemeihao
Copy link
Contributor

caoxiemeihao commented Aug 5, 2022

This Issue re-open form #9180
Related Issue electron-vite/electron-vite-vue#212

Describe the bug

I tried to Debug to find out the BUG, and finally I found that the BUG is caused by esbuild

馃憠 esbuild#2441 馃悶 import.meta.url transpile.

But this BUG only appears when I use base: './' and format: 'cjs'. The following options are often used with Electron-Renderer

export {
  base: './', // Here
  build: {
    minify: false,
    rollupOptions: {
      output: {
        format: 'cjs', // Here
      },
    },
  },
}

Reproduction

https://stackblitz.com/edit/vite-invalid-url?file=vite.config.js

System Info

System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
    Memory: 1010.00 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 103.0.5060.134
    Safari: 15.4
  npmPackages:
    vite: ^3.0.4 => 3.0.4

Used Package Manager

npm

Logs

No response

Validations

@caoxiemeihao
Copy link
Contributor Author

Finally I use config.experimental.renderBuiltUrl to avoid this Error.

exprot {
  experimental = {
    renderBuiltUrl(filename, type) {
      // https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts#L84-L124
      if (
        KNOWN_ASSET_TYPES.includes(path.extname(filename).slice(1)) &&
        type.hostType === 'js'
      ) {
        // Avoid Vite relative-path assets handling
        // https://github.com/vitejs/vite/blob/89dd31cfe228caee358f4032b31fdf943599c842/packages/vite/src/node/build.ts#L838-L875
        return { runtime: JSON.stringify(filename) }
      }
    },
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
1 participant