From d47e10c77d642b0446cb6ea1d5fd5a33346e9391 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 8 Jan 2021 18:17:43 -0500 Subject: [PATCH] fix: handle potential imports that has no correspodning chunks during html generation --- packages/vite/src/node/plugins/html.ts | 41 ++++++++++++++++---------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 78a52ab2491b85..8151c97f8b9488 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -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 } @@ -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 }