Skip to content

Commit

Permalink
fix: handle potential imports that has no correspodning chunks
Browse files Browse the repository at this point in the history
during html generation
  • Loading branch information
yyx990803 committed Jan 8, 2021
1 parent cca015b commit d47e10c
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -223,15 +223,19 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const getPreloadLinksForChunk = (
chunk: OutputChunk
): HtmlTagDescriptor[] => {
const tags: HtmlTagDescriptor[] = chunk.imports.map((file) => ({
tag: 'link',
attrs: {
rel: 'modulepreload',
href: toPublicPath(file, config)
}
}))
const tags: HtmlTagDescriptor[] = []
chunk.imports.forEach((file) => {
tags.push(...getPreloadLinksForChunk(bundle[file] as OutputChunk))
const importee = bundle[file]
if (importee && importee.type === 'chunk') {
tags.push({
tag: 'link',
attrs: {
rel: 'modulepreload',
href: toPublicPath(file, config)
}
})
tags.push(...getPreloadLinksForChunk(importee))
}
})
return tags
}
Expand All @@ -241,16 +245,21 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const cssFileHandle = chunkToEmittedCssFileMap.get(chunk)
if (cssFileHandle) {
const file = this.getFileName(cssFileHandle)
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: toPublicPath(file, config)
}
})
if (file) {
tags.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: toPublicPath(file, config)
}
})
}
}
chunk.imports.forEach((file) => {
tags.push(...getCssTagsForChunk(bundle[file] as OutputChunk))
const importee = bundle[file]
if (importee && importee.type === 'chunk') {
tags.push(...getCssTagsForChunk(importee))
}
})
return tags
}
Expand Down

0 comments on commit d47e10c

Please sign in to comment.