Skip to content

Commit c7e4da2

Browse files
committed
refactor: base environment.config + environment.options
1 parent 72fe84e commit c7e4da2

8 files changed

Lines changed: 108 additions & 125 deletions

File tree

packages/vite/src/node/build.ts

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ import {
2929
VERSION,
3030
} from './constants'
3131
import type {
32-
BuildEnvironmentConfig,
32+
EnvironmentOptions,
3333
InlineConfig,
3434
ResolvedConfig,
35+
ResolvedEnvironmentOptions,
3536
} from './config'
36-
import { resolveConfig } from './config'
37+
import { getDefaultResolvedEnvironmentOptions, resolveConfig } from './config'
3738
import { buildReporterPlugin } from './plugins/reporter'
3839
import { buildEsbuildPlugin } from './plugins/esbuild'
3940
import { type TerserOptions, terserPlugin } from './plugins/terser'
@@ -1322,20 +1323,31 @@ function areSeparateFolders(a: string, b: string) {
13221323
export class BuildEnvironment extends Environment {
13231324
mode = 'build' as const
13241325
builder: ViteBuilder
1325-
config: BuildEnvironmentConfig
1326+
13261327
constructor(
13271328
builder: ViteBuilder,
13281329
name: string,
1329-
options?: { config?: BuildEnvironmentConfig },
1330+
setup?: {
1331+
options?: EnvironmentOptions
1332+
},
13301333
) {
1331-
super(name)
1334+
let options =
1335+
builder.config.environments[name] ??
1336+
getDefaultResolvedEnvironmentOptions(builder.config)
1337+
if (setup?.options) {
1338+
options = mergeConfig(
1339+
options,
1340+
setup?.options,
1341+
) as ResolvedEnvironmentOptions
1342+
}
1343+
super(name, builder.config, options)
13321344
this.builder = builder
1333-
this.config = options?.config ?? { build: {} }
13341345
}
13351346
}
13361347

13371348
export interface ViteBuilder {
13381349
environments: Record<string, BuildEnvironment>
1350+
config: ResolvedConfig
13391351
build(): Promise<void>
13401352
buildEnvironment(environment: BuildEnvironment): Promise<void>
13411353
}
@@ -1383,27 +1395,21 @@ export async function createViteBuilder(
13831395
): Promise<ViteBuilder> {
13841396
// Plugins passed to the Builder inline config needs to be created
13851397
// from a factory to ensure each build has their own instances
1386-
const getDefaultInlineConfig = (): InlineConfig => {
1398+
const resolveConfig = (): Promise<ResolvedConfig> => {
13871399
const { plugins } = defaultBuilderInlineConfig
1388-
return plugins
1400+
const defaultInlineConfig = plugins
13891401
? {
13901402
...defaultBuilderInlineConfig,
13911403
plugins: plugins(),
13921404
}
13931405
: (defaultBuilderInlineConfig as InlineConfig)
1394-
}
13951406

1396-
// TODO: defineEnvironments -> config
1407+
// We resolve the whole config including plugins here but later on we
1408+
// need to refactor resolveConfig to only resolve the environments config
1409+
return resolveConfigToBuild(defaultInlineConfig)
1410+
}
13971411

1398-
// We resolve the whole config including plugins here but later on we
1399-
// need to refactor resolveConfig to only resolve the environments config
1400-
const defaultInlineConfig = getDefaultInlineConfig()
1401-
const defaultConfig = await resolveConfig(
1402-
defaultInlineConfig,
1403-
'build',
1404-
'production',
1405-
'production',
1406-
)
1412+
const defaultConfig = await resolveConfig()
14071413

14081414
if (defaultConfig.build.lib) {
14091415
throw new Error('Library mode is not supported in ViteBuilder')
@@ -1414,37 +1420,17 @@ export async function createViteBuilder(
14141420

14151421
const environments: Record<string, BuildEnvironment> = {}
14161422

1417-
async function resolveEnvironmentConfig(environment: BuildEnvironment) {
1418-
// We need to resolve the config again so we can properly merge options
1419-
// and get a new set of plugins for each build environment. The ecosystem
1420-
// expects plugins to be run for the same environment once they are created
1421-
// and to process a single bundle at a time (contrary to dev mode where
1422-
// plugins are built to handle multiple environments concurrently).
1423-
1424-
let userConfig = getDefaultInlineConfig()
1425-
const inlineConfigEnvironmentOverrides =
1426-
userConfig.environments?.[environment.name]
1427-
if (inlineConfigEnvironmentOverrides) {
1428-
userConfig = mergeConfig(userConfig, inlineConfigEnvironmentOverrides)
1429-
}
1430-
if (environment.config) {
1431-
userConfig = mergeConfig(userConfig, environment.config)
1432-
}
1433-
1434-
return await resolveConfigToBuild(userConfig)
1435-
}
1436-
14371423
const builder: ViteBuilder = {
14381424
environments,
1425+
config: defaultConfig,
14391426
async build() {
14401427
const buildTasks = []
14411428
for (const environment of Object.values(environments)) {
1442-
const config = await resolveEnvironmentConfig(environment)
14431429
const buildTask = {
14441430
environment,
1445-
config,
1431+
config: environment.config,
14461432
run: async () => {
1447-
await buildEnvironment(config, environment)
1433+
await buildEnvironment(environment.config, environment)
14481434
},
14491435
cancel: () => {}, // TODO, maybe not needed
14501436
}
@@ -1453,18 +1439,26 @@ export async function createViteBuilder(
14531439
await defaultConfig.builder.runBuildTasks(builder, buildTasks)
14541440
},
14551441
async buildEnvironment(environment: BuildEnvironment) {
1456-
const config = await resolveEnvironmentConfig(environment)
1457-
await buildEnvironment(config, environment)
1442+
await buildEnvironment(environment.config, environment)
14581443
},
14591444
}
14601445

14611446
for (const name of Object.keys(defaultConfig.environments)) {
1462-
const environmentConfig = defaultConfig.environments[name]
1447+
const environmentOptions = defaultConfig.environments[name]
14631448
const createEnvironment =
1464-
environmentConfig.build?.createEnvironment ??
1449+
environmentOptions.build?.createEnvironment ??
14651450
((builder: ViteBuilder, name: string) =>
14661451
new BuildEnvironment(builder, name))
1467-
const environment = createEnvironment(builder, name)
1452+
1453+
// We need to resolve the config again so we can properly merge options
1454+
// and get a new set of plugins for each build environment. The ecosystem
1455+
// expects plugins to be run for the same environment once they are created
1456+
// and to process a single bundle at a time (contrary to dev mode where
1457+
// plugins are built to handle multiple environments concurrently).
1458+
const environmentConfig = await resolveConfig()
1459+
const environmentBuilder = { ...builder, config: environmentConfig }
1460+
1461+
const environment = createEnvironment(environmentBuilder, name)
14681462
environments[name] = environment
14691463
}
14701464

packages/vite/src/node/config.ts

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ type EnvironmentResolveOptions = ResolveOptions & {
187187
alias?: AliasOptions
188188
}
189189

190-
export interface SharedEnvironmentConfig {
190+
export interface SharedEnvironmentOptions {
191191
/**
192192
* Configure resolver
193193
*/
194194
resolve?: EnvironmentResolveOptions
195195
}
196196

197-
export interface EnvironmentConfig extends SharedEnvironmentConfig {
197+
export interface EnvironmentOptions extends SharedEnvironmentOptions {
198198
/**
199199
* Dev specific options
200200
*/
@@ -205,21 +205,9 @@ export interface EnvironmentConfig extends SharedEnvironmentConfig {
205205
build?: BuildOptions
206206
}
207207

208-
export type ResolvedEnvironmentConfig = Required<EnvironmentConfig>
208+
export type ResolvedEnvironmentOptions = Required<EnvironmentOptions>
209209

210-
export interface DevEnvironmentConfig extends SharedEnvironmentConfig {
211-
dev?: DevOptions
212-
}
213-
214-
export type ResolvedDevEnvironmentConfig = Required<DevEnvironmentConfig>
215-
216-
export interface BuildEnvironmentConfig extends SharedEnvironmentConfig {
217-
build?: BuildOptions
218-
}
219-
220-
export type ResolvedBuildEnvironmentConfig = Required<BuildEnvironmentConfig>
221-
222-
export interface UserConfig extends EnvironmentConfig {
210+
export interface UserConfig extends EnvironmentOptions {
223211
/**
224212
* Project root directory. Can be an absolute path, or a path relative from
225213
* the location of the config file itself.
@@ -365,14 +353,14 @@ export interface UserConfig extends EnvironmentConfig {
365353
optimizeDeps?: DepOptimizationOptions
366354
/**
367355
* SSR specific options
368-
* We could make SSROptions be a EnvironmentConfig if we can abstract
356+
* We could make SSROptions be a EnvironmentOptions if we can abstract
369357
* external/noExternal for environments in general.
370358
*/
371359
ssr?: SSROptions
372360
/**
373361
* Environment overrides
374362
*/
375-
environments?: Record<string, EnvironmentConfig>
363+
environments?: Record<string, EnvironmentOptions>
376364
/**
377365
* Whether your application is a Single Page Application (SPA),
378366
* a Multi-Page Application (MPA), or Custom Application (SSR
@@ -500,7 +488,7 @@ export type ResolvedConfig = Readonly<
500488
worker: ResolvedWorkerOptions
501489
appType: AppType
502490
experimental: ExperimentalOptions
503-
environments: Record<string, ResolvedEnvironmentConfig>
491+
environments: Record<string, ResolvedEnvironmentOptions>
504492
} & PluginHookUtils
505493
>
506494

@@ -526,11 +514,11 @@ export function resolveDevOptions(
526514
}
527515
}
528516

529-
function resolveEnvironmentConfig(
530-
config: EnvironmentConfig,
517+
function resolveEnvironmentOptions(
518+
config: EnvironmentOptions,
531519
resolvedRoot: string,
532520
logger: Logger,
533-
): ResolvedEnvironmentConfig {
521+
): ResolvedEnvironmentOptions {
534522
const resolve = resolveEnvironmentResolveOptions(config.resolve, logger)
535523
return {
536524
resolve,
@@ -539,30 +527,22 @@ function resolveEnvironmentConfig(
539527
}
540528
}
541529

542-
export function getDefaultEnvironmentConfig(
530+
export function getDefaultEnvironmentOptions(
543531
config: UserConfig,
544-
): EnvironmentConfig {
532+
): EnvironmentOptions {
545533
return {
546534
resolve: config.resolve,
547535
dev: config.dev,
548536
build: config.build,
549537
}
550538
}
551539

552-
export function getDefaultResolvedDevEnvironmentConfig(
540+
export function getDefaultResolvedEnvironmentOptions(
553541
config: ResolvedConfig,
554-
): ResolvedDevEnvironmentConfig {
542+
): ResolvedEnvironmentOptions {
555543
return {
556544
resolve: config.resolve,
557545
dev: config.dev,
558-
}
559-
}
560-
561-
export function getDefaultResolvedBuildEnvironmentConfig(
562-
config: ResolvedConfig,
563-
): ResolvedBuildEnvironmentConfig {
564-
return {
565-
resolve: config.resolve,
566546
build: config.build,
567547
}
568548
}
@@ -782,7 +762,7 @@ export async function resolveConfig(
782762
checkBadCharactersInPath(resolvedRoot, logger)
783763

784764
// Backward compatibility: merge optimizeDeps into environments.client.dev.optimizeDeps as defaults
785-
// TODO: should entries and force be in EnvironmentConfig?
765+
// TODO: should entries and force be in EnvironmentOptions?
786766
const { entries, force, ...deprecatedClientOptimizeDepsConfig } =
787767
config.optimizeDeps ?? {}
788768
let configEnvironmentsClient = config.environments!.client!
@@ -820,19 +800,19 @@ export async function resolveConfig(
820800
}
821801

822802
// Merge default environment config values
823-
const defaultEnvironmentConfig = getDefaultEnvironmentConfig(config)
803+
const defaultEnvironmentOptions = getDefaultEnvironmentOptions(config)
824804
for (const name of Object.keys(config.environments)) {
825805
config.environments[name] = mergeConfig(
826-
defaultEnvironmentConfig,
806+
defaultEnvironmentOptions,
827807
config.environments[name],
828808
)
829809
}
830810

831811
await runConfigEnvironmentHook(config.environments, userPlugins, configEnv)
832812

833-
const resolvedEnvironments: Record<string, ResolvedEnvironmentConfig> = {}
813+
const resolvedEnvironments: Record<string, ResolvedEnvironmentOptions> = {}
834814
for (const name of Object.keys(config.environments)) {
835-
resolvedEnvironments[name] = resolveEnvironmentConfig(
815+
resolvedEnvironments[name] = resolveEnvironmentOptions(
836816
config.environments[name],
837817
resolvedRoot,
838818
logger,
@@ -1593,7 +1573,7 @@ async function runConfigHook(
15931573
}
15941574

15951575
async function runConfigEnvironmentHook(
1596-
environments: Record<string, EnvironmentConfig>,
1576+
environments: Record<string, EnvironmentOptions>,
15971577
plugins: Plugin[],
15981578
configEnv: ConfigEnv,
15991579
): Promise<void> {
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
// We are using command: 'serve' | 'build' to aling with the command flag passed to hooks.
2-
// It would be better if we would use 'dev' here but can tackle that if we managed to modify
3-
// command at the hooks level. There could also be Preview environments later on.
1+
import type { Logger } from './logger'
2+
import type { ResolvedConfig, ResolvedEnvironmentOptions } from './config'
43

54
export class Environment {
65
name: string
7-
constructor(name: string) {
6+
config: ResolvedConfig
7+
options: ResolvedEnvironmentOptions
8+
get logger(): Logger {
9+
return this.config.logger
10+
}
11+
constructor(
12+
name: string,
13+
config: ResolvedConfig,
14+
options: ResolvedEnvironmentOptions,
15+
) {
816
this.name = name
17+
this.config = config
18+
this.options = options
919
}
1020
}

packages/vite/src/node/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ export { transformWithEsbuild } from './plugins/esbuild'
1717
export { buildErrorMessage } from './server/middlewares/error'
1818

1919
export { RemoteEnvironmentTransport } from './server/environmentTransport'
20-
export { createNodeEnvironment } from './server/environments/nodeEnvironment'
21-
export {
22-
DevEnvironment,
23-
type DevEnvironmentOptions,
24-
} from './server/environment'
20+
export { createNodeDevEnvironment } from './server/environments/nodeEnvironment'
21+
export { DevEnvironment, type DevEnvironmentSetup } from './server/environment'
2522
export { BuildEnvironment } from './build'
2623

2724
export { fetchModule, type FetchModuleOptions } from './ssr/fetchModule'

packages/vite/src/node/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
} from 'rollup'
1111
import type {
1212
ConfigEnv,
13-
EnvironmentConfig,
13+
EnvironmentOptions,
1414
ResolvedConfig,
1515
UserConfig,
1616
} from './config'
@@ -111,13 +111,13 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
111111
(
112112
this: void,
113113
name: string,
114-
config: EnvironmentConfig,
114+
config: EnvironmentOptions,
115115
env: ConfigEnv,
116116
) =>
117-
| EnvironmentConfig
117+
| EnvironmentOptions
118118
| null
119119
| void
120-
| Promise<EnvironmentConfig | null | void>
120+
| Promise<EnvironmentOptions | null | void>
121121
>
122122
/**
123123
* Use this hook to read and store the final resolved vite config.

0 commit comments

Comments
 (0)