Skip to content

Commit 488085d

Browse files
authoredJul 31, 2023
fix: modulePreload false (#13973)
1 parent 4ca7c13 commit 488085d

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed
 

‎packages/vite/src/node/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ export interface InlineConfig extends UserConfig {
344344
export type ResolvedConfig = Readonly<
345345
Omit<
346346
UserConfig,
347-
'plugins' | 'css' | 'assetsInclude' | 'optimizeDeps' | 'worker'
347+
'plugins' | 'css' | 'assetsInclude' | 'optimizeDeps' | 'worker' | 'build'
348348
> & {
349349
configFile: string | undefined
350350
configFileDependencies: string[]

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
593593
// inject module preload polyfill only when configured and needed
594594
const { modulePreload } = config.build
595595
if (
596-
(modulePreload === true ||
597-
(typeof modulePreload === 'object' && modulePreload.polyfill)) &&
596+
modulePreload !== false &&
597+
modulePreload.polyfill &&
598598
(someScriptsAreAsync || someScriptsAreDefer)
599599
) {
600600
js = `import "${modulePreloadPolyfillId}";\n${js}`

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+10-15
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ function preload(
154154
export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
155155
const ssr = !!config.build.ssr
156156
const isWorker = config.isWorker
157-
const insertPreload = !(
158-
ssr ||
159-
!!config.build.lib ||
160-
isWorker ||
161-
config.build.modulePreload === false
162-
)
157+
const insertPreload = !(ssr || !!config.build.lib || isWorker)
163158

164159
const resolveModulePreloadDependencies =
165160
config.build.modulePreload && config.build.modulePreload.resolveDependencies
@@ -448,12 +443,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
448443
},
449444

450445
generateBundle({ format }, bundle) {
451-
if (
452-
format !== 'es' ||
453-
ssr ||
454-
isWorker ||
455-
config.build.modulePreload === false
456-
) {
446+
if (format !== 'es' || ssr || isWorker) {
457447
return
458448
}
459449

@@ -564,14 +554,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
564554
deps.size > 1 ||
565555
// main chunk is removed
566556
(hasRemovedPureCssChunk && deps.size > 0)
567-
? [...deps]
557+
? modulePreload === false
558+
? // CSS deps use the same mechanism as module preloads, so even if disabled,
559+
// we still need to pass these deps to the preload helper in dynamic imports.
560+
[...deps].filter((d) => d.endsWith('.css'))
561+
: [...deps]
568562
: []
569563

570564
let renderedDeps: string[]
571565
if (normalizedFile && customModulePreloadPaths) {
572566
const { modulePreload } = config.build
573-
const resolveDependencies =
574-
modulePreload && modulePreload.resolveDependencies
567+
const resolveDependencies = modulePreload
568+
? modulePreload.resolveDependencies
569+
: undefined
575570
let resolvedDeps: string[]
576571
if (resolveDependencies) {
577572
// We can't let the user remove css deps as these aren't really preloads, they are just using

‎packages/vite/src/node/plugins/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ export async function resolvePlugins(
5454
preAliasPlugin(config),
5555
aliasPlugin({ entries: config.resolve.alias }),
5656
...prePlugins,
57-
modulePreload === true ||
58-
(typeof modulePreload === 'object' && modulePreload.polyfill)
57+
modulePreload !== false && modulePreload.polyfill
5958
? modulePreloadPolyfillPlugin(config)
6059
: null,
6160
resolvePlugin({

‎playground/preload/__tests__/preload-disabled/preload-disabled.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ describe.runIf(isBuild)('build', () => {
1919

2020
const html = await page.content()
2121
expect(html).not.toMatch(/link rel="modulepreload"/)
22-
expect(html).not.toMatch(/link rel="stylesheet"/)
22+
23+
expect(html).toMatch(
24+
/link rel="stylesheet".*?href=".*?\/assets\/hello-\w{8}\.css"/,
25+
)
2326
})
2427
})

0 commit comments

Comments
 (0)