Skip to content

Commit a576326

Browse files
authored
fix: make isBundled per environment (#22257)
1 parent 8c766a6 commit a576326

19 files changed

Lines changed: 231 additions & 167 deletions

packages/vite/src/node/__tests__/plugins/define.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ async function createDefinePluginTransform(
2727
)
2828
return result?.code || result
2929
} else {
30+
const nativeDefinePlugin = await (
31+
definePlugin(config) as any
32+
).applyToEnvironment(environment)
3033
const bundler = await rolldown({
3134
input: 'entry.js',
3235
plugins: [
@@ -45,9 +48,7 @@ async function createDefinePluginTransform(
4548
},
4649
{
4750
name: 'native:define',
48-
options: (definePlugin(config).options! as any).bind({
49-
environment,
50-
}),
51+
options: nativeDefinePlugin.options.bind({ environment }),
5152
},
5253
],
5354
experimental: {

packages/vite/src/node/build.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,18 +512,24 @@ export function resolveBuildPlugins(config: ResolvedConfig): {
512512
...(isBuild && !config.isWorker ? [prepareOutDirPlugin()] : []),
513513
perEnvironmentPlugin(
514514
'vite:rollup-options-plugins',
515-
async (environment) =>
516-
(
515+
async (environment) => {
516+
if (!isBuild && !environment.config.isBundled) {
517+
return false
518+
}
519+
return (
517520
await asyncFlatten(
518521
arraify(environment.config.build.rollupOptions.plugins),
519522
)
520-
).filter(Boolean) as Plugin[],
523+
).filter(Boolean) as Plugin[]
524+
},
521525
),
522526
...(config.isWorker ? [webWorkerPostPlugin(config)] : []),
523527
],
524528
post: [
525529
...(isBuild ? buildImportAnalysisPlugin(config) : []),
526-
...(config.build.minify === 'esbuild' ? [buildEsbuildPlugin()] : []),
530+
...(isBuild && config.build.minify === 'esbuild'
531+
? [buildEsbuildPlugin()]
532+
: []),
527533
...(isBuild ? [terserPlugin(config)] : []),
528534
...(isBuild && !config.isWorker
529535
? [

packages/vite/src/node/config.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ export interface SharedEnvironmentOptions {
303303
* Optimize deps config
304304
*/
305305
optimizeDeps?: DepOptimizationOptions
306+
/**
307+
* Whether this environment produces a bundled output.
308+
*
309+
* During `build`, this defaults to `true` for every environment.
310+
* During `serve`, this defaults to `true` only for the client environment
311+
* when `experimental.bundledDev` is enabled, and `false` otherwise.
312+
* Setting this explicitly on an environment always overrides the default.
313+
*
314+
* @experimental
315+
*/
316+
isBundled?: boolean
306317
}
307318

308319
export interface EnvironmentOptions extends SharedEnvironmentOptions {
@@ -326,6 +337,7 @@ export type ResolvedEnvironmentOptions = {
326337
optimizeDeps: DepOptimizationOptions
327338
dev: ResolvedDevEnvironmentOptions
328339
build: ResolvedBuildEnvironmentOptions
340+
isBundled: boolean
329341
plugins: readonly Plugin[]
330342
/** @internal */
331343
optimizeDepsPluginNames: string[]
@@ -568,7 +580,11 @@ export interface ExperimentalOptions {
568580
*/
569581
hmrPartialAccept?: boolean
570582
/**
571-
* Enable full bundle mode.
583+
* Enable full bundle mode during `serve`.
584+
*
585+
* This seeds the default for the client environment's `isBundled` option.
586+
* Other environments default to `false` during `serve`. Any environment
587+
* can override its `isBundled` value via `environments[name].isBundled`.
572588
*
573589
* This is highly experimental.
574590
*
@@ -654,8 +670,6 @@ export interface ResolvedConfig extends Readonly<
654670
cacheDir: string
655671
command: 'build' | 'serve'
656672
mode: string
657-
/** `true` when build or full-bundle mode dev */
658-
isBundled: boolean
659673
isWorker: boolean
660674
// in nested worker bundle to find the main config
661675
/** @internal */
@@ -907,6 +921,7 @@ function resolveEnvironmentOptions(
907921
forceOptimizeDeps: boolean | undefined,
908922
logger: Logger,
909923
environmentName: string,
924+
isBuild: boolean,
910925
isBundledDev: boolean,
911926
// Backward compatibility
912927
isSsrTargetWebworkerSet?: boolean,
@@ -918,6 +933,9 @@ function resolveEnvironmentOptions(
918933
const isSsrTargetWebworkerEnvironment =
919934
isSsrTargetWebworkerSet && environmentName === 'ssr'
920935

936+
const isBundled =
937+
options.isBundled ?? (isBuild || (isClientEnvironment && isBundledDev))
938+
921939
if (options.define?.['process.env']) {
922940
const processEnvDefine = options.define['process.env']
923941
if (typeof processEnvDefine === 'object') {
@@ -970,9 +988,10 @@ function resolveEnvironmentOptions(
970988
options.build ?? {},
971989
logger,
972990
consumer,
973-
isBundledDev,
991+
isBundled && !isBuild,
974992
isSsrTargetWebworkerEnvironment,
975993
),
994+
isBundled,
976995
plugins: undefined!, // to be resolved later
977996
// will be set by `setOptimizeDepsPluginNames` later
978997
optimizeDepsPluginNames: undefined!,
@@ -1608,6 +1627,7 @@ export async function resolveConfig(
16081627
inlineConfig.forceOptimizeDeps,
16091628
logger,
16101629
environmentName,
1630+
isBuild,
16111631
isBundledDev,
16121632
config.ssr?.target === 'webworker',
16131633
config.server?.preTransformRequests,
@@ -1900,7 +1920,6 @@ export async function resolveConfig(
19001920
cacheDir,
19011921
command,
19021922
mode,
1903-
isBundled: config.experimental?.bundledDev || isBuild,
19041923
isWorker: false,
19051924
mainConfig: null,
19061925
bundleChain: [],

packages/vite/src/node/plugins/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ async function fileToBuiltUrl(
475475

476476
if (
477477
environment.config.command === 'serve' &&
478-
environment.config.experimental.bundledDev
478+
environment.config.isBundled
479479
) {
480480
const outputFilename = pluginContext.getFileName(referenceId)
481481
url = toOutputFilePathInJSForBundledDev(environment, outputFilename)

packages/vite/src/node/plugins/clientInjections.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
3434

3535
return {
3636
name: 'vite:client-inject',
37+
applyToEnvironment(environment) {
38+
return !environment.config.isBundled
39+
},
3740
async buildStart() {
3841
injectConfigValues = await createClientConfigValueReplacer(config)
3942
},

packages/vite/src/node/plugins/css.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
594594

595595
const cssContent = await getContentWithSourcemap(css)
596596
const code = [
597-
config.isBundled
597+
this.environment.config.isBundled
598598
? `const { updateStyle: __vite__updateStyle, removeStyle: __vite__removeStyle } = import.meta.hot._internal`
599599
: `import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
600600
path.posix.join(config.base, CLIENT_PUBLIC_PATH),
@@ -1175,6 +1175,10 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
11751175
return {
11761176
name: 'vite:css-analysis',
11771177

1178+
applyToEnvironment(environment) {
1179+
return !environment.config.isBundled
1180+
},
1181+
11781182
transform: {
11791183
filter: {
11801184
id: {

packages/vite/src/node/plugins/define.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)
1010
const escapedDotRE = /(?<!\\)\\./g
1111

1212
export function definePlugin(config: ResolvedConfig): Plugin {
13-
const isBundled = config.isBundled
1413
const isBuild = config.command === 'build'
1514
const isBuildLib = isBuild && config.build.lib
1615

@@ -28,27 +27,28 @@ export function definePlugin(config: ResolvedConfig): Plugin {
2827
})
2928
}
3029

31-
// during dev, import.meta properties are handled by importAnalysis plugin.
32-
const importMetaKeys: Record<string, string> = {}
33-
const importMetaEnvKeys: Record<string, string> = {}
34-
const importMetaFallbackKeys: Record<string, string> = {}
35-
if (isBuild) {
36-
importMetaKeys['import.meta.hot'] = `undefined`
37-
}
38-
if (isBundled) {
39-
for (const key in config.env) {
40-
const val = JSON.stringify(config.env[key])
41-
importMetaKeys[`import.meta.env.${key}`] = val
42-
importMetaEnvKeys[key] = val
43-
}
44-
// these will be set to a proper value in `generatePattern`
45-
importMetaKeys['import.meta.env.SSR'] = `undefined`
46-
importMetaFallbackKeys['import.meta.env'] = `undefined`
47-
}
48-
4930
function generatePattern(environment: Environment) {
31+
const isBundled = environment.config.isBundled
5032
const keepProcessEnv = environment.config.keepProcessEnv
5133

34+
// during dev, import.meta properties are handled by importAnalysis plugin.
35+
const importMetaKeys: Record<string, string> = {}
36+
const importMetaEnvKeys: Record<string, string> = {}
37+
const importMetaFallbackKeys: Record<string, string> = {}
38+
if (isBuild) {
39+
importMetaKeys['import.meta.hot'] = `undefined`
40+
}
41+
if (isBundled) {
42+
for (const key in config.env) {
43+
const val = JSON.stringify(config.env[key])
44+
importMetaKeys[`import.meta.env.${key}`] = val
45+
importMetaEnvKeys[key] = val
46+
}
47+
// these will be set to a proper value below
48+
importMetaKeys['import.meta.env.SSR'] = `undefined`
49+
importMetaFallbackKeys['import.meta.env'] = `undefined`
50+
}
51+
5252
const userDefine: Record<string, string> = {}
5353
const userDefineEnv: Record<string, any> = {}
5454
for (const key in environment.config.define) {
@@ -113,24 +113,27 @@ export function definePlugin(config: ResolvedConfig): Plugin {
113113
return pattern
114114
}
115115

116-
if (isBundled) {
117-
return {
118-
name: 'vite:define',
119-
options(option) {
120-
const [define, _pattern, importMetaEnvVal] = getPattern(
121-
this.environment,
122-
)
123-
define['import.meta.env'] = importMetaEnvVal
124-
define['import.meta.env.*'] = 'undefined'
125-
option.transform ??= {}
126-
option.transform.define = { ...option.transform.define, ...define }
127-
},
128-
}
129-
}
130-
131116
return {
132117
name: 'vite:define',
133118

119+
applyToEnvironment(environment) {
120+
if (environment.config.isBundled) {
121+
return {
122+
name: 'vite:define',
123+
options(option) {
124+
const [define, _pattern, importMetaEnvVal] = getPattern(
125+
this.environment,
126+
)
127+
define['import.meta.env'] = importMetaEnvVal
128+
define['import.meta.env.*'] = 'undefined'
129+
option.transform ??= {}
130+
option.transform.define = { ...option.transform.define, ...define }
131+
},
132+
}
133+
}
134+
return true
135+
},
136+
134137
transform: {
135138
handler(code, id) {
136139
if (this.environment.config.consumer === 'client') {

packages/vite/src/node/plugins/dynamicImportVars.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { parseAst } from 'rolldown/parseAst'
66
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
77
import { viteDynamicImportVarsPlugin as nativeDynamicImportVarsPlugin } from 'rolldown/experimental'
88
import { exactRegex } from 'rolldown/filter'
9-
import { type Plugin, perEnvironmentPlugin } from '../plugin'
9+
import type { Plugin } from '../plugin'
1010
import type { ResolvedConfig } from '../config'
1111
import { CLIENT_ENTRY } from '../constants'
1212
import { createBackCompatIdResolver } from '../idResolver'
@@ -173,22 +173,6 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
173173
extensions: [],
174174
})
175175

176-
if (config.isBundled) {
177-
return perEnvironmentPlugin('native:dynamic-import-vars', (environment) => {
178-
const { include, exclude } =
179-
environment.config.build.dynamicImportVarsOptions
180-
181-
return nativeDynamicImportVarsPlugin({
182-
include,
183-
exclude,
184-
resolver(id, importer) {
185-
return resolve(environment, id, importer)
186-
},
187-
sourcemap: !!environment.config.build.sourcemap,
188-
})
189-
})
190-
}
191-
192176
const getFilter = perEnvironmentState((environment: Environment) => {
193177
const { include, exclude } =
194178
environment.config.build.dynamicImportVarsOptions
@@ -198,6 +182,23 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
198182
return {
199183
name: 'vite:dynamic-import-vars',
200184

185+
applyToEnvironment(environment) {
186+
if (environment.config.isBundled) {
187+
const { include, exclude } =
188+
environment.config.build.dynamicImportVarsOptions
189+
190+
return nativeDynamicImportVarsPlugin({
191+
include,
192+
exclude,
193+
resolver(id, importer) {
194+
return resolve(environment, id, importer)
195+
},
196+
sourcemap: !!environment.config.build.sourcemap,
197+
})
198+
}
199+
return true
200+
},
201+
201202
resolveId: {
202203
filter: { id: exactRegex(dynamicImportHelperId) },
203204
handler(id) {

packages/vite/src/node/plugins/html.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
430430
return {
431431
name: 'vite:build-html',
432432

433+
applyToEnvironment(environment) {
434+
return environment.config.isBundled
435+
},
436+
433437
transform: {
434438
filter: { id: /\.html$/ },
435439
async handler(html, id) {

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
257257
return {
258258
name: 'vite:import-analysis',
259259

260+
applyToEnvironment(environment) {
261+
return !environment.config.isBundled
262+
},
263+
260264
async transform(source, importer) {
261265
const environment = this.environment as DevEnvironment
262266
const ssr = environment.config.consumer === 'server'

0 commit comments

Comments
 (0)