Skip to content

Commit 2fa1495

Browse files
authored
fix: keep entry asset files imported by other files (#19779)
1 parent 62d7e81 commit 2fa1495

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

packages/vite/src/node/plugins/asset.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
225225

226226
generateBundle(_, bundle) {
227227
// Remove empty entry point file
228+
let importedFiles: Set<string> | undefined
228229
for (const file in bundle) {
229230
const chunk = bundle[file]
230231
if (
@@ -234,7 +235,23 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
234235
config.assetsInclude(chunk.moduleIds[0]) &&
235236
this.getModuleInfo(chunk.moduleIds[0])?.meta['vite:asset']
236237
) {
237-
delete bundle[file]
238+
if (!importedFiles) {
239+
importedFiles = new Set()
240+
for (const file in bundle) {
241+
const chunk = bundle[file]
242+
if (chunk.type === 'chunk') {
243+
for (const importedFile of chunk.imports) {
244+
importedFiles.add(importedFile)
245+
}
246+
for (const importedFile of chunk.dynamicImports) {
247+
importedFiles.add(importedFile)
248+
}
249+
}
250+
}
251+
}
252+
if (!importedFiles.has(file)) {
253+
delete bundle[file]
254+
}
238255
}
239256
}
240257

playground/backend-integration/__tests__/backend-integration.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe.runIf(isBuild)('build', () => {
5656
const imgAssetEntry = manifest['../images/logo.png']
5757
const dirFooAssetEntry = manifest['../../dir/foo.css']
5858
const iconEntrypointEntry = manifest['icon.png']
59+
const waterContainerEntry = manifest['water-container.svg']
5960
expect(htmlEntry.css.length).toEqual(1)
6061
expect(htmlEntry.assets.length).toEqual(1)
6162
expect(mainTsEntry.assets?.length ?? 0).toBeGreaterThanOrEqual(1)
@@ -75,6 +76,7 @@ describe.runIf(isBuild)('build', () => {
7576
// use the entry name
7677
expect(dirFooAssetEntry.file).toMatch('assets/bar-')
7778
expect(iconEntrypointEntry?.file).not.toBeUndefined()
79+
expect(waterContainerEntry?.file).not.toBeUndefined()
7880
})
7981

8082
test('CSS imported from JS entry should have a non-nested chunk name', () => {

playground/backend-integration/frontend/entrypoints/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import 'vite/modulepreload-polyfill'
22
import cssUrl from '../styles/url.css?url'
3+
import waterContainer from './water-container.svg'
34

45
const cssLink = document.createElement('link')
56
cssLink.rel = 'stylesheet'
67
cssLink.href = cssUrl
78
document.querySelector('head').prepend(cssLink)
89

10+
const dummyMeta = document.createElement('meta')
11+
dummyMeta.name = 'dummy'
12+
dummyMeta.content = waterContainer
13+
document.querySelector('head').append(dummyMeta)
14+
915
export const colorClass = 'text-black'
1016

1117
export function colorHeading() {
Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)