Skip to content

Commit 82412fc

Browse files
committed
feat: use Rolldown's watch API
1 parent f646104 commit 82412fc

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
@@ -14,7 +14,7 @@ import type {
1414
RollupLog,
1515
RollupOptions,
1616
RollupOutput,
17-
// RollupWatcher,
17+
watch as rolldownWatch,
1818
// WatcherOptions,
1919
} from 'rolldown'
2020
import {
@@ -65,7 +65,7 @@ import { findNearestPackageData } from './packages'
6565
import type { PackageCache } from './packages'
6666
import {
6767
getResolvedOutDirs,
68-
// resolveChokidarOptions,
68+
resolveChokidarOptions,
6969
resolveEmptyOutDir,
7070
} from './watch'
7171
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -79,6 +79,9 @@ import {
7979
import type { MinimalPluginContext, Plugin, PluginContext } from './plugin'
8080
import type { RollupPluginHooks } from './typeUtils'
8181

82+
export type RollupWatcher = Awaited<ReturnType<typeof rolldownWatch>>
83+
type WatcherOptions = { _: never }
84+
8285
export interface BuildEnvironmentOptions {
8386
/**
8487
* Compatibility transform target. The transform is performed with esbuild
@@ -272,7 +275,7 @@ export interface BuildEnvironmentOptions {
272275
* https://rollupjs.org/configuration-options/#watch
273276
* @default null
274277
*/
275-
// watch?: WatcherOptions | null
278+
watch?: WatcherOptions | null
276279
/**
277280
* create the Build Environment instance
278281
*/
@@ -536,7 +539,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
536539
*/
537540
export async function build(
538541
inlineConfig: InlineConfig = {},
539-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */> {
542+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
540543
const builder = await createBuilder(inlineConfig, true)
541544
const environment = Object.values(builder.environments)[0]
542545
if (!environment) throw new Error('No environment found')
@@ -564,7 +567,7 @@ function resolveConfigToBuild(
564567
**/
565568
async function buildEnvironment(
566569
environment: BuildEnvironment,
567-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */> {
570+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher> {
568571
const { root, packageCache } = environment.config
569572
const options = environment.config.build
570573
const libOptions = options.lib
@@ -699,11 +702,11 @@ async function buildEnvironment(
699702
}
700703
}
701704

702-
// const outputBuildError = (e: RollupError) => {
703-
// enhanceRollupError(e)
704-
// clearLine()
705-
// logger.error(e.message, { error: e })
706-
// }
705+
const outputBuildError = (e: RollupError) => {
706+
enhanceRollupError(e)
707+
clearLine()
708+
logger.error(e.message, { error: e })
709+
}
707710

708711
let bundle: RollupBuild | undefined
709712
let startTime: number | undefined
@@ -806,42 +809,42 @@ async function buildEnvironment(
806809
)
807810

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

846849
// write or generate files with rollup
847850
const { rolldown } = await import('rolldown')
@@ -1502,7 +1505,7 @@ export interface ViteBuilder {
15021505
buildApp(): Promise<void>
15031506
build(
15041507
environment: BuildEnvironment,
1505-
): Promise<RollupOutput | RollupOutput[] /* | RollupWatcher */>
1508+
): Promise<RollupOutput | RollupOutput[] | RollupWatcher>
15061509
}
15071510

15081511
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'), true)
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)