@@ -6,7 +6,7 @@ import { basename, parse, resolve } from 'path';
66import babelPresetEnv from '@babel/preset-env' ;
77import babelPresetTypescript from '@babel/preset-typescript' ;
88import builtinModules from 'builtin-modules' ;
9- import { rollup , OutputOptions , InputOptions } from 'rollup' ;
9+ import { rollup , watch , OutputOptions , InputOptions } from 'rollup' ;
1010import babel from 'rollup-plugin-babel' ;
1111import commonjs from 'rollup-plugin-commonjs' ;
1212import json from 'rollup-plugin-json' ;
@@ -149,6 +149,7 @@ interface BundlerOptions {
149149 nodeTarget : string ;
150150 outputDir : string ;
151151 typesDir ?: string ;
152+ watchBuild ?: boolean ;
152153}
153154
154155export async function bundler ( {
@@ -157,6 +158,7 @@ export async function bundler({
157158 nodeTarget,
158159 outputDir,
159160 typesDir,
161+ watchBuild,
160162} : BundlerOptions ) {
161163 // the current working directory
162164 const cwd = process . cwd ( ) ;
@@ -171,11 +173,13 @@ export async function bundler({
171173 const pkgDependencies = Object . keys ( pkg . dependencies || { } ) ;
172174
173175 // find all the input TypeScript files
174- const inputs = await glob ( input , { absolute : true } ) ;
176+ const inputs = await glob ( input ) ;
175177
176178 // if we have more than one input, flag this as a multi-input run
177179 const withMultipleInputs = inputs . length > 1 ;
178180
181+ const runs = [ ] ;
182+
179183 // loop thorugh the inputs, creating a rollup configuraion for each one
180184 for ( let idx = 0 ; idx < inputs . length ; idx ++ ) {
181185 const input = inputs [ idx ] ;
@@ -184,7 +188,7 @@ export async function bundler({
184188 inputs . filter ( e => e !== input )
185189 ) ;
186190
187- const { inputOptions , outputOptions } = await createRollupConfig ( {
191+ const options = await createRollupConfig ( {
188192 compress,
189193 externalDependencies,
190194 input,
@@ -194,13 +198,44 @@ export async function bundler({
194198 pkgMain : pkg . main ,
195199 } ) ;
196200
197- const bundle = await rollup ( inputOptions ) ;
198-
199- for ( let idx = 0 ; idx < outputOptions . length ; idx ++ ) {
200- const output = outputOptions [ idx ] ;
201+ runs . push ( options ) ;
202+ }
201203
202- await bundle . write ( output ) ;
203- await createTypes ( { input, output : typesDir } ) ;
204+ for ( const { inputOptions, outputOptions } of runs ) {
205+ if ( watchBuild ) {
206+ const watcher = watch (
207+ Object . assign (
208+ {
209+ output : outputOptions ,
210+ watch : {
211+ exclude : 'node_modules/**' ,
212+ } ,
213+ } ,
214+ inputOptions
215+ )
216+ ) ;
217+
218+ watcher . on ( 'event' , ( { code, error } ) => {
219+ switch ( code ) {
220+ case 'FATAL' :
221+ throw new Error ( error ) ;
222+ case 'ERROR' :
223+ console . error ( error ) ;
224+ break ;
225+ case 'END' :
226+ console . log ( `Successful build. (${ inputOptions . input } )` ) ;
227+ break ;
228+ }
229+ } ) ;
230+ } else {
231+ const bundle = await rollup ( inputOptions ) ;
232+
233+ for ( let idx = 0 ; idx < outputOptions . length ; idx ++ ) {
234+ const output = outputOptions [ idx ] ;
235+
236+ await bundle . write ( output ) ;
237+ await createTypes ( { input, output : typesDir } ) ;
238+ }
204239 }
205240 }
206241}
0 commit comments