Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html): respect disable modulepreload #12111

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
}
},
},
},
},
})