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

Dynamic import fails even if following all rules outlined in Rollup limitations doc #8424

Closed
7 tasks done
brunobely opened this issue Jun 1, 2022 · 1 comment
Closed
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@brunobely
Copy link

Describe the bug

I'm trying to dynamically import files with the format \./${import.meta.env.VITE_PROJECT_NAME}__site_config.json``, however I get a warning saying Vite cannot statically analyze the import. I'm following all the rules outlined in Rollup dynamic import limitations doc.

If I change the the static portion of the string to be at the beginning of the path, however, like \./site_config__${import.meta.env.VITE_PROJECT_NAME}.json``, it works normally. Is there a reason for this?

Reproduction

https://stackblitz.com/edit/vitejs-vite-ma2hgh?file=main.js

System Info

System:
    OS: macOS 12.3.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 979.52 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.0.1 - ~/.nvm/versions/node/v17.0.1/bin/node
    npm: 8.10.0 - ~/.nvm/versions/node/v17.0.1/bin/npm
  Browsers:
    Brave Browser: 99.1.36.117
    Chrome: 102.0.5005.61
    Chrome Canary: 104.0.5095.0
    Firefox: 100.0
    Safari: 15.4

Used Package Manager

pnpm

Logs

1  |  import { SiteConfig } from "$proto/ts/common/config/site_config";
2  |  console.log(`dynamically importing ${import.meta.env.VITE_PROJECT_NAME}__site_config.json`);
3  |  const configJson = (await import(`./${import.meta.env.VITE_PROJECT_NAME}__site_config.json`)).default;
   |                                   ^
4  |  console.log(configJson);
5  |  export default SiteConfig.fromJson(configJson);
The above dynamic import cannot be analyzed by vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.

Validations

@sapphi-red
Copy link
Member

Closing as it is fixed by #7756 which is released with Vite v3.0.0-alpha.0.

The reason was because this part was not accurate.

// must be more specific if importing from same dir
if (url.startsWith('./${') && url.indexOf('/') === url.lastIndexOf('/')) {
return false
}

@sapphi-red sapphi-red added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Jun 1, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

2 participants