Skip to content

Commit ed36df4

Browse files
aleclarsoncpojer
authored andcommitted
add --watch mode to "yarn build" (#15116)
* wip: add --watch mode to "yarn build" * fix: handle error events
1 parent 793ef9b commit ed36df4

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

scripts/rollup/build.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const {rollup} = require('rollup');
3+
const rollup = require('rollup');
44
const babel = require('rollup-plugin-babel');
55
const closure = require('./plugins/closure-plugin');
66
const commonjs = require('rollup-plugin-commonjs');
@@ -78,6 +78,7 @@ const requestedBundleTypes = argv.type
7878
: [];
7979
const requestedBundleNames = parseRequestedNames(argv._, 'lowercase');
8080
const forcePrettyOutput = argv.pretty;
81+
const isWatchMode = argv.watch;
8182
const syncFBSourcePath = argv['sync-fbsource'];
8283
const syncWWWPath = argv['sync-www'];
8384
const shouldExtractErrors = argv['extract-errors'];
@@ -526,19 +527,42 @@ async function createBundle(bundle, bundleType) {
526527
bundleType
527528
);
528529

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`);
540565
}
541-
console.log(`${chalk.bgGreen.black(' COMPLETE ')} ${logKey}\n`);
542566
}
543567

544568
function handleRollupWarning(warning) {

0 commit comments

Comments
 (0)