fix(html): respect disable modulepreload (#12111)

This commit is contained in:
Bjorn Lu 2023-02-20 04:21:23 +08:00 committed by GitHub
parent 575bcf61c5
commit 6c5011947e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 16 deletions

View File

@ -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))

View File

@ -1,4 +1,5 @@
<h1>preload</h1>
<div class="chunk"></div>
<div id="hello">
<button class="load">Load hello</button>
<div class="msg"></div>

View File

@ -0,0 +1 @@
export default '[success] message from chunk.js'

View File

@ -1,3 +1,7 @@
import chunkMsg from './chunk'
document.querySelector('.chunk').textContent = chunkMsg
const ids = {
hello: async () => {
await import(/* a comment */ './hello.js')

View File

@ -12,6 +12,15 @@ export default defineConfig({
passes: 3,
},
},
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('chunk.js')) {
return 'chunk'
}
},
},
},
modulePreload: false,
},
})

View File

@ -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')) {

View File

@ -12,5 +12,14 @@ export default defineConfig({
passes: 3,
},
},
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('chunk.js')) {
return 'chunk'
}
},
},
},
},
})