Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(v6): reproduce custom environment preload injection #16541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
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
@@ -0,0 +1 @@
export const hello = 'hello'
@@ -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
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
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