Skip to content

Commit 9f740e2

Browse files
committed
wip: test
1 parent e3b3ca5 commit 9f740e2

File tree

7 files changed

+100
-70
lines changed

7 files changed

+100
-70
lines changed

packages/vite/src/node/build.ts

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import type {
1717
RolldownBuild,
1818
RolldownOptions,
1919
RolldownOutput,
20+
RolldownWatcher,
2021
RollupError,
2122
RollupLog,
22-
// RollupWatcher,
2323
WarningHandlerWithDefault,
24-
// WatcherOptions,
2524
} from 'rolldown'
2625
import {
2726
loadFallbackPlugin as nativeLoadFallbackPlugin,
@@ -71,9 +70,11 @@ import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
7170
import { buildLoadFallbackPlugin } from './plugins/loadFallback'
7271
import { findNearestMainPackageData, findNearestPackageData } from './packages'
7372
import type { PackageCache } from './packages'
73+
import type { WatcherOptions } from './watch'
7474
import {
75+
convertToNotifyOptions,
7576
getResolvedOutDirs,
76-
// resolveChokidarOptions,
77+
resolveChokidarOptions,
7778
resolveEmptyOutDir,
7879
} from './watch'
7980
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -280,7 +281,7 @@ export interface BuildEnvironmentOptions {
280281
* https://rollupjs.org/configuration-options/#watch
281282
* @default null
282283
*/
283-
// watch?: WatcherOptions | null
284+
watch?: WatcherOptions | null
284285
/**
285286
* create the Build Environment instance
286287
*/
@@ -537,7 +538,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
537538
*/
538539
export async function build(
539540
inlineConfig: InlineConfig = {},
540-
): Promise<RolldownOutput | RolldownOutput[] /* | RollupWatcher */> {
541+
): Promise<RolldownOutput | RolldownOutput[] | RolldownWatcher> {
541542
const builder = await createBuilder(inlineConfig, true)
542543
const environment = Object.values(builder.environments)[0]
543544
if (!environment) throw new Error('No environment found')
@@ -565,7 +566,7 @@ function resolveConfigToBuild(
565566
**/
566567
async function buildEnvironment(
567568
environment: BuildEnvironment,
568-
): Promise<RolldownOutput | RolldownOutput[] /* | RollupWatcher */> {
569+
): Promise<RolldownOutput | RolldownOutput[] | RolldownWatcher> {
569570
const { root, packageCache } = environment.config
570571
const options = environment.config.build
571572
const libOptions = options.lib
@@ -711,11 +712,11 @@ async function buildEnvironment(
711712
}
712713
}
713714

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

720721
const isSsrTargetWebworkerEnvironment =
721722
environment.name === 'ssr' &&
@@ -831,42 +832,46 @@ async function buildEnvironment(
831832
)
832833

833834
// watch file changes with rollup
834-
// if (options.watch) {
835-
// logger.info(colors.cyan(`\nwatching for file changes...`))
836-
837-
// const resolvedChokidarOptions = resolveChokidarOptions(
838-
// options.watch.chokidar,
839-
// resolvedOutDirs,
840-
// emptyOutDir,
841-
// environment.config.cacheDir,
842-
// )
843-
844-
// const { watch } = await import('rollup')
845-
// const watcher = watch({
846-
// ...rollupOptions,
847-
// output: normalizedOutputs,
848-
// watch: {
849-
// ...options.watch,
850-
// chokidar: resolvedChokidarOptions,
851-
// },
852-
// })
853-
854-
// watcher.on('event', (event) => {
855-
// if (event.code === 'BUNDLE_START') {
856-
// logger.info(colors.cyan(`\nbuild started...`))
857-
// if (options.write) {
858-
// prepareOutDir(resolvedOutDirs, emptyOutDir, environment)
859-
// }
860-
// } else if (event.code === 'BUNDLE_END') {
861-
// event.result.close()
862-
// logger.info(colors.cyan(`built in ${event.duration}ms.`))
863-
// } else if (event.code === 'ERROR') {
864-
// outputBuildError(event.error)
865-
// }
866-
// })
867-
868-
// return watcher
869-
// }
835+
if (options.watch) {
836+
logger.info(colors.cyan(`\nwatching for file changes...`))
837+
838+
const resolvedChokidarOptions = resolveChokidarOptions(
839+
// @ts-expect-error TODO: chokidar option does not exist in rolldown
840+
options.watch.chokidar,
841+
resolvedOutDirs,
842+
emptyOutDir,
843+
environment.config.cacheDir,
844+
)
845+
846+
const { watch } = await import('rolldown')
847+
const watcher = watch({
848+
...rollupOptions,
849+
output: normalizedOutputs,
850+
watch: {
851+
...options.watch,
852+
// TODO: convert chokidar options more precisely
853+
// TODO: support more chokidar options
854+
notify: convertToNotifyOptions(resolvedChokidarOptions),
855+
},
856+
})
857+
858+
watcher.on('event', (event) => {
859+
if (event.code === 'BUNDLE_START') {
860+
logger.info(colors.cyan(`\nbuild started...`))
861+
if (options.write) {
862+
prepareOutDir(resolvedOutDirs, emptyOutDir, environment)
863+
}
864+
} else if (event.code === 'BUNDLE_END') {
865+
// FIXME: https://github.com/rolldown/rolldown/issues/4380
866+
// event.result.close()
867+
logger.info(colors.cyan(`built in ${event.duration}ms.`))
868+
} else if (event.code === 'ERROR') {
869+
outputBuildError(event.error)
870+
}
871+
})
872+
873+
return watcher
874+
}
870875

871876
// write or generate files with rolldown
872877
const { rolldown } = await import('rolldown')
@@ -1613,7 +1618,7 @@ export interface ViteBuilder {
16131618
buildApp(): Promise<void>
16141619
build(
16151620
environment: BuildEnvironment,
1616-
): Promise<RolldownOutput | RolldownOutput[] /* | RollupWatcher */>
1621+
): Promise<RolldownOutput | RolldownOutput[] | RolldownWatcher>
16171622
}
16181623

16191624
export interface BuilderOptions {

packages/vite/src/node/watch.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { EventEmitter } from 'node:events'
22
import path from 'node:path'
33
import type { FSWatcher, WatchOptions } from 'dep-types/chokidar'
4-
import type { OutputOptions } from 'rolldown'
4+
import type { InputOptions, OutputOptions } from 'rolldown'
55
import colors from 'picocolors'
66
import { escapePath } from 'tinyglobby'
77
import { withTrailingSlash } from '../shared/utils'
88
import { arraify, normalizePath } from './utils'
99
import type { Logger } from './logger'
1010

11+
// TODO: expose type from rolldown
12+
export type WatcherOptions = Exclude<InputOptions['watch'], false | undefined>
13+
1114
export function getResolvedOutDirs(
1215
root: string,
1316
outDir: string,
@@ -78,6 +81,15 @@ export function resolveChokidarOptions(
7881
return resolvedWatchOptions
7982
}
8083

84+
export function convertToNotifyOptions(
85+
options: WatchOptions | undefined,
86+
): WatcherOptions['notify'] {
87+
if (!options) return
88+
return {
89+
pollInterval: options.interval,
90+
}
91+
}
92+
8193
class NoopWatcher extends EventEmitter implements FSWatcher {
8294
constructor(public options: WatchOptions) {
8395
super()

playground/assets/__tests__/assets.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,7 @@ test.runIf(isBuild)('manifest', async () => {
633633
}
634634
})
635635

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

playground/assets/vite.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export default defineConfig({
1515
outDir: 'dist/foo',
1616
assetsInlineLimit: 8000, // 8 kB
1717
manifest: true,
18-
// watch: {},
18+
watch: {},
1919
},
2020
})

playground/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"devDependencies": {
1010
"convert-source-map": "^2.0.0",
1111
"css-color-names": "^1.0.1",
12-
"kill-port": "^1.6.1"
12+
"kill-port": "^1.6.1",
13+
"rolldown": "1.0.0-beta.8-commit.2686eb1"
1314
}
1415
}

playground/vitestSetup.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
InlineConfig,
88
Logger,
99
PluginOption,
10-
// ResolvedConfig,
10+
ResolvedConfig,
1111
UserConfig,
1212
ViteDevServer,
1313
} from 'vite'
@@ -20,10 +20,12 @@ import {
2020
preview,
2121
} from 'vite'
2222
import type { Browser, Page } from 'playwright-chromium'
23-
import type { RollupError, RollupWatcher, RollupWatcherEvent } from 'rollup'
23+
import type { RolldownWatcher, RollupError } from 'rolldown'
2424
import type { RunnerTestFile } from 'vitest'
2525
import { beforeAll, inject } from 'vitest'
2626

27+
type RolldownWatcherEvent = any // TODO: export type from rolldown
28+
2729
// #region env
2830

2931
export const workspaceRoot = path.resolve(__dirname, '../')
@@ -70,7 +72,7 @@ export const browserErrors: Error[] = []
7072
export let page: Page = undefined!
7173
export let browser: Browser = undefined!
7274
export let viteTestUrl: string = ''
73-
export const watcher: RollupWatcher | undefined = undefined
75+
export let watcher: RolldownWatcher | undefined = undefined
7476

7577
export function setViteUrl(url: string): void {
7678
viteTestUrl = url
@@ -242,12 +244,12 @@ export async function startDefaultServe(): Promise<void> {
242244
await page.goto(viteTestUrl)
243245
} else {
244246
process.env.VITE_INLINE = 'inline-build'
245-
// let resolvedConfig: ResolvedConfig
247+
let resolvedConfig: ResolvedConfig
246248
// determine build watch
247249
const resolvedPlugin: () => PluginOption = () => ({
248250
name: 'vite-plugin-watcher',
249-
configResolved(_config) {
250-
// resolvedConfig = config
251+
configResolved(config) {
252+
resolvedConfig = config
251253
},
252254
})
253255
const buildConfig = mergeConfig(
@@ -260,13 +262,13 @@ export async function startDefaultServe(): Promise<void> {
260262
const builder = await createBuilder(buildConfig)
261263
await builder.buildApp()
262264
} else {
263-
/* const rollupOutput = */ await build(buildConfig)
264-
// const isWatch = !!resolvedConfig!.build.watch
265-
// // in build watch,call startStaticServer after the build is complete
266-
// if (isWatch) {
267-
// watcher = rollupOutput as RollupWatcher
268-
// await notifyRebuildComplete(watcher)
269-
// }
265+
const rollupOutput = await build(buildConfig)
266+
const isWatch = !!resolvedConfig!.build.watch
267+
// in build watch,call startStaticServer after the build is complete
268+
if (isWatch) {
269+
watcher = rollupOutput as RolldownWatcher
270+
await notifyRebuildComplete(watcher)
271+
}
270272
if (buildConfig.__test__) {
271273
buildConfig.__test__()
272274
}
@@ -293,19 +295,27 @@ export async function startDefaultServe(): Promise<void> {
293295
* Send the rebuild complete message in build watch
294296
*/
295297
export async function notifyRebuildComplete(
296-
watcher: RollupWatcher,
297-
): Promise<RollupWatcher> {
298+
watcher: RolldownWatcher,
299+
): Promise<RolldownWatcher> {
298300
let resolveFn: undefined | (() => void)
299-
const callback = (event: RollupWatcherEvent): void => {
301+
const callback = (event: RolldownWatcherEvent): void => {
300302
if (event.code === 'END') {
301303
resolveFn?.()
304+
resolveFn = undefined // set to undefined instead of watcher.off for now
302305
}
303306
}
304307
watcher.on('event', callback)
305308
await new Promise<void>((resolve) => {
306309
resolveFn = resolve
307310
})
308-
return watcher.off('event', callback)
311+
312+
// During tests we edit the files too fast and sometimes chokidar
313+
// misses change events, so wait 100ms for consistency
314+
await new Promise<void>((resolve) => setTimeout(resolve, 100))
315+
316+
// TODO: not supported yet
317+
// return watcher.off('event', callback)
318+
return watcher
309319
}
310320

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

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)