Skip to content

Conversation

luffy84217
Copy link

Description

When one imports an asset (let's say .svg) using import.meta.glob(..., { eager: true, import: 'default' }) new path will be inlined in a const and then this const get's tree-shaked/optimized, corresponding asset is not excluded from the final build.

It's expected that no unused assets, especially heavy ones like fonts and images, are not included in final build.

Fix: #19949
Ref: https://github.com/BerkliumBirb/vite-broken-tree-shaking

@sapphi-red
Copy link
Member

@sapphi-red Hi, since this is my very first PR to vite, if I'm lack of consideration, please give me advice. Since you changed the type from bug to feature, does it need test for it?
#19949 (comment)

@luffy84217 The tests are needed for both bugs and features.

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are failing.

Also the code doesn't look right. This would remove all assets that are not used from chunks and css files. But the asset might be emitted by a plugin that Vite itself is not aware of and Vite shouldn't remove those assets.
I guess we can use needsCodeReference if we can mark the asset as used. I'm not sure if we can mark the asset as used though.

@luffy84217
Copy link
Author

@sapphi-red Got it! I will check test and make it pass.
I will find external asset plugin and use needsCodeReference to see if it works

luffy84217 and others added 2 commits October 6, 2025 15:13
When one imports an asset (let's say .svg) using import.meta.glob(..., {
eager: true, import: 'default' }) new path will be inlined in a const
and then this const get's tree-shaked/optimized, corresponding asset is
not excluded from the final build.

It's expected that no unused assets, especially heavy ones like fonts
and images, are not included in final build.

Fix: vitejs#19949
Ref: https://github.com/BerkliumBirb/vite-broken-tree-shaking
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
@luffy84217 luffy84217 force-pushed the feat/remove-unused-assets-css-in-dist-issue-19949 branch from 1cc7b9e to ad6e714 Compare October 6, 2025 07:13
@luffy84217
Copy link
Author

luffy84217 commented Oct 6, 2025

@sapphi-red I used needsCodeReference to emit file and get reference Id, but it doesn't mark the asset as used as we thought.
The asset files no matter used or not in app code (tree-shaking works well) won't be added in bundle in generateBundle(this, bundle) so asset files will be lost in dist/assets finally.

It doesn't behave like what official says Now the asset will only be added to the bundle if the reference import.meta.ROLLUP_FILE_URL_referenceId is actually used in the code.

@sapphi-red
Copy link
Member

Vite doesn't use import.meta.ROLLUP_FILE_URL_referenceId, so unless you changed the code around it, I don't think Rollup will preserve the assets.

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

Successfully merging this pull request may close these issues.

Broken eagerly imported assets tree shaking
2 participants