Skip to content

Commit

Permalink
fix: modulePreload false (#13973)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jul 31, 2023
1 parent 4ca7c13 commit 488085d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
25 changes: 10 additions & 15 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"/,
)
})
})

0 comments on commit 488085d

Please sign in to comment.