Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day committed Jun 25, 2023
1 parent 29f6fd3 commit 675c8a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

async generateBundle(options, bundle) {
const analyzedChunk: Map<OutputChunk, number> = new Map()
const inlineEntryChunk: Map<string, number> = new Map()
const getImportedChunks = (
chunk: OutputChunk,
seen: Set<string> = new Set(),
Expand Down Expand Up @@ -827,7 +828,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

if (chunk && canInlineEntry) {
// all imports from entry have been inlined to html, prevent rollup from outputting it
delete bundle[chunk.fileName]
const importNum = inlineEntryChunk.get(chunk.fileName) || 0
inlineEntryChunk.set(chunk.fileName, importNum + 1)
}

const shortEmitName = normalizePath(path.relative(config.root, id))
Expand All @@ -837,6 +839,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
source: result,
})
}

for (const [fileName, importNum] of inlineEntryChunk) {
// #13436, only remove the independent inline entry chunk
if (importNum === 1) {
delete bundle[fileName]
}
}
},
}
}
Expand Down
6 changes: 5 additions & 1 deletion playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe.runIf(isBuild)('build', () => {
const countPreloadTags = _countTags.bind(this, 'link[rel=modulepreload]')

test('is inlined', async () => {
await page.goto(viteTestUrl + '/inline/shared-1.html?v=1')
await page.goto(viteTestUrl + '/inline/shared-2.html?v=1')
expect(await countScriptTags()).toBeGreaterThan(1)
expect(await countPreloadTags()).toBe(0)
})
Expand All @@ -162,6 +162,10 @@ describe.runIf(isBuild)('build', () => {
})

test('execution order when inlined', async () => {
await page.goto(viteTestUrl + '/inline/shared-1.html?v=1')
expect((await page.textContent('#output')).trim()).toBe(
'dep1 common dep2 dep3 shared',
)
await page.goto(viteTestUrl + '/inline/shared-2.html?v=1')
expect((await page.textContent('#output')).trim()).toBe(
'dep1 common dep2 dep3 shared',
Expand Down

0 comments on commit 675c8a1

Please sign in to comment.