From b39e69638b3e2e658ff6712be83b549b28103c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sat, 30 Nov 2024 05:58:10 +0900 Subject: [PATCH] fix(ssr): set `ssr.target: 'webworker'` defaults as fallback (#18827) --- packages/vite/src/node/build.ts | 26 +++++++++----------------- packages/vite/src/node/config.ts | 1 - 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index e5d80841ad4289..8dd3df266c58f3 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -390,8 +390,6 @@ export function resolveBuildEnvironmentOptions( raw: BuildEnvironmentOptions, logger: Logger, consumer: 'client' | 'server' | undefined, - // Backward compatibility - isSsrTargetWebworkerEnvironment?: boolean, ): ResolvedBuildEnvironmentOptions { const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload const { polyfillModulePreload, ...rest } = raw @@ -453,19 +451,6 @@ export function resolveBuildEnvironmentOptions( }, } - if (isSsrTargetWebworkerEnvironment) { - resolved.rollupOptions ??= {} - resolved.rollupOptions.output ??= {} - const output = resolved.rollupOptions.output - for (const out of arraify(output)) { - out.entryFileNames ??= `[name].js` - out.chunkFileNames ??= `[name]-[hash].js` - const input = resolved.rollupOptions.input - out.inlineDynamicImports ??= - !input || typeof input === 'string' || Object.keys(input).length === 1 - } - } - return resolved } @@ -677,6 +662,10 @@ async function buildEnvironment( logger.error(e.message, { error: e }) } + const isSsrTargetWebworkerEnvironment = + environment.name === 'ssr' && + environment.getTopLevelConfig().ssr?.target === 'webworker' + let bundle: RollupBuild | undefined let startTime: number | undefined try { @@ -706,7 +695,7 @@ async function buildEnvironment( const format = output.format || 'es' const jsExt = - environment.config.consumer === 'server' || libOptions + (ssr && !isSsrTargetWebworkerEnvironment) || libOptions ? resolveOutputJsExtension( format, findNearestPackageData(root, packageCache)?.data.type, @@ -744,7 +733,10 @@ async function buildEnvironment( ? `[name].[ext]` : path.posix.join(options.assetsDir, `[name]-[hash].[ext]`), inlineDynamicImports: - output.format === 'umd' || output.format === 'iife', + output.format === 'umd' || + output.format === 'iife' || + (isSsrTargetWebworkerEnvironment && + (typeof input === 'string' || Object.keys(input).length === 1)), ...output, } } diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index de7c16b559c427..c2c3bda7a16147 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -782,7 +782,6 @@ function resolveEnvironmentOptions( options.build ?? {}, logger, consumer, - isSsrTargetWebworkerEnvironment, ), } }