Skip to content

Commit

Permalink
fix: custom environment preload injection (#16541)
Browse files Browse the repository at this point in the history
Co-authored-by: patak-dev <[email protected]>
  • Loading branch information
hi-ogawa and patak-dev authored Apr 28, 2024
1 parent a8adcac commit 00079da
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
40 changes: 40 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,46 @@ describe('resolveBuildOutputs', () => {
],
})
})

test('ssr builtin', async () => {
const builder = await createBuilder({
root: resolve(__dirname, 'fixtures/dynamic-import'),
environments: {
ssr: {
build: {
ssr: true,
rollupOptions: {
input: {
index: '/entry',
},
},
},
},
},
})
const result = await builder.build(builder.environments.ssr)
expect((result as RollupOutput).output[0].code).not.toContain('preload')
})

test('ssr custom', async () => {
const builder = await createBuilder({
root: resolve(__dirname, 'fixtures/dynamic-import'),
environments: {
custom: {
build: {
ssr: true,
rollupOptions: {
input: {
index: '/entry',
},
},
},
},
},
})
const result = await builder.build(builder.environments.custom)
expect((result as RollupOutput).output[0].code).not.toContain('preload')
})
})

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = 'hello'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function main() {
const mod = await import('./dep.mjs')
console.log(mod)
}
41 changes: 24 additions & 17 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import type {
LoggingFunction,
ModuleFormat,
OutputOptions,
Plugin,
RollupBuild,
RollupError,
RollupLog,
RollupOptions,
RollupOutput,
PluginContext as RollupPluginContext,
RollupWatcher,
WatcherOptions,
} from 'rollup'
Expand Down Expand Up @@ -68,6 +66,7 @@ import { mergeConfig } from './publicUtils'
import { webWorkerPostPlugin } from './plugins/worker'
import { getHookHandler } from './plugins'
import { Environment } from './environment'
import type { Plugin, PluginContext } from './plugin'

export interface BuildEnvironmentOptions {
/**
Expand Down Expand Up @@ -535,6 +534,7 @@ export async function build(
function resolveConfigToBuild(
inlineConfig: InlineConfig = {},
patchConfig?: (config: ResolvedConfig) => void,
patchPlugins?: (plugins: Plugin[]) => void,
) {
return resolveConfig(
inlineConfig,
Expand All @@ -543,6 +543,7 @@ function resolveConfigToBuild(
'production',
false,
patchConfig,
patchPlugins,
)
}

Expand Down Expand Up @@ -1125,7 +1126,6 @@ function wrapEnvironmentLoad(
return fn.call(
injectEnvironmentInContext(this, environment),
id,
// @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
injectSsrFlag(args[0], environment),
)
}
Expand All @@ -1152,7 +1152,6 @@ function wrapEnvironmentTransform(
injectEnvironmentInContext(this, environment),
code,
importer,
// @ts-expect-error: Receiving options param to be future-proof if Rollup adds it
injectSsrFlag(args[0], environment),
)
}
Expand All @@ -1167,8 +1166,8 @@ function wrapEnvironmentTransform(
}
}

function injectEnvironmentInContext(
context: RollupPluginContext,
function injectEnvironmentInContext<Context extends PluginContext>(
context: Context,
environment?: BuildEnvironment,
) {
return new Proxy(context, {
Expand Down Expand Up @@ -1479,8 +1478,18 @@ export async function createBuilder(
let environmentConfig = config
if (!config.builder.sharedConfigBuild) {
const patchConfig = (resolved: ResolvedConfig) => {
// Until the ecosystem updates to use `environment.options.build` instead of `config.build`,
// we need to make override `config.build` for the current environment.
// We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
// remove the default values that shouldn't be used at all once the config is resolved
;(resolved.build as ResolvedBuildOptions) = {
...resolved.environments[name].build,
lib: false,
}
}
const patchPlugins = (resolvedPlugins: Plugin[]) => {
// Force opt-in shared plugins
const environmentPlugins = [...resolved.plugins]
const environmentPlugins = [...resolvedPlugins]
let validMixedPlugins = true
for (let i = 0; i < environmentPlugins.length; i++) {
const environmentPlugin = environmentPlugins[i]
Expand All @@ -1497,18 +1506,16 @@ export async function createBuilder(
}
}
if (validMixedPlugins) {
;(resolved.plugins as Plugin[]) = environmentPlugins
}
// Until the ecosystem updates to use `environment.options.build` instead of `config.build`,
// we need to make override `config.build` for the current environment.
// We can deprecate `config.build` in ResolvedConfig and push everyone to upgrade, and later
// remove the default values that shouldn't be used at all once the config is resolved
;(resolved.build as ResolvedBuildOptions) = {
...resolved.environments[name].build,
lib: false,
for (let i = 0; i < environmentPlugins.length; i++) {
resolvedPlugins[i] = environmentPlugins[i]
}
}
}
environmentConfig = await resolveConfigToBuild(inlineConfig, patchConfig)
environmentConfig = await resolveConfigToBuild(
inlineConfig,
patchConfig,
patchPlugins,
)
}

const environment = await createEnvironment(name, environmentConfig)
Expand Down
14 changes: 12 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ export async function resolveConfig(
defaultNodeEnv = 'development',
isPreview = false,
patchConfig: ((config: ResolvedConfig) => void) | undefined = undefined,
patchPlugins: ((plugins: Plugin[]) => void) | undefined = undefined,
): Promise<ResolvedConfig> {
let config = inlineConfig
let configFileDependencies: string[] = []
Expand Down Expand Up @@ -1284,15 +1285,24 @@ export async function resolveConfig(
...config,
...resolved,
}
;(resolved.plugins as Plugin[]) = await resolvePlugins(

// Backward compatibility hook, modify the resolved config before it is used
// to create inernal plugins. For example, `config.build.ssr`. Once we rework
// internal plugins to use environment.options, we can remove the dual
// patchConfig/patchPlugins and have a single patchConfig before configResolved
// gets called
patchConfig?.(resolved)

const resolvedPlugins = await resolvePlugins(
resolved,
prePlugins,
normalPlugins,
postPlugins,
)

// Backward compatibility hook used in builder
patchConfig?.(resolved)
patchPlugins?.(resolvedPlugins)
;(resolved.plugins as Plugin[]) = resolvedPlugins

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

Expand Down

0 comments on commit 00079da

Please sign in to comment.