add --watch mode to "yarn build" (#15116)
* wip: add --watch mode to "yarn build" * fix: handle error events
Alec Larson committed
Apr 25, 2019 at 07:55 UTC
ed36df46c60ea1654d403aaad899cb73f997bdfb
1 file changed
+37
-13
scripts/rollup/build.js
+37
-13
@@ -1,6 +1,6 @@
1
'use strict';
2
3
-const {rollup} = require('rollup');
3
+const rollup = require('rollup');
4
const babel = require('rollup-plugin-babel');
5
const closure = require('./plugins/closure-plugin');
6
const commonjs = require('rollup-plugin-commonjs');
@@ -78,6 +78,7 @@ const requestedBundleTypes = argv.type
78
: [];
79
const requestedBundleNames = parseRequestedNames(argv._, 'lowercase');
80
const forcePrettyOutput = argv.pretty;
81
+const isWatchMode = argv.watch;
82
const syncFBSourcePath = argv['sync-fbsource'];
83
const syncWWWPath = argv['sync-www'];
84
const shouldExtractErrors = argv['extract-errors'];
@@ -526,19 +527,42 @@ async function createBundle(bundle, bundleType) {
527
bundleType
528
);
529
529
- console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
530
- try {
531
- const result = await rollup(rollupConfig);
532
- await result.write(rollupOutputOptions);
533
- } catch (error) {
534
- console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`);
535
- handleRollupError(error);
536
- throw error;
537
- }
538
- for (let i = 0; i < otherOutputPaths.length; i++) {
539
- await asyncCopyTo(mainOutputPath, otherOutputPaths[i]);
530
+ if (isWatchMode) {
531
+ rollupConfig.output = [rollupOutputOptions];
532
+ const watcher = rollup.watch(rollupConfig);
533
+ watcher.on('event', async event => {
534
+ switch (event.code) {
535
+ case 'BUNDLE_START':
536
+ console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
537
+ break;
538
+ case 'BUNDLE_END':
539
+ for (let i = 0; i < otherOutputPaths.length; i++) {
540
+ await asyncCopyTo(mainOutputPath, otherOutputPaths[i]);
541
+ }
542
+ console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
543
+ break;
544
+ case 'ERROR':
545
+ case 'FATAL':
546
+ console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`);
547
+ handleRollupError(event.error);
548
+ break;
549
+ }
550
+ });
551
+ } else {
552
+ console.log(`${chalk.bgYellow.black(' BUILDING ')} ${logKey}`);
553
+ try {
554
+ const result = await rollup.rollup(rollupConfig);
555
+ await result.write(rollupOutputOptions);
556
+ } catch (error) {
557
+ console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`);
558
+ handleRollupError(error);
559
+ throw error;
560
+ }
561
+ for (let i = 0; i < otherOutputPaths.length; i++) {
562
+ await asyncCopyTo(mainOutputPath, otherOutputPaths[i]);
563
+ }
564
+ console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
565
}
541
- console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
566
}
567
568
function handleRollupWarning(warning) {