Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
WatcherOptions,
} from 'rolldown'
import { viteLoadFallbackPlugin as nativeLoadFallbackPlugin } from 'rolldown/experimental'
import { esmExternalRequirePlugin } from 'rolldown/plugins'
import type { EsbuildTarget } from '#types/internal/esbuildOptions'
import type { RollupCommonJSOptions } from '#dep-types/commonjs'
import type { RollupDynamicImportVarsOptions } from '#dep-types/dynamicImportVars'
Expand Down Expand Up @@ -420,6 +421,11 @@ export function resolveBuildEnvironmentOptions(
logger: Logger,
consumer: 'client' | 'server' | undefined,
isBundledDev: boolean,
// Webworker SSR should not use Node platform — createRequire(import.meta.url)
// crashes in worker runtimes where import.meta.url is undefined. We use 'browser'
// rather than 'neutral' because it aligns with Vite's resolve conditions for
// webworker targets (defaultClientConditions + worker).
Comment thread
jmalmo marked this conversation as resolved.
Outdated
isSsrTargetWebworkerEnvironment?: boolean,
): ResolvedBuildEnvironmentOptions {
const deprecatedPolyfillModulePreload = raw.polyfillModulePreload
const { polyfillModulePreload, ...rest } = raw
Expand Down Expand Up @@ -451,7 +457,10 @@ export function resolveBuildEnvironmentOptions(
)
setupRollupOptionCompat(merged, 'build')
merged.rolldownOptions = {
platform: consumer === 'server' ? 'node' : 'browser',
platform:
consumer === 'server' && !isSsrTargetWebworkerEnvironment
? 'node'
: 'browser',
Comment thread
jmalmo marked this conversation as resolved.
Outdated
...merged.rolldownOptions,
}

Expand Down Expand Up @@ -654,6 +663,13 @@ export function resolveRolldownOptions(
environment.name === 'ssr' &&
environment.getTopLevelConfig().ssr?.target === 'webworker'

// For webworker SSR with platform: 'browser', external CJS require() calls
// need to be converted to ESM imports since createRequire is not available.
// Rolldown's esmExternalRequirePlugin handles this conversion.
Comment thread
jmalmo marked this conversation as resolved.
Outdated
if (isSsrTargetWebworkerEnvironment) {
plugins.push(esmExternalRequirePlugin())
}

const buildOutputOptions = (output: OutputOptions = {}): OutputOptions => {
// @ts-expect-error See https://github.com/vitejs/vite/issues/5812#issuecomment-984345618
if (output.output) {
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ function resolveEnvironmentOptions(
logger,
consumer,
isBundledDev,
isSsrTargetWebworkerEnvironment,
),
plugins: undefined!, // to be resolved later
// will be set by `setOptimizeDepsPluginNames` later
Expand Down
6 changes: 6 additions & 0 deletions playground/ssr-webworker/__tests__/ssr-webworker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ test('supports nodejs_compat', async () => {
)
})

test.runIf(isBuild)('build output does not contain createRequire', async () => {
const workerContent = findAssetFile(/entry-worker/, 'worker', '')
expect(workerContent).toBeDefined()
expect(workerContent).not.toContain('createRequire')
})

test.runIf(isBuild)('codeSplitting: false', () => {
const dynamicJsContent = findAssetFile(/dynamic-[-\w]+\.js/, 'worker')
expect(dynamicJsContent).toBeUndefined()
Expand Down