fix: modulePreload false (#13973)

This commit is contained in:
patak 2023-07-31 11:52:56 +02:00 committed by GitHub
parent 4ca7c13b73
commit 488085d631
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 21 deletions

View File

@ -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[]

View File

@ -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}`

View File

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

View File

@ -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({

View File

@ -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"/,
)
})
})