@@ -493,35 +493,13 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
493
493
export async function build (
494
494
inlineConfig : InlineConfig = { } ,
495
495
) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
496
- const patchConfig = ( resolved : ResolvedConfig ) => {
497
- // Until the ecosystem updates to use `environment.config.build` instead of `config.build`,
498
- // we need to make override `config.build` for the current environment.
499
- // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
500
- // remove the default values that shouldn't be used at all once the config is resolved
501
- const environmentName = resolved . build . ssr ? 'ssr' : 'client'
502
- ; ( resolved . build as ResolvedBuildOptions ) = {
503
- ...resolved . environments [ environmentName ] . build ,
504
- }
505
- }
506
- const config = await resolveConfigToBuild ( inlineConfig , patchConfig )
507
- return buildWithResolvedConfig ( config )
496
+ const builder = await createBuilder ( inlineConfig , true )
497
+ const environment = Object . values ( builder . environments ) [ 0 ]
498
+ if ( ! environment ) throw new Error ( 'No environment found' )
499
+ return builder . build ( environment )
508
500
}
509
501
510
- /**
511
- * @internal used to implement `vite build` for backward compatibility
512
- */
513
- export async function buildWithResolvedConfig (
514
- config : ResolvedConfig ,
515
- ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
516
- const environmentName = config . build . ssr ? 'ssr' : 'client'
517
- const environment = await config . environments [
518
- environmentName
519
- ] . build . createEnvironment ( environmentName , config )
520
- await environment . init ( )
521
- return buildEnvironment ( environment )
522
- }
523
-
524
- export function resolveConfigToBuild (
502
+ function resolveConfigToBuild (
525
503
inlineConfig : InlineConfig = { } ,
526
504
patchConfig ?: ( config : ResolvedConfig ) => void ,
527
505
patchPlugins ?: ( resolvedPlugins : Plugin [ ] ) => void ,
@@ -540,7 +518,7 @@ export function resolveConfigToBuild(
540
518
/**
541
519
* Build an App environment, or a App library (if libraryOptions is provided)
542
520
**/
543
- export async function buildEnvironment (
521
+ async function buildEnvironment (
544
522
environment : BuildEnvironment ,
545
523
) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
546
524
const { root, packageCache } = environment . config
@@ -1486,7 +1464,6 @@ export interface ViteBuilder {
1486
1464
export interface BuilderOptions {
1487
1465
sharedConfigBuild ?: boolean
1488
1466
sharedPlugins ?: boolean
1489
- entireApp ?: boolean
1490
1467
buildApp ?: ( builder : ViteBuilder ) => Promise < void >
1491
1468
}
1492
1469
@@ -1497,12 +1474,12 @@ async function defaultBuildApp(builder: ViteBuilder): Promise<void> {
1497
1474
}
1498
1475
1499
1476
export function resolveBuilderOptions (
1500
- options : BuilderOptions = { } ,
1501
- ) : ResolvedBuilderOptions {
1477
+ options : BuilderOptions | undefined ,
1478
+ ) : ResolvedBuilderOptions | undefined {
1479
+ if ( ! options ) return
1502
1480
return {
1503
1481
sharedConfigBuild : options . sharedConfigBuild ?? false ,
1504
1482
sharedPlugins : options . sharedPlugins ?? false ,
1505
- entireApp : options . entireApp ?? false ,
1506
1483
buildApp : options . buildApp ?? defaultBuildApp ,
1507
1484
}
1508
1485
}
@@ -1515,83 +1492,91 @@ export type ResolvedBuilderOptions = Required<BuilderOptions>
1515
1492
*/
1516
1493
export async function createBuilder (
1517
1494
inlineConfig : InlineConfig = { } ,
1495
+ useLegacyBuilder : null | boolean = false ,
1518
1496
) : Promise < ViteBuilder > {
1519
- const config = await resolveConfigToBuild ( inlineConfig )
1520
- return createBuilderWithResolvedConfig ( inlineConfig , config )
1521
- }
1497
+ const patchConfig = ( resolved : ResolvedConfig ) => {
1498
+ if ( ! ( useLegacyBuilder ?? ! resolved . builder ) ) return
1499
+
1500
+ // Until the ecosystem updates to use `environment.config.build` instead of `config.build`,
1501
+ // we need to make override `config.build` for the current environment.
1502
+ // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
1503
+ // remove the default values that shouldn't be used at all once the config is resolved
1504
+ const environmentName = resolved . build . ssr ? 'ssr' : 'client'
1505
+ ; ( resolved . build as ResolvedBuildOptions ) = {
1506
+ ...resolved . environments [ environmentName ] . build ,
1507
+ }
1508
+ }
1509
+ const config = await resolveConfigToBuild ( inlineConfig , patchConfig )
1510
+ useLegacyBuilder ??= ! config . builder
1511
+ const configBuilder = config . builder ?? resolveBuilderOptions ( { } ) !
1522
1512
1523
- /**
1524
- * Used to implement the `vite build` command without resolving the config twice
1525
- * @internal
1526
- */
1527
- export async function createBuilderWithResolvedConfig (
1528
- inlineConfig : InlineConfig ,
1529
- config : ResolvedConfig ,
1530
- ) : Promise < ViteBuilder > {
1531
1513
const environments : Record < string , BuildEnvironment > = { }
1532
1514
1533
1515
const builder : ViteBuilder = {
1534
1516
environments,
1535
1517
config,
1536
1518
async buildApp ( ) {
1537
- return config . builder . buildApp ( builder )
1519
+ return configBuilder . buildApp ( builder )
1538
1520
} ,
1539
1521
async build ( environment : BuildEnvironment ) {
1540
1522
return buildEnvironment ( environment )
1541
1523
} ,
1542
1524
}
1543
1525
1544
- for ( const environmentName of Object . keys ( config . environments ) ) {
1545
- // We need to resolve the config again so we can properly merge options
1546
- // and get a new set of plugins for each build environment. The ecosystem
1547
- // expects plugins to be run for the same environment once they are created
1548
- // and to process a single bundle at a time (contrary to dev mode where
1549
- // plugins are built to handle multiple environments concurrently).
1550
- let environmentConfig = config
1551
- if ( ! config . builder . sharedConfigBuild ) {
1552
- const patchConfig = ( resolved : ResolvedConfig ) => {
1553
- // Until the ecosystem updates to use `environment.config.build` instead of `config.build`,
1554
- // we need to make override `config.build` for the current environment.
1555
- // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
1556
- // remove the default values that shouldn't be used at all once the config is resolved
1557
- ; ( resolved . build as ResolvedBuildOptions ) = {
1558
- ...resolved . environments [ environmentName ] . build ,
1526
+ async function setupEnvironment ( name : string , config : ResolvedConfig ) {
1527
+ const environment = await config . build . createEnvironment ( name , config )
1528
+ await environment . init ( )
1529
+ environments [ name ] = environment
1530
+ }
1531
+
1532
+ if ( useLegacyBuilder ) {
1533
+ await setupEnvironment ( config . build . ssr ? 'ssr' : 'client' , config )
1534
+ } else {
1535
+ for ( const environmentName of Object . keys ( config . environments ) ) {
1536
+ // We need to resolve the config again so we can properly merge options
1537
+ // and get a new set of plugins for each build environment. The ecosystem
1538
+ // expects plugins to be run for the same environment once they are created
1539
+ // and to process a single bundle at a time (contrary to dev mode where
1540
+ // plugins are built to handle multiple environments concurrently).
1541
+ let environmentConfig = config
1542
+ if ( ! configBuilder . sharedConfigBuild ) {
1543
+ const patchConfig = ( resolved : ResolvedConfig ) => {
1544
+ // Until the ecosystem updates to use `environment.config.build` instead of `config.build`,
1545
+ // we need to make override `config.build` for the current environment.
1546
+ // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
1547
+ // remove the default values that shouldn't be used at all once the config is resolved
1548
+ ; ( resolved . build as ResolvedBuildOptions ) = {
1549
+ ...resolved . environments [ environmentName ] . build ,
1550
+ }
1559
1551
}
1560
- }
1561
- const patchPlugins = ( resolvedPlugins : Plugin [ ] ) => {
1562
- // Force opt-in shared plugins
1563
- let j = 0
1564
- for ( let i = 0 ; i < resolvedPlugins . length ; i ++ ) {
1565
- const environmentPlugin = resolvedPlugins [ i ]
1566
- if (
1567
- config . builder . sharedPlugins ||
1568
- environmentPlugin . sharedDuringBuild
1569
- ) {
1570
- for ( let k = j ; k < config . plugins . length ; k ++ ) {
1571
- if ( environmentPlugin . name === config . plugins [ k ] . name ) {
1572
- resolvedPlugins [ i ] = config . plugins [ k ]
1573
- j = k + 1
1574
- break
1552
+ const patchPlugins = ( resolvedPlugins : Plugin [ ] ) => {
1553
+ // Force opt-in shared plugins
1554
+ let j = 0
1555
+ for ( let i = 0 ; i < resolvedPlugins . length ; i ++ ) {
1556
+ const environmentPlugin = resolvedPlugins [ i ]
1557
+ if (
1558
+ configBuilder . sharedPlugins ||
1559
+ environmentPlugin . sharedDuringBuild
1560
+ ) {
1561
+ for ( let k = j ; k < config . plugins . length ; k ++ ) {
1562
+ if ( environmentPlugin . name === config . plugins [ k ] . name ) {
1563
+ resolvedPlugins [ i ] = config . plugins [ k ]
1564
+ j = k + 1
1565
+ break
1566
+ }
1575
1567
}
1576
1568
}
1577
1569
}
1578
1570
}
1571
+ environmentConfig = await resolveConfigToBuild (
1572
+ inlineConfig ,
1573
+ patchConfig ,
1574
+ patchPlugins ,
1575
+ )
1579
1576
}
1580
- environmentConfig = await resolveConfigToBuild (
1581
- inlineConfig ,
1582
- patchConfig ,
1583
- patchPlugins ,
1584
- )
1585
- }
1586
-
1587
- const environment = await environmentConfig . build . createEnvironment (
1588
- environmentName ,
1589
- environmentConfig ,
1590
- )
1591
1577
1592
- await environment . init ( )
1593
-
1594
- environments [ environmentName ] = environment
1578
+ await setupEnvironment ( environmentName , environmentConfig )
1579
+ }
1595
1580
}
1596
1581
1597
1582
return builder
0 commit comments