@@ -29,11 +29,12 @@ import {
2929 VERSION ,
3030} from './constants'
3131import 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'
3738import { buildReporterPlugin } from './plugins/reporter'
3839import { buildEsbuildPlugin } from './plugins/esbuild'
3940import { type TerserOptions , terserPlugin } from './plugins/terser'
@@ -1322,20 +1323,31 @@ function areSeparateFolders(a: string, b: string) {
13221323export 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
13371348export 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
0 commit comments