From c47392bb7b60662ae68dd0e83c429ba6a837c29e Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 19 Sep 2024 10:50:19 +0900 Subject: [PATCH] fix: use `config.consumer` instead of `options?.ssr` / `config.build.ssr` --- packages/vite/src/node/plugins/css.ts | 6 +++--- packages/vite/src/node/plugins/importAnalysisBuild.ts | 11 +++++++---- .../vite/src/node/plugins/modulePreloadPolyfill.ts | 8 +++++--- packages/vite/src/node/plugins/preAlias.ts | 2 +- packages/vite/src/node/plugins/reporter.ts | 4 ++-- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 390804543ce9e3..7f177952966aba 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -452,7 +452,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { codeSplitEmitQueue = createSerialPromiseQueue() }, - async transform(css, id, options) { + async transform(css, id) { if ( !isCSSRequest(id) || commonjsProxyRE.test(id) || @@ -510,7 +510,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { return null } // server only - if (options?.ssr) { + if (this.environment.config.consumer !== 'client') { return modulesCode || `export default ${JSON.stringify(css)}` } if (inlined) { @@ -783,7 +783,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { }) generatedAssets.set(referenceId, { originalFileName, isEntry }) chunk.viteMetadata!.importedCss.add(this.getFileName(referenceId)) - } else if (!config.build.ssr) { + } else if (this.environment.config.consumer === 'client') { // legacy build and inline css // Entry chunk CSS will be collected into `chunk.viteMetadata.importedCss` diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 6da7bc23103356..d883e0c4cd222e 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -18,6 +18,7 @@ import type { Plugin } from '../plugin' import type { ResolvedConfig } from '../config' import { toOutputFilePathInJS } from '../build' import { genSourceMapUrl } from '../server/sourcemap' +import type { Environment } from '../environment' import { removedPureCssFilesCache } from './css' import { createParseErrorInfo } from './importAnalysis' @@ -169,9 +170,10 @@ function preload( * Build only. During serve this is performed as part of ./importAnalysis. */ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { - const ssr = !!config.build.ssr - const isWorker = config.isWorker - const insertPreload = !(ssr || !!config.build.lib || isWorker) + const getInsertPreload = (environment: Environment) => + environment.config.consumer === 'client' && + !config.isWorker && + !config.build.lib const renderBuiltUrl = config.experimental.renderBuiltUrl const isRelativeBase = config.base === './' || config.base === '' @@ -237,6 +239,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { return null } + const insertPreload = getInsertPreload(this.environment) // when wrapping dynamic imports with a preload helper, Rollup is unable to analyze the // accessed variables for treeshaking. This below tries to match common accessed syntax // to "copy" it over to the dynamic import wrapped by the preload helper. @@ -402,7 +405,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin { // If preload is not enabled, we parse through each imports and remove any imports to pure CSS chunks // as they are removed from the bundle - if (!insertPreload) { + if (!getInsertPreload(this.environment)) { const removedPureCssFiles = removedPureCssFilesCache.get(config) if (removedPureCssFiles && removedPureCssFiles.size > 0) { for (const file in bundle) { diff --git a/packages/vite/src/node/plugins/modulePreloadPolyfill.ts b/packages/vite/src/node/plugins/modulePreloadPolyfill.ts index 2a363e13f99882..8bf5e6abce4b28 100644 --- a/packages/vite/src/node/plugins/modulePreloadPolyfill.ts +++ b/packages/vite/src/node/plugins/modulePreloadPolyfill.ts @@ -6,8 +6,6 @@ export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill' const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js' export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin { - // `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis` - const skip = config.command !== 'build' || config.build.ssr let polyfillString: string | undefined return { @@ -19,7 +17,11 @@ export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin { }, load(id) { if (id === resolvedModulePreloadPolyfillId) { - if (skip) { + // `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis` + if ( + config.command !== 'build' || + this.environment.config.consumer !== 'client' + ) { return '' } if (!polyfillString) { diff --git a/packages/vite/src/node/plugins/preAlias.ts b/packages/vite/src/node/plugins/preAlias.ts index 693b70bdf1ed91..aa83bfe492a3a4 100644 --- a/packages/vite/src/node/plugins/preAlias.ts +++ b/packages/vite/src/node/plugins/preAlias.ts @@ -28,7 +28,7 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin { name: 'vite:pre-alias', async resolveId(id, importer, options) { const { environment } = this - const ssr = options?.ssr === true + const ssr = environment.config.consumer === 'server' const depsOptimizer = environment.mode === 'dev' ? environment.depsOptimizer : undefined if ( diff --git a/packages/vite/src/node/plugins/reporter.ts b/packages/vite/src/node/plugins/reporter.ts index c502e300674c30..1d6cef214ef1ec 100644 --- a/packages/vite/src/node/plugins/reporter.ts +++ b/packages/vite/src/node/plugins/reporter.ts @@ -93,7 +93,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { code: string | Uint8Array, ): Promise { if ( - environment.config.build.ssr || + environment.config.consumer !== 'client' || !environment.config.build.reportCompressedSize ) { return null @@ -255,7 +255,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin { hasLargeChunks && environment.config.build.minify && !config.build.lib && - !environment.config.build.ssr + environment.config.consumer === 'client' ) { environment.logger.warn( colors.yellow(