@@ -163,7 +163,7 @@ async function runDtsBundler(entrypoint, output) {
163
163
* @typedef BundlerTaskOptions
164
164
* @property {boolean } [exportIsTsObject]
165
165
* @property {boolean } [treeShaking]
166
- * @property {esbuild.WatchMode } [watchMode ]
166
+ * @property {() => void } [onWatchRebuild ]
167
167
*/
168
168
function createBundler ( entrypoint , outfile , taskOptions = { } ) {
169
169
const getOptions = memoize ( async ( ) => {
@@ -220,7 +220,30 @@ function createBundler(entrypoint, outfile, taskOptions = {}) {
220
220
221
221
return {
222
222
build : async ( ) => esbuild . build ( await getOptions ( ) ) ,
223
- watch : async ( ) => esbuild . build ( { ...await getOptions ( ) , watch : taskOptions . watchMode ?? true , logLevel : "info" } ) ,
223
+ watch : async ( ) => {
224
+ /** @type {esbuild.BuildOptions } */
225
+ const options = { ...await getOptions ( ) , logLevel : "info" } ;
226
+ if ( taskOptions . onWatchRebuild ) {
227
+ const onRebuild = taskOptions . onWatchRebuild ;
228
+ options . plugins = ( options . plugins ?. slice ( 0 ) ?? [ ] ) . concat ( [ {
229
+ name : "watch" ,
230
+ setup : ( build ) => {
231
+ let firstBuild = true ;
232
+ build . onEnd ( ( ) => {
233
+ if ( firstBuild ) {
234
+ firstBuild = false ;
235
+ }
236
+ else {
237
+ onRebuild ( ) ;
238
+ }
239
+ } ) ;
240
+ }
241
+ } ] ) ;
242
+ }
243
+
244
+ const ctx = await esbuild . context ( options ) ;
245
+ ctx . watch ( ) ;
246
+ } ,
224
247
} ;
225
248
}
226
249
@@ -426,10 +449,8 @@ const { main: tests, watch: watchTests } = entrypointBuildTask({
426
449
bundlerOptions : {
427
450
// Ensure we never drop any dead code, which might be helpful while debugging.
428
451
treeShaking : false ,
429
- watchMode : {
430
- onRebuild ( ) {
431
- watchTestsEmitter . emit ( "rebuild" ) ;
432
- }
452
+ onWatchRebuild ( ) {
453
+ watchTestsEmitter . emit ( "rebuild" ) ;
433
454
}
434
455
} ,
435
456
} ) ;
0 commit comments