@@ -17,7 +17,7 @@ import type {
17
17
RollupLog ,
18
18
RollupOptions ,
19
19
RollupOutput ,
20
- // RollupWatcher ,
20
+ watch as rolldownWatch ,
21
21
// WatcherOptions,
22
22
} from 'rolldown'
23
23
import {
@@ -69,7 +69,7 @@ import { findNearestPackageData } from './packages'
69
69
import type { PackageCache } from './packages'
70
70
import {
71
71
getResolvedOutDirs ,
72
- // resolveChokidarOptions,
72
+ resolveChokidarOptions ,
73
73
resolveEmptyOutDir ,
74
74
} from './watch'
75
75
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -83,6 +83,9 @@ import {
83
83
import type { MinimalPluginContext , Plugin , PluginContext } from './plugin'
84
84
import type { RollupPluginHooks } from './typeUtils'
85
85
86
+ export type RollupWatcher = Awaited < ReturnType < typeof rolldownWatch > >
87
+ type WatcherOptions = { _ : never }
88
+
86
89
export interface BuildEnvironmentOptions {
87
90
/**
88
91
* Compatibility transform target. The transform is performed with esbuild
@@ -276,7 +279,7 @@ export interface BuildEnvironmentOptions {
276
279
* https://rollupjs.org/configuration-options/#watch
277
280
* @default null
278
281
*/
279
- // watch?: WatcherOptions | null
282
+ watch ?: WatcherOptions | null
280
283
/**
281
284
* create the Build Environment instance
282
285
*/
@@ -544,7 +547,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
544
547
*/
545
548
export async function build (
546
549
inlineConfig : InlineConfig = { } ,
547
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ > {
550
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
548
551
const builder = await createBuilder ( inlineConfig , true )
549
552
const environment = Object . values ( builder . environments ) [ 0 ]
550
553
if ( ! environment ) throw new Error ( 'No environment found' )
@@ -572,7 +575,7 @@ function resolveConfigToBuild(
572
575
**/
573
576
async function buildEnvironment (
574
577
environment : BuildEnvironment ,
575
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ > {
578
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
576
579
const { root, packageCache } = environment . config
577
580
const options = environment . config . build
578
581
const libOptions = options . lib
@@ -708,11 +711,11 @@ async function buildEnvironment(
708
711
}
709
712
}
710
713
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
+ }
716
719
717
720
let bundle : RollupBuild | undefined
718
721
let startTime : number | undefined
@@ -816,42 +819,42 @@ async function buildEnvironment(
816
819
)
817
820
818
821
// 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
+ }
855
858
856
859
// write or generate files with rollup
857
860
const { rolldown } = await import ( 'rolldown' )
@@ -1553,7 +1556,7 @@ export interface ViteBuilder {
1553
1556
buildApp ( ) : Promise < void >
1554
1557
build (
1555
1558
environment : BuildEnvironment ,
1556
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ >
1559
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher >
1557
1560
}
1558
1561
1559
1562
export interface BuilderOptions {
0 commit comments