Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 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,7 @@ export function resolveBuildEnvironmentOptions(
logger: Logger,
consumer: 'client' | 'server' | undefined,
isBundledDev: boolean,
isSsrTargetWebworkerEnvironment?: boolean,
): ResolvedBuildEnvironmentOptions {
const deprecatedPolyfillModulePreload = raw.polyfillModulePreload
const { polyfillModulePreload, ...rest } = raw
Expand Down Expand Up @@ -451,7 +453,10 @@ export function resolveBuildEnvironmentOptions(
)
setupRollupOptionCompat(merged, 'build')
merged.rolldownOptions = {
platform: consumer === 'server' ? 'node' : 'browser',
platform:
consumer === 'client' || isSsrTargetWebworkerEnvironment
? 'browser'
: 'node',
...merged.rolldownOptions,
}

Expand Down Expand Up @@ -654,6 +659,12 @@ 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.
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