Optimize bundle for Object.keys(import.meta.glob())
and Object.values(import.meta.glob())
#18662
Open
4 tasks done
Labels
Description
If I only care about the file names of all the files in a folder in the source directory of the project, and I don't care about the contents of those files. I can use
Object.keys(import.meta.glob())
to get it. However, this will also bundle the contents of these files, resulting in a larger distribution file size.Or maybe I only care about the contents of all the files in a folder in the source directory of the project, and I don't care about the directories and file names of those files. I can use
Object.values(import.meta.glob())
to get it. Although this file size is a bit less expensive than the former, it can also solve the problem together.Suggested solution
If I declare this:
It may get this result:
And also copy three unnecessary png files to the dist directory, which I don't really need them.
Those assets have been optimized and bundled by other vite plugins, so I don't need to copy them, I just need their file names.
It can be completely simplified to:
In the same way, if I only need their file contents:
It can also be compile to:
Alternative
If just for the former, I can also use a special query, like "?nothing". Make it become:
Which compile to:
This temporarily alleviates the problem of distribution file size. Although this is not the optimal solution.
Additional context
By searching for
Object.keys(import.meta.glob
andObject.values(import.meta.glob
on GitHub, you can see that there is already a lot of code that uses them. So if optimize this macro, it can make quite a few distribution files smaller.Validations
The text was updated successfully, but these errors were encountered: