@@ -14,7 +14,7 @@ import type {
14
14
RollupLog ,
15
15
RollupOptions ,
16
16
RollupOutput ,
17
- // RollupWatcher ,
17
+ watch as rolldownWatch ,
18
18
// WatcherOptions,
19
19
} from 'rolldown'
20
20
import {
@@ -65,7 +65,7 @@ import { findNearestPackageData } from './packages'
65
65
import type { PackageCache } from './packages'
66
66
import {
67
67
getResolvedOutDirs ,
68
- // resolveChokidarOptions,
68
+ resolveChokidarOptions ,
69
69
resolveEmptyOutDir ,
70
70
} from './watch'
71
71
import { completeSystemWrapPlugin } from './plugins/completeSystemWrap'
@@ -79,6 +79,9 @@ import {
79
79
import type { MinimalPluginContext , Plugin , PluginContext } from './plugin'
80
80
import type { RollupPluginHooks } from './typeUtils'
81
81
82
+ export type RollupWatcher = Awaited < ReturnType < typeof rolldownWatch > >
83
+ type WatcherOptions = { _ : never }
84
+
82
85
export interface BuildEnvironmentOptions {
83
86
/**
84
87
* Compatibility transform target. The transform is performed with esbuild
@@ -272,7 +275,7 @@ export interface BuildEnvironmentOptions {
272
275
* https://rollupjs.org/configuration-options/#watch
273
276
* @default null
274
277
*/
275
- // watch?: WatcherOptions | null
278
+ watch ?: WatcherOptions | null
276
279
/**
277
280
* create the Build Environment instance
278
281
*/
@@ -536,7 +539,7 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
536
539
*/
537
540
export async function build (
538
541
inlineConfig : InlineConfig = { } ,
539
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ > {
542
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
540
543
const builder = await createBuilder ( inlineConfig , true )
541
544
const environment = Object . values ( builder . environments ) [ 0 ]
542
545
if ( ! environment ) throw new Error ( 'No environment found' )
@@ -564,7 +567,7 @@ function resolveConfigToBuild(
564
567
**/
565
568
async function buildEnvironment (
566
569
environment : BuildEnvironment ,
567
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ > {
570
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher > {
568
571
const { root, packageCache } = environment . config
569
572
const options = environment . config . build
570
573
const libOptions = options . lib
@@ -699,11 +702,11 @@ async function buildEnvironment(
699
702
}
700
703
}
701
704
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
+ }
707
710
708
711
let bundle : RollupBuild | undefined
709
712
let startTime : number | undefined
@@ -806,42 +809,42 @@ async function buildEnvironment(
806
809
)
807
810
808
811
// 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
+ }
845
848
846
849
// write or generate files with rollup
847
850
const { rolldown } = await import ( 'rolldown' )
@@ -1502,7 +1505,7 @@ export interface ViteBuilder {
1502
1505
buildApp ( ) : Promise < void >
1503
1506
build (
1504
1507
environment : BuildEnvironment ,
1505
- ) : Promise < RollupOutput | RollupOutput [ ] /* | RollupWatcher */ >
1508
+ ) : Promise < RollupOutput | RollupOutput [ ] | RollupWatcher >
1506
1509
}
1507
1510
1508
1511
export interface BuilderOptions {
0 commit comments