Skip to content

Commit

Permalink
feat: add isSsrTargetWebWorker flag to configEnvironment hook (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 14, 2024
1 parent d9be921 commit 3f5fab0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,13 +747,13 @@ function resolveEnvironmentOptions(
environmentName: string,
// Backward compatibility
skipSsrTransform?: boolean,
ssrTargetWebworker?: boolean,
isSsrTargetWebworkerSet?: boolean,
): ResolvedEnvironmentOptions {
const isClientEnvironment = environmentName === 'client'
const consumer =
options.consumer ?? (isClientEnvironment ? 'client' : 'server')
const isSsrTargetWebworkerEnvironment =
ssrTargetWebworker && environmentName === 'ssr'
isSsrTargetWebworkerSet && environmentName === 'ssr'
const resolve = resolveEnvironmentResolveOptions(
options.resolve,
alias,
Expand Down Expand Up @@ -1145,7 +1145,12 @@ export async function resolveConfig(
)
}

await runConfigEnvironmentHook(config.environments, userPlugins, configEnv)
await runConfigEnvironmentHook(
config.environments,
userPlugins,
configEnv,
config.ssr?.target === 'webworker',
)

const resolvedDefaultResolve = resolveResolveOptions(config.resolve, logger)

Expand Down Expand Up @@ -1916,14 +1921,18 @@ async function runConfigEnvironmentHook(
environments: Record<string, EnvironmentOptions>,
plugins: Plugin[],
configEnv: ConfigEnv,
isSsrTargetWebworkerSet: boolean,
): Promise<void> {
const environmentNames = Object.keys(environments)
for (const p of getSortedPluginsByHook('configEnvironment', plugins)) {
const hook = p.configEnvironment
const handler = getHookHandler(hook)
if (handler) {
for (const name of environmentNames) {
const res = await handler(name, environments[name], configEnv)
const res = await handler(name, environments[name], {
...configEnv,
isSsrTargetWebworker: isSsrTargetWebworkerSet && name === 'ssr',
})
if (res) {
environments[name] = mergeConfig(environments[name], res)
}
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
this: void,
name: string,
config: EnvironmentOptions,
env: ConfigEnv,
env: ConfigEnv & {
/**
* Whether this environment is SSR environment and `ssr.target` is set to `'webworker'`.
* Only intended to be used for backward compatibility.
*/
isSsrTargetWebworker?: boolean
},
) =>
| EnvironmentOptions
| null
Expand Down

0 comments on commit 3f5fab0

Please sign in to comment.