|
| 1 | +#!/usr/bin/env node |
| 2 | +const nodemon = require('nodemon'); |
| 3 | +const path = require('path'); |
| 4 | +const { once } = require('ramda'); |
| 5 | +const webpack = require('webpack'); |
| 6 | +const paths = require('../config/paths'); |
| 7 | +const webpackConfig = require('../config/webpack.config'); |
| 8 | + |
| 9 | +const compiler = webpack(webpackConfig); |
| 10 | + |
| 11 | +const startDevServer = () => { |
| 12 | + const serverPaths = Object.keys(compiler.options.entry).map(entry => |
| 13 | + path.join(compiler.options.output.path, `${entry}.js`), |
| 14 | + ); |
| 15 | + compiler.watch( |
| 16 | + webpackConfig.watchOptions, |
| 17 | + once((error, stats) => { |
| 18 | + if (error || stats.hasErrors()) { |
| 19 | + throw Error(error || stats.errors); |
| 20 | + } |
| 21 | + |
| 22 | + nodemon({ script: serverPaths[0], watch: serverPaths }).on( |
| 23 | + 'quit', |
| 24 | + process.exit, |
| 25 | + ); |
| 26 | + }), |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +const createProductionBuild = () => { |
| 31 | + compiler.run((error, stats) => { |
| 32 | + if (error || stats.hasErrors()) { |
| 33 | + throw Error(error || stats.errors); |
| 34 | + } |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +require('yargs') |
| 39 | + .command(['$0', 'dev'], 'Run development mode', {}, startDevServer) |
| 40 | + .command('build', 'Create a production build', {}, createProductionBuild) |
| 41 | + .argv; |
0 commit comments