Skip to content
Merged
Prev Previous commit
Next Next commit
Configure watch mode for error-overlay webpack build
  • Loading branch information
tharakawj committed Oct 3, 2017
commit f569f45f30ad7b19b8fba530fc250d50021d7cfb
31 changes: 30 additions & 1 deletion packages/react-error-overlay/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const chalk = require('chalk');
const webpackConfig = require('./webpack.config.js');
const iframeWebpackConfig = require('./webpack.config.iframe.js');
const rimraf = require('rimraf');
const chokidar = require('chokidar');

const args = process.argv.slice(2);
const watchMode = args[0] === '--watch' || args[0] === '-w';

function build(config, name, callback) {
console.log(chalk.cyan('Compiling ' + name));
Expand Down Expand Up @@ -38,13 +42,38 @@ function runBuildSteps() {
build(iframeWebpackConfig, 'iframeScript.js', () => {
build(webpackConfig, 'index.js', () => {
console.log(chalk.bold.green('Compiled successfully!'));
console.log();
console.log();
});
});
}

function setupWatch() {
const watcher = chokidar.watch('./src', {
ignoreInitial: true,
});

watcher.on('change', runBuildSteps);
watcher.on('add', runBuildSteps);

watcher.on('ready', () => {
runBuildSteps();
});

process.on('SIGINT', function() {
watcher.close();
process.exit(0);
});

watcher.on('error', error => {
console.error('Watcher failure', error);
process.exit(1);
});
}

// Clean up lib folder
rimraf('lib/', () => {
console.log('Cleaned up the lib folder.');
console.log();
runBuildSteps();
watchMode ? setupWatch() : runBuildSteps();
});
3 changes: 2 additions & 1 deletion packages/react-error-overlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/index.js",
"scripts": {
"prepublishOnly": "npm run build:prod && npm test",
"start": "cross-env NODE_ENV=development node build.js",
"start": "cross-env NODE_ENV=development node build.js --watch",
"test": "flow && cross-env NODE_ENV=test jest",
"build": "cross-env NODE_ENV=development node build.js",
"build:prod": "cross-env NODE_ENV=production node build.js"
Expand Down Expand Up @@ -46,6 +46,7 @@
"babel-eslint": "7.2.3",
"babel-preset-react-app": "^3.0.3",
"chalk": "^2.1.0",
"chokidar": "^1.7.0",
"cross-env": "5.0.5",
"eslint": "4.4.1",
"eslint-config-react-app": "^2.0.1",
Expand Down