diff --git a/packages/rolldown/src/node/rolldown/__tests__/build-runner.test.ts b/packages/rolldown/src/node/rolldown/__tests__/build-runner.test.ts new file mode 100644 index 000000000..406fbe624 --- /dev/null +++ b/packages/rolldown/src/node/rolldown/__tests__/build-runner.test.ts @@ -0,0 +1,41 @@ +import type { ViteDevToolsNodeContext } from '@vitejs/devtools-kit' +import { describe, expect, it, vi } from 'vitest' +import { getBuildCommand, ROLLDOWN_DEVTOOLS_ENV, startBuild } from '../build-runner' + +function fakeContext() { + const startChildProcess = vi.fn(async (_exec: unknown, meta: { id: string }) => ({ + ...meta, + status: 'running', + terminate: vi.fn(async () => {}), + getResult: vi.fn(), + })) + const context = { + cwd: '/project', + terminals: { sessions: new Map(), startChildProcess }, + } as unknown as ViteDevToolsNodeContext + return { context, startChildProcess } +} + +describe('rolldown build runner', () => { + it('builds for production even though the dev server runs with NODE_ENV=development', () => { + const { context } = fakeContext() + // The dev server sets `NODE_ENV=development`, and the spawned build + // inherits the dev server's env. Vite keeps an already-set `NODE_ENV`, + // so the build must override it to get a production bundle. + expect(getBuildCommand(context)).toEqual({ + command: 'vite', + args: ['build'], + cwd: '/project', + env: { NODE_ENV: 'production', [ROLLDOWN_DEVTOOLS_ENV]: 'true' }, + }) + }) + + it('spawns the build with the production NODE_ENV', async () => { + const { context, startChildProcess } = fakeContext() + await startBuild(context) + expect(startChildProcess).toHaveBeenCalledTimes(1) + expect(startChildProcess.mock.calls[0]![0]).toMatchObject({ + env: { NODE_ENV: 'production' }, + }) + }) +}) diff --git a/packages/rolldown/src/node/rolldown/build-runner.ts b/packages/rolldown/src/node/rolldown/build-runner.ts index 3d4e18663..e4a6eba00 100644 --- a/packages/rolldown/src/node/rolldown/build-runner.ts +++ b/packages/rolldown/src/node/rolldown/build-runner.ts @@ -43,7 +43,14 @@ export function getBuildCommand(context: ViteDevToolsNodeContext): DevToolsChild command: 'vite', args: ['build'], cwd: context.cwd ?? process.cwd(), - env: { [ROLLDOWN_DEVTOOLS_ENV]: 'true' }, + env: { + // The child inherits the dev server's env, where Vite has set + // `NODE_ENV=development`. `vite build` keeps an already-set `NODE_ENV`, + // which would keep every library's dev-only branches in the bundle, so + // pin what a plain `vite build` resolves to. + NODE_ENV: 'production', + [ROLLDOWN_DEVTOOLS_ENV]: 'true', + }, } }