Skip to content

Commit 0684f20

Browse files
committed
feat: use Rolldown's watch API
1 parent 84b75a3 commit 0684f20

File tree

4 files changed

+67
-60
lines changed

4 files changed

+67
-60
lines changed

packages/vite/src/node/build.ts

+50-47
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
RollupLog,
1818
RollupOptions,
1919
RollupOutput,
20-
// RollupWatcher,
20+
watch as rolldownWatch,
2121
// WatcherOptions,
2222
} from 'rolldown'
2323
import {
@@ -69,7 +69,7 @@ import { findNearestPackageData } from './packages'
6969
import type { PackageCache } from './packages'
7070
import {
7171
getResolvedOutDirs,
72-
// resolveChokidarOptions,
72+
resolveChokidarOptions,
7373
resolveEmptyOutDir,
7474
} from './watch'
7575
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -83,6 +83,9 @@ import {
8383
import type { MinimalPluginContext, Plugin, PluginContext } from './plugin'
8484
import type { RollupPluginHooks } from './typeUtils'
8585

86+
export type RollupWatcher = Awaited<ReturnType<typeof rolldownWatch>>
87+
type WatcherOptions = { _: never }
88+
8689
export interface BuildEnvironmentOptions {
8790
/**
8891
* Compatibility transform target. The transform is performed with esbuild
@@ -276,7 +279,7 @@ export interface BuildEnvironmentOptions {
276279
* https://rollupjs.org/configuration-options/#watch
277280
* @default null
278281
*/
279-
// watch?: WatcherOptions | null
282+
watch?: WatcherOptions | null
280283
/**
281284
* create the Build Environment instance
282285
*/
@@ -544,7 +547,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
544547
*/
545548
export async function build(
546549
inlineConfig: InlineConfig = {},
547-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */> {
550+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
548551
const builder = await createBuilder(inlineConfig, true)
549552
const environment = Object.values(builder.environments)[0]
550553
if (!environment) throw new Error('No environment found')
@@ -572,7 +575,7 @@ function resolveConfigToBuild(
572575
**/
573576
async function buildEnvironment(
574577
environment: BuildEnvironment,
575-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */> {
578+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
576579
const { root, packageCache } = environment.config
577580
const options = environment.config.build
578581
const libOptions = options.lib
@@ -708,11 +711,11 @@ async function buildEnvironment(
708711
}
709712
}
710713

711-
// const outputBuildError = (e: RollupError) => {
712-
// enhanceRollupError(e)
713-
// clearLine()
714-
// logger.error(e.message, { error: e })
715-
// }
714+
const outputBuildError = (e: RollupError) => {
715+
enhanceRollupError(e)
716+
clearLine()
717+
logger.error(e.message, { error: e })
718+
}
716719

717720
let bundle: RollupBuild | undefined
718721
let startTime: number | undefined
@@ -816,42 +819,42 @@ async function buildEnvironment(
816819
)
817820

818821
// watch file changes with rollup
819-
// if (options.watch) {
820-
// logger.info(colors.cyan(`\nwatching for file changes...`))
821-
822-
// const resolvedChokidarOptions = resolveChokidarOptions(
823-
// options.watch.chokidar,
824-
// resolvedOutDirs,
825-
// emptyOutDir,
826-
// environment.config.cacheDir,
827-
// )
828-
829-
// const { watch } = await import('rolldown')
830-
// const watcher = watch({
831-
// ...rollupOptions,
832-
// output: normalizedOutputs,
833-
// watch: {
834-
// ...options.watch,
835-
// chokidar: resolvedChokidarOptions,
836-
// },
837-
// })
838-
839-
// watcher.on('event', (event) => {
840-
// if (event.code === 'BUNDLE_START') {
841-
// logger.info(colors.cyan(`\nbuild started...`))
842-
// if (options.write) {
843-
// prepareOutDir(resolvedOutDirs, emptyOutDir, environment)
844-
// }
845-
// } else if (event.code === 'BUNDLE_END') {
846-
// event.result.close()
847-
// logger.info(colors.cyan(`built in ${event.duration}ms.`))
848-
// } else if (event.code === 'ERROR') {
849-
// outputBuildError(event.error)
850-
// }
851-
// })
852-
853-
// return watcher
854-
// }
822+
if (options.watch) {
823+
logger.info(colors.cyan(`\nwatching for file changes...`))
824+
825+
const resolvedChokidarOptions = resolveChokidarOptions(
826+
{}, // options.watch.chokidar,
827+
resolvedOutDirs,
828+
emptyOutDir,
829+
environment.config.cacheDir,
830+
)
831+
832+
const { watch } = await import('rolldown')
833+
const watcher = await watch({
834+
...rollupOptions,
835+
output: normalizedOutputs[0], // normalizedOutputs,
836+
watch: {
837+
...options.watch,
838+
chokidar: resolvedChokidarOptions,
839+
},
840+
})
841+
842+
watcher.on('event', (event) => {
843+
if (event.code === 'BUNDLE_START') {
844+
logger.info(colors.cyan(`\nbuild started...`))
845+
if (options.write) {
846+
prepareOutDir(resolvedOutDirs, emptyOutDir, environment)
847+
}
848+
} else if (event.code === 'BUNDLE_END') {
849+
// event.result.close()
850+
logger.info(colors.cyan(`built in ${event.duration}ms.`))
851+
} else if (event.code === 'ERROR') {
852+
outputBuildError(event.error)
853+
}
854+
})
855+
856+
return watcher
857+
}
855858

856859
// write or generate files with rollup
857860
const { rolldown } = await import('rolldown')
@@ -1553,7 +1556,7 @@ export interface ViteBuilder {
15531556
buildApp(): Promise<void>
15541557
build(
15551558
environment: BuildEnvironment,
1556-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */>
1559+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher>
15571560
}
15581561

15591562
export interface BuilderOptions {

packages/vite/src/node/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export { perEnvironmentPlugin } from './plugin'
1212
export { perEnvironmentState } from './environment'
1313
export { createServer } from './server'
1414
export { preview } from './preview'
15-
export { build, createBuilder } from './build'
15+
export { build, createBuilder, type RollupWatcher } from './build'
1616

1717
export { optimizeDeps } from './optimizer'
1818
export { createIdResolver } from './idResolver'

playground/assets/__tests__/assets.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,7 @@ test.runIf(isBuild)('manifest', async () => {
550550
}
551551
})
552552

553-
// TODO: rolldown does not support rebuild
554-
describe.runIf(isBuild).skip('css and assets in css in build watch', () => {
553+
describe.runIf(isBuild)('css and assets in css in build watch', () => {
555554
test('css will not be lost and css does not contain undefined', async () => {
556555
editFile('index.html', (code) => code.replace('Assets', 'assets'))
557556
await notifyRebuildComplete(watcher)

playground/vitestSetup.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
Logger,
99
PluginOption,
1010
ResolvedConfig,
11+
RollupWatcher,
1112
UserConfig,
1213
ViteDevServer,
1314
} from 'vite'
@@ -20,7 +21,7 @@ import {
2021
preview,
2122
} from 'vite'
2223
import type { Browser, Page } from 'playwright-chromium'
23-
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
24+
import type { RollupError, RollupWatcherEvent } from 'rollup'
2425
import type { RunnerTestFile } from 'vitest'
2526
import { beforeAll, inject } from 'vitest'
2627

@@ -72,7 +73,7 @@ export let resolvedConfig: ResolvedConfig = undefined!
7273
export let page: Page = undefined!
7374
export let browser: Browser = undefined!
7475
export let viteTestUrl: string = ''
75-
export const watcher: RollupWatcher | undefined = undefined
76+
export let watcher: RollupWatcher | undefined = undefined
7677

7778
export function setViteUrl(url: string): void {
7879
viteTestUrl = url
@@ -273,13 +274,14 @@ export async function startDefaultServe(): Promise<void> {
273274
const builder = await createBuilder(buildConfig)
274275
await builder.buildApp()
275276
} else {
276-
/* const rollupOutput = */ await build(buildConfig)
277-
// const isWatch = !!resolvedConfig!.build.watch
278-
// // in build watch,call startStaticServer after the build is complete
279-
// if (isWatch) {
280-
// watcher = rollupOutput as RollupWatcher
281-
// await notifyRebuildComplete(watcher)
282-
// }
277+
const rollupOutput = await build(buildConfig)
278+
const isWatch = !!resolvedConfig!.build.watch
279+
// in build watch,call startStaticServer after the build is complete
280+
if (isWatch) {
281+
watcher = rollupOutput as RollupWatcher
282+
await notifyRebuildComplete(watcher)
283+
await new Promise<void>((resolve) => setTimeout(resolve, 1000))
284+
}
283285
if (buildConfig.__test__) {
284286
buildConfig.__test__()
285287
}
@@ -315,7 +317,10 @@ export async function notifyRebuildComplete(
315317
await new Promise<void>((resolve) => {
316318
resolveFn = resolve
317319
})
318-
return watcher.off('event', callback)
320+
// During tests we edit the files too fast and sometimes chokidar
321+
// misses change events, so wait 100ms for consistency
322+
await new Promise<void>((resolve) => setTimeout(resolve, 100))
323+
return watcher // watcher.off('event', callback)
319324
}
320325

321326
export function createInMemoryLogger(logs: string[]): Logger {

0 commit comments

Comments
 (0)