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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asset-import-meta-url cant resolve non existing paths #4308

Closed
6 tasks done
brandonryan opened this issue Jul 18, 2021 · 1 comment · Fixed by #4732 or #7306
Closed
6 tasks done

asset-import-meta-url cant resolve non existing paths #4308

brandonryan opened this issue Jul 18, 2021 · 1 comment · Fixed by #4732 or #7306

Comments

@brandonryan
Copy link

Describe the bug

When using import.meta.url with URL and an invalid path, the asset-import-meta-url plugin tries to open the file and errors out. This also happens when the line of code is commented out.

//no validation should be done to find out if this is actually a file or not.
const url = new URL('some/non/existant/path.js', import.meta.url)

// This also gets transformed and errors even though its commented out
// new URL('some/non/existant/path.js', import.meta.url)

console.log(url)

Reproduction

Clone this then do yarn & yarn run build (or npm equiv)
https://gitlab.com/brandonryan/example/-/tree/main/bugs/vite-asset-import-meta-url-bug

System Info

System:
    OS: macOS 11.4
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 65.06 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.4.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.18.1 - /usr/local/bin/npm
  Browsers:
    Chrome: 91.0.4472.114
    Firefox: 89.0.2
    Safari: 14.1.1
  npmPackages:
    vite: ^2.4.2 => 2.4.2

Used Package Manager

yarn

Logs

vite v2.4.2 building for production...
✓ 0 modules transformed.
[asset-import-meta-url] ENOENT: no such file or directory, open '/Users/smidget/Development/example/bugs/vite-asset-import-meta-url-bug/src/some/non/existant/path.js'
file: /Users/smidget/Development/example/bugs/vite-asset-import-meta-url-bug/src/index.js
error during build:
Error: ENOENT: no such file or directory, open '/Users/smidget/Development/example/bugs/vite-asset-import-meta-url-bug/src/some/non/existant/path.js'

Validations

@brandonryan
Copy link
Author

brandonryan commented Aug 30, 2021

I think this issue needs to be re-opened. The pr does not fully addresses the problem.
It shouldn't try to resolve the file on disk at all. There are going to be many times when files don't exist at build time that exist at runtime.

For example, in my electron app I have this code for the main process:

export const electronURL = isDevelopment ? 
    process.env['VITE_DEV_SERVER_URL'] : 
    new URL('../../build/renderer/index.html', import.meta.url).toString()

This will always fail file lookup when in dev, because I am using the VITE_DEV_SERVER_URL

@patak-dev patak-dev reopened this Aug 30, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants