Skip to content
Open
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
41 changes: 41 additions & 0 deletions packages/rolldown/src/node/rolldown/__tests__/build-runner.test.ts
Original file line number Diff line number Diff line change
@@ -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' },
})
})
})
9 changes: 8 additions & 1 deletion packages/rolldown/src/node/rolldown/build-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}
}

Expand Down