Skip to content

Commit

Permalink
fix: use config.consumer instead of options?.ssr / `config.build.…
Browse files Browse the repository at this point in the history
…ssr` (#18140)
  • Loading branch information
sapphi-red authored Sep 19, 2024
1 parent 7722c06 commit 21ec1ce
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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`
Expand Down
11 changes: 7 additions & 4 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 === ''
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/modulePreloadPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/preAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function buildReporterPlugin(config: ResolvedConfig): Plugin {
code: string | Uint8Array,
): Promise<number | null> {
if (
environment.config.build.ssr ||
environment.config.consumer !== 'client' ||
!environment.config.build.reportCompressedSize
) {
return null
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 21ec1ce

Please sign in to comment.