From 11eb8c3489ae1b55dab159731e23fd8a094d8ac4 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 20 Feb 2023 04:21:23 +0800 Subject: [PATCH] fix(html): respect disable modulepreload (#12111) --- packages/vite/src/node/plugins/html.ts | 34 ++++++++++--------- playground/preload/index.html | 1 + playground/preload/src/chunk.js | 1 + playground/preload/src/main.js | 4 +++ .../preload/vite.config-preload-disabled.ts | 9 +++++ .../preload/vite.config-resolve-deps.ts | 9 +++++ playground/preload/vite.config.ts | 9 +++++ 7 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 playground/preload/src/chunk.js diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 4661fe5ea606bb..f865a27ac288b7 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -737,23 +737,25 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { toScriptTag(chunk, toOutputAssetFilePath, isAsync), ) } else { + assetTags = [toScriptTag(chunk, toOutputAssetFilePath, isAsync)] const { modulePreload } = config.build - const resolveDependencies = - typeof modulePreload === 'object' && - modulePreload.resolveDependencies - const importsFileNames = imports.map((chunk) => chunk.fileName) - const resolvedDeps = resolveDependencies - ? resolveDependencies(chunk.fileName, importsFileNames, { - hostId: relativeUrlPath, - hostType: 'html', - }) - : importsFileNames - assetTags = [ - toScriptTag(chunk, toOutputAssetFilePath, isAsync), - ...resolvedDeps.map((i) => - toPreloadTag(i, toOutputAssetFilePath), - ), - ] + if (modulePreload !== false) { + const resolveDependencies = + typeof modulePreload === 'object' && + modulePreload.resolveDependencies + const importsFileNames = imports.map((chunk) => chunk.fileName) + const resolvedDeps = resolveDependencies + ? resolveDependencies(chunk.fileName, importsFileNames, { + hostId: relativeUrlPath, + hostType: 'html', + }) + : importsFileNames + assetTags.push( + ...resolvedDeps.map((i) => + toPreloadTag(i, toOutputAssetFilePath), + ), + ) + } } assetTags.push(...getCssTagsForChunk(chunk, toOutputAssetFilePath)) diff --git a/playground/preload/index.html b/playground/preload/index.html index 59f13ff117eb5d..363b9993fe4d01 100644 --- a/playground/preload/index.html +++ b/playground/preload/index.html @@ -1,4 +1,5 @@

preload

+
diff --git a/playground/preload/src/chunk.js b/playground/preload/src/chunk.js new file mode 100644 index 00000000000000..b0ccae69cf3275 --- /dev/null +++ b/playground/preload/src/chunk.js @@ -0,0 +1 @@ +export default '[success] message from chunk.js' diff --git a/playground/preload/src/main.js b/playground/preload/src/main.js index d6c80df573ebff..d5004c02610090 100644 --- a/playground/preload/src/main.js +++ b/playground/preload/src/main.js @@ -1,3 +1,7 @@ +import chunkMsg from './chunk' + +document.querySelector('.chunk').textContent = chunkMsg + const ids = { hello: async () => { await import(/* a comment */ './hello.js') diff --git a/playground/preload/vite.config-preload-disabled.ts b/playground/preload/vite.config-preload-disabled.ts index 4cef9067fe93f2..4f1c6674165c2c 100644 --- a/playground/preload/vite.config-preload-disabled.ts +++ b/playground/preload/vite.config-preload-disabled.ts @@ -12,6 +12,15 @@ export default defineConfig({ passes: 3, }, }, + rollupOptions: { + output: { + manualChunks(id) { + if (id.includes('chunk.js')) { + return 'chunk' + } + }, + }, + }, modulePreload: false, }, }) diff --git a/playground/preload/vite.config-resolve-deps.ts b/playground/preload/vite.config-resolve-deps.ts index d9a859c2491b5e..6a60567be82d47 100644 --- a/playground/preload/vite.config-resolve-deps.ts +++ b/playground/preload/vite.config-resolve-deps.ts @@ -12,6 +12,15 @@ export default defineConfig({ passes: 3, }, }, + rollupOptions: { + output: { + manualChunks(id) { + if (id.includes('chunk.js')) { + return 'chunk' + } + }, + }, + }, modulePreload: { resolveDependencies(filename, deps, { hostId, hostType }) { if (filename.includes('hello')) { diff --git a/playground/preload/vite.config.ts b/playground/preload/vite.config.ts index c6aa914e71aadf..6ec8716860526a 100644 --- a/playground/preload/vite.config.ts +++ b/playground/preload/vite.config.ts @@ -12,5 +12,14 @@ export default defineConfig({ passes: 3, }, }, + rollupOptions: { + output: { + manualChunks(id) { + if (id.includes('chunk.js')) { + return 'chunk' + } + }, + }, + }, }, })