Skip to content

Commit 6c50119

Browse files
authored
fix(html): respect disable modulepreload (#12111)
1 parent 575bcf6 commit 6c50119

File tree

7 files changed

+51
-16
lines changed

7 files changed

+51
-16
lines changed

packages/vite/src/node/plugins/html.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -737,23 +737,25 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
737737
toScriptTag(chunk, toOutputAssetFilePath, isAsync),
738738
)
739739
} else {
740+
assetTags = [toScriptTag(chunk, toOutputAssetFilePath, isAsync)]
740741
const { modulePreload } = config.build
741-
const resolveDependencies =
742-
typeof modulePreload === 'object' &&
743-
modulePreload.resolveDependencies
744-
const importsFileNames = imports.map((chunk) => chunk.fileName)
745-
const resolvedDeps = resolveDependencies
746-
? resolveDependencies(chunk.fileName, importsFileNames, {
747-
hostId: relativeUrlPath,
748-
hostType: 'html',
749-
})
750-
: importsFileNames
751-
assetTags = [
752-
toScriptTag(chunk, toOutputAssetFilePath, isAsync),
753-
...resolvedDeps.map((i) =>
754-
toPreloadTag(i, toOutputAssetFilePath),
755-
),
756-
]
742+
if (modulePreload !== false) {
743+
const resolveDependencies =
744+
typeof modulePreload === 'object' &&
745+
modulePreload.resolveDependencies
746+
const importsFileNames = imports.map((chunk) => chunk.fileName)
747+
const resolvedDeps = resolveDependencies
748+
? resolveDependencies(chunk.fileName, importsFileNames, {
749+
hostId: relativeUrlPath,
750+
hostType: 'html',
751+
})
752+
: importsFileNames
753+
assetTags.push(
754+
...resolvedDeps.map((i) =>
755+
toPreloadTag(i, toOutputAssetFilePath),
756+
),
757+
)
758+
}
757759
}
758760
assetTags.push(...getCssTagsForChunk(chunk, toOutputAssetFilePath))
759761

playground/preload/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<h1>preload</h1>
2+
<div class="chunk"></div>
23
<div id="hello">
34
<button class="load">Load hello</button>
45
<div class="msg"></div>

playground/preload/src/chunk.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default '[success] message from chunk.js'

playground/preload/src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import chunkMsg from './chunk'
2+
3+
document.querySelector('.chunk').textContent = chunkMsg
4+
15
const ids = {
26
hello: async () => {
37
await import(/* a comment */ './hello.js')

playground/preload/vite.config-preload-disabled.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ export default defineConfig({
1212
passes: 3,
1313
},
1414
},
15+
rollupOptions: {
16+
output: {
17+
manualChunks(id) {
18+
if (id.includes('chunk.js')) {
19+
return 'chunk'
20+
}
21+
},
22+
},
23+
},
1524
modulePreload: false,
1625
},
1726
})

playground/preload/vite.config-resolve-deps.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ export default defineConfig({
1212
passes: 3,
1313
},
1414
},
15+
rollupOptions: {
16+
output: {
17+
manualChunks(id) {
18+
if (id.includes('chunk.js')) {
19+
return 'chunk'
20+
}
21+
},
22+
},
23+
},
1524
modulePreload: {
1625
resolveDependencies(filename, deps, { hostId, hostType }) {
1726
if (filename.includes('hello')) {

playground/preload/vite.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ export default defineConfig({
1212
passes: 3,
1313
},
1414
},
15+
rollupOptions: {
16+
output: {
17+
manualChunks(id) {
18+
if (id.includes('chunk.js')) {
19+
return 'chunk'
20+
}
21+
},
22+
},
23+
},
1524
},
1625
})

0 commit comments

Comments
 (0)