Skip to content

Commit 00079da

Browse files
hi-ogawapatak-dev
andauthored
fix: custom environment preload injection (#16541)
Co-authored-by: patak-dev <[email protected]>
1 parent a8adcac commit 00079da

File tree

5 files changed

+81
-19
lines changed

5 files changed

+81
-19
lines changed

packages/vite/src/node/__tests__/build.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,46 @@ describe('resolveBuildOutputs', () => {
637637
],
638638
})
639639
})
640+
641+
test('ssr builtin', async () => {
642+
const builder = await createBuilder({
643+
root: resolve(__dirname, 'fixtures/dynamic-import'),
644+
environments: {
645+
ssr: {
646+
build: {
647+
ssr: true,
648+
rollupOptions: {
649+
input: {
650+
index: '/entry',
651+
},
652+
},
653+
},
654+
},
655+
},
656+
})
657+
const result = await builder.build(builder.environments.ssr)
658+
expect((result as RollupOutput).output[0].code).not.toContain('preload')
659+
})
660+
661+
test('ssr custom', async () => {
662+
const builder = await createBuilder({
663+
root: resolve(__dirname, 'fixtures/dynamic-import'),
664+
environments: {
665+
custom: {
666+
build: {
667+
ssr: true,
668+
rollupOptions: {
669+
input: {
670+
index: '/entry',
671+
},
672+
},
673+
},
674+
},
675+
},
676+
})
677+
const result = await builder.build(builder.environments.custom)
678+
expect((result as RollupOutput).output[0].code).not.toContain('preload')
679+
})
640680
})
641681

642682
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const hello = 'hello'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export async function main() {
2+
const mod = await import('./dep.mjs')
3+
console.log(mod)
4+
}

packages/vite/src/node/build.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
6866
import { webWorkerPostPlugin } from './plugins/worker'
6967
import { getHookHandler } from './plugins'
7068
import { Environment } from './environment'
69+
import type { Plugin, PluginContext } from './plugin'
7170

7271
export interface BuildEnvironmentOptions {
7372
/**
@@ -535,6 +534,7 @@ export async function build(
535534
function 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)

packages/vite/src/node/config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ export async function resolveConfig(
759759
defaultNodeEnv = 'development',
760760
isPreview = false,
761761
patchConfig: ((config: ResolvedConfig) => void) | undefined = undefined,
762+
patchPlugins: ((plugins: Plugin[]) => void) | undefined = undefined,
762763
): Promise<ResolvedConfig> {
763764
let config = inlineConfig
764765
let configFileDependencies: string[] = []
@@ -1284,15 +1285,24 @@ export async function resolveConfig(
12841285
...config,
12851286
...resolved,
12861287
}
1287-
;(resolved.plugins as Plugin[]) = await resolvePlugins(
1288+
1289+
// Backward compatibility hook, modify the resolved config before it is used
1290+
// to create inernal plugins. For example, `config.build.ssr`. Once we rework
1291+
// internal plugins to use environment.options, we can remove the dual
1292+
// patchConfig/patchPlugins and have a single patchConfig before configResolved
1293+
// gets called
1294+
patchConfig?.(resolved)
1295+
1296+
const resolvedPlugins = await resolvePlugins(
12881297
resolved,
12891298
prePlugins,
12901299
normalPlugins,
12911300
postPlugins,
12921301
)
12931302

12941303
// Backward compatibility hook used in builder
1295-
patchConfig?.(resolved)
1304+
patchPlugins?.(resolvedPlugins)
1305+
;(resolved.plugins as Plugin[]) = resolvedPlugins
12961306

12971307
Object.assign(resolved, createPluginHookUtils(resolved.plugins))
12981308

0 commit comments

Comments
 (0)