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: modulePreload false #13973

Merged
merged 2 commits into from
Jul 31, 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
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 @@ -446,12 +441,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 @@ -562,14 +552,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"/,
)
})
})