@@ -8,13 +8,11 @@ import type {
88 LoggingFunction ,
99 ModuleFormat ,
1010 OutputOptions ,
11- Plugin ,
1211 RollupBuild ,
1312 RollupError ,
1413 RollupLog ,
1514 RollupOptions ,
1615 RollupOutput ,
17- PluginContext as RollupPluginContext ,
1816 RollupWatcher ,
1917 WatcherOptions ,
2018} from 'rollup'
@@ -68,6 +66,7 @@ import { mergeConfig } from './publicUtils'
6866import { webWorkerPostPlugin } from './plugins/worker'
6967import { getHookHandler } from './plugins'
7068import { Environment } from './environment'
69+ import type { Plugin , PluginContext } from './plugin'
7170
7271export interface BuildEnvironmentOptions {
7372 /**
@@ -535,6 +534,7 @@ export async function build(
535534function resolveConfigToBuild (
536535 inlineConfig : InlineConfig = { } ,
537536 patchConfig ?: ( config : ResolvedConfig ) => void ,
537+ patchPlugins ?: ( plugins : Plugin [ ] ) => void ,
538538) {
539539 return resolveConfig (
540540 inlineConfig ,
@@ -543,6 +543,7 @@ function resolveConfigToBuild(
543543 'production' ,
544544 false ,
545545 patchConfig ,
546+ patchPlugins ,
546547 )
547548}
548549
@@ -1125,7 +1126,6 @@ function wrapEnvironmentLoad(
11251126 return fn . call (
11261127 injectEnvironmentInContext ( this , environment ) ,
11271128 id ,
1128- // @ts -expect-error: Receiving options param to be future-proof if Rollup adds it
11291129 injectSsrFlag ( args [ 0 ] , environment ) ,
11301130 )
11311131 }
@@ -1152,7 +1152,6 @@ function wrapEnvironmentTransform(
11521152 injectEnvironmentInContext ( this , environment ) ,
11531153 code ,
11541154 importer ,
1155- // @ts -expect-error: Receiving options param to be future-proof if Rollup adds it
11561155 injectSsrFlag ( args [ 0 ] , environment ) ,
11571156 )
11581157 }
@@ -1167,8 +1166,8 @@ function wrapEnvironmentTransform(
11671166 }
11681167}
11691168
1170- function injectEnvironmentInContext (
1171- context : RollupPluginContext ,
1169+ function injectEnvironmentInContext < Context extends PluginContext > (
1170+ context : Context ,
11721171 environment ?: BuildEnvironment ,
11731172) {
11741173 return new Proxy ( context , {
@@ -1479,8 +1478,18 @@ export async function createBuilder(
14791478 let environmentConfig = config
14801479 if ( ! config . builder . sharedConfigBuild ) {
14811480 const patchConfig = ( resolved : ResolvedConfig ) => {
1481+ // Until the ecosystem updates to use `environment.options.build` instead of `config.build`,
1482+ // we need to make override `config.build` for the current environment.
1483+ // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
1484+ // remove the default values that shouldn't be used at all once the config is resolved
1485+ ; ( resolved . build as ResolvedBuildOptions ) = {
1486+ ...resolved . environments [ name ] . build ,
1487+ lib : false ,
1488+ }
1489+ }
1490+ const patchPlugins = ( resolvedPlugins : Plugin [ ] ) => {
14821491 // Force opt-in shared plugins
1483- const environmentPlugins = [ ...resolved . plugins ]
1492+ const environmentPlugins = [ ...resolvedPlugins ]
14841493 let validMixedPlugins = true
14851494 for ( let i = 0 ; i < environmentPlugins . length ; i ++ ) {
14861495 const environmentPlugin = environmentPlugins [ i ]
@@ -1497,18 +1506,16 @@ export async function createBuilder(
14971506 }
14981507 }
14991508 if ( validMixedPlugins ) {
1500- ; ( resolved . plugins as Plugin [ ] ) = environmentPlugins
1501- }
1502- // Until the ecosystem updates to use `environment.options.build` instead of `config.build`,
1503- // we need to make override `config.build` for the current environment.
1504- // We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
1505- // remove the default values that shouldn't be used at all once the config is resolved
1506- ; ( resolved . build as ResolvedBuildOptions ) = {
1507- ...resolved . environments [ name ] . build ,
1508- lib : false ,
1509+ for ( let i = 0 ; i < environmentPlugins . length ; i ++ ) {
1510+ resolvedPlugins [ i ] = environmentPlugins [ i ]
1511+ }
15091512 }
15101513 }
1511- environmentConfig = await resolveConfigToBuild ( inlineConfig , patchConfig )
1514+ environmentConfig = await resolveConfigToBuild (
1515+ inlineConfig ,
1516+ patchConfig ,
1517+ patchPlugins ,
1518+ )
15121519 }
15131520
15141521 const environment = await createEnvironment ( name , environmentConfig )
0 commit comments