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

import.meta.glob() does not work with variables #5478

Closed
7 tasks done
boehs opened this issue Oct 29, 2021 · 7 comments · Fixed by #5491
Closed
7 tasks done

import.meta.glob() does not work with variables #5478

boehs opened this issue Oct 29, 2021 · 7 comments · Fixed by #5491
Labels

Comments

@boehs
Copy link
Contributor

boehs commented Oct 29, 2021

Describe the bug

I'm trying to use import.meta.glob to read a folder. It works if I use a string literal. But I have been unable to make it work with a dynamic folder name. The error I get is import.meta.glob() can only accept string literals.

Reproduction

    let url = "/posts/**/*.md"
    posts = import.meta.globEager(url)

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 AMD Ryzen 3 5300U with Radeon Graphics
    Memory: 1005.28 MB / 7.37 GB
  Binaries:
    Node: 16.8.0 - C:\Program Files\nodejs\node.EXE
    npm: 7.21.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (95.0.1020.30)
  npmPackages:
    vite: ^2.6.10 => 2.6.10

Used Package Manager

pnpm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Oct 29, 2021

This is working as intended. Glob imports can't use variables as they are not statically analyzable. I can't find this information in the docs though so maybe we could add them.

@github-actions
Copy link

Hello @boehs. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!

@mingyuyuyu
Copy link

mingyuyuyu commented Oct 29, 2021

oh, its issue for me too,i want to build necessary chunks for dist through variable in .env or defines in vite.config.js , but it does not work for import.glob, its so bad. i want to help.

example:

.env
LANG=chinese
but it does not work for below:
import.meta.glob(./img/${import.meta.env.LANG}/.aa.png);

because i intend generate different dist img with different country.

@boehs
Copy link
Contributor Author

boehs commented Oct 29, 2021

This is working as intended. Glob imports can't use variables as they are not statically analyzable. I can't find this information in the docs though so maybe we could add them.

what if a function was provided to "register interest"? so to access meta you need to do something like

meta.import.register("path")

you could also do

meta.import.register(["path","path","path"])

or do multiple calls to register. This makes it statically analyzable, and gives an immense amount of flexibility in contrast to the current system.


maybe (can constants be analyzed?) register can take a variable that is a const to make config easy too with some restrictions (const must be defined as a raw string or array)

@boehs
Copy link
Contributor Author

boehs commented Oct 30, 2021

    switch (purl) {
      case "posts": posts = Object.entries(import.meta.globEager("/posts/**/*.md"));
      case "proj": posts = Object.entries(import.meta.globEager("/proj/**"));
    }

@bluwy
Copy link
Member

bluwy commented Oct 31, 2021

oh, its issue for me too,i want to build necessary chunks for dist through variable in .env or defines in vite.config.js , but it does not work for import.glob, its so bad. i want to help.

@mingyuyuyu .env variables can only be accessed if it starts with VITE_, e.g. VITE_LANG. Unless you configure envPrefix. The import.meta.glob code is correct.

what if a function was provided to "register interest"? so to access meta you need to do something like

@boehs I'm not sure how that would work, and if it's necessary either?

maybe (can constants be analyzed?) register can take a variable that is a const to make config easy too with some restrictions (const must be defined as a raw string or array)

Yes, it would make things statically analyzable, but it would require an AST parse, which would be quite expensive for a module dev server and probably not worth the effort. A direct string identifier would be easier to handle with some string manipulation.

The code you posted looks like the right way forward. Was that the working solution?

@boehs
Copy link
Contributor Author

boehs commented Oct 31, 2021

oh, its issue for me too,i want to build necessary chunks for dist through variable in .env or defines in vite.config.js , but it does not work for import.glob, its so bad. i want to help.

@mingyuyuyu .env variables can only be accessed if it starts with VITE_, e.g. VITE_LANG. Unless you configure envPrefix. The import.meta.glob code is correct.

what if a function was provided to "register interest"? so to access meta you need to do something like

@boehs I'm not sure how that would work, and if it's necessary either?

maybe (can constants be analyzed?) register can take a variable that is a const to make config easy too with some restrictions (const must be defined as a raw string or array)

Yes, it would make things statically analyzable, but it would require an AST parse, which would be quite expensive for a module dev server and probably not worth the effort. A direct string identifier would be easier to handle with some string manipulation.

The code you posted looks like the right way forward. Was that the working solution?

yes, and it works with a config file, so that's cool, it could certainly be more elegant but for now it's fine. will leave this issue open because it could be noted in documentation, I might do that shortly

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

Successfully merging a pull request may close this issue.

4 participants