Skip to content

Commit

Permalink
fix(html): respect disable modulepreload (#12111)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Feb 19, 2023
1 parent 575bcf6 commit 6c50119
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
1 change: 1 addition & 0 deletions playground/preload/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<h1>preload</h1>
<div class="chunk"></div>
<div id="hello">
<button class="load">Load hello</button>
<div class="msg"></div>
Expand Down
1 change: 1 addition & 0 deletions playground/preload/src/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '[success] message from chunk.js'
4 changes: 4 additions & 0 deletions playground/preload/src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import chunkMsg from './chunk'

document.querySelector('.chunk').textContent = chunkMsg

const ids = {
hello: async () => {
await import(/* a comment */ './hello.js')
Expand Down
9 changes: 9 additions & 0 deletions playground/preload/vite.config-preload-disabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export default defineConfig({
passes: 3,
},
},
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('chunk.js')) {
return 'chunk'
}
},
},
},
modulePreload: false,
},
})
9 changes: 9 additions & 0 deletions playground/preload/vite.config-resolve-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
9 changes: 9 additions & 0 deletions playground/preload/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ export default defineConfig({
passes: 3,
},
},
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('chunk.js')) {
return 'chunk'
}
},
},
},
},
})

0 comments on commit 6c50119

Please sign in to comment.