From 488085d63115014a243ed93608a1667c356d8749 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 31 Jul 2023 11:52:56 +0200 Subject: [PATCH] fix: modulePreload false (#13973) --- packages/vite/src/node/config.ts | 2 +- packages/vite/src/node/plugins/html.ts | 4 +-- .../src/node/plugins/importAnalysisBuild.ts | 25 ++++++++----------- packages/vite/src/node/plugins/index.ts | 3 +-- .../preload-disabled/preload-disabled.spec.ts | 5 +++- 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 19714b192f45bb..f73ae2ae703e1c 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -344,7 +344,7 @@ export interface InlineConfig extends UserConfig { export type ResolvedConfig = Readonly< Omit< UserConfig, - 'plugins' | 'css' | 'assetsInclude' | 'optimizeDeps' | 'worker' + 'plugins' | 'css' | 'assetsInclude' | 'optimizeDeps' | 'worker' | 'build' > & { configFile: string | undefined configFileDependencies: string[] diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 707345a55bfb21..4b00fd1c9c33ad 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -593,8 +593,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // inject module preload polyfill only when configured and needed const { modulePreload } = config.build if ( - (modulePreload === true || - (typeof modulePreload === 'object' && modulePreload.polyfill)) && + modulePreload !== false && + modulePreload.polyfill && (someScriptsAreAsync || someScriptsAreDefer) ) { js = `import "${modulePreloadPolyfillId}";\n${js}` diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index fe7b586ef7c7e7..a5b961a3bb3718 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -154,12 +154,7 @@ function preload( export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { const ssr = !!config.build.ssr const isWorker = config.isWorker - const insertPreload = !( - ssr || - !!config.build.lib || - isWorker || - config.build.modulePreload === false - ) + const insertPreload = !(ssr || !!config.build.lib || isWorker) const resolveModulePreloadDependencies = config.build.modulePreload && config.build.modulePreload.resolveDependencies @@ -448,12 +443,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { }, generateBundle({ format }, bundle) { - if ( - format !== 'es' || - ssr || - isWorker || - config.build.modulePreload === false - ) { + if (format !== 'es' || ssr || isWorker) { return } @@ -564,14 +554,19 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { deps.size > 1 || // main chunk is removed (hasRemovedPureCssChunk && deps.size > 0) - ? [...deps] + ? modulePreload === false + ? // CSS deps use the same mechanism as module preloads, so even if disabled, + // we still need to pass these deps to the preload helper in dynamic imports. + [...deps].filter((d) => d.endsWith('.css')) + : [...deps] : [] let renderedDeps: string[] if (normalizedFile && customModulePreloadPaths) { const { modulePreload } = config.build - const resolveDependencies = - modulePreload && modulePreload.resolveDependencies + const resolveDependencies = modulePreload + ? modulePreload.resolveDependencies + : undefined let resolvedDeps: string[] if (resolveDependencies) { // We can't let the user remove css deps as these aren't really preloads, they are just using diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 203dd64f47bbe3..45b9416363a9cb 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -54,8 +54,7 @@ export async function resolvePlugins( preAliasPlugin(config), aliasPlugin({ entries: config.resolve.alias }), ...prePlugins, - modulePreload === true || - (typeof modulePreload === 'object' && modulePreload.polyfill) + modulePreload !== false && modulePreload.polyfill ? modulePreloadPolyfillPlugin(config) : null, resolvePlugin({ diff --git a/playground/preload/__tests__/preload-disabled/preload-disabled.spec.ts b/playground/preload/__tests__/preload-disabled/preload-disabled.spec.ts index 638b8f241a0943..1ef462ca7c238b 100644 --- a/playground/preload/__tests__/preload-disabled/preload-disabled.spec.ts +++ b/playground/preload/__tests__/preload-disabled/preload-disabled.spec.ts @@ -19,6 +19,9 @@ describe.runIf(isBuild)('build', () => { const html = await page.content() expect(html).not.toMatch(/link rel="modulepreload"/) - expect(html).not.toMatch(/link rel="stylesheet"/) + + expect(html).toMatch( + /link rel="stylesheet".*?href=".*?\/assets\/hello-\w{8}\.css"/, + ) }) })