-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Comments
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. |
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! |
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 because i intend generate different dist img with different country. |
what if a function was provided to "register interest"? so to access meta you need to do something like
you could also do
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) |
switch (purl) {
case "posts": posts = Object.entries(import.meta.globEager("/posts/**/*.md"));
case "proj": posts = Object.entries(import.meta.globEager("/proj/**"));
} |
@mingyuyuyu .env variables can only be accessed if it starts with
@boehs I'm not sure how that would work, and if it's necessary either?
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 |
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 isimport.meta.glob() can only accept string literals.
Reproduction
System Info
Used Package Manager
pnpm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: