Matteo Scandolo | fc4b37b | 2017-02-02 12:18:47 -0800 | [diff] [blame] | 1 | const gulp = require('gulp'); |
| 2 | const gutil = require('gulp-util'); |
| 3 | |
| 4 | const webpack = require('webpack'); |
| 5 | const webpackConf = require('../conf/webpack.conf'); |
| 6 | const webpackDistConf = require('../conf/webpack-dist.conf'); |
| 7 | const gulpConf = require('../conf/gulp.conf'); |
| 8 | const browsersync = require('browser-sync'); |
| 9 | |
| 10 | gulp.task('webpack:dev', done => { |
| 11 | webpackWrapper(false, webpackConf, done); |
| 12 | }); |
| 13 | |
| 14 | gulp.task('webpack:watch', done => { |
| 15 | webpackWrapper(true, webpackConf, done); |
| 16 | }); |
| 17 | |
| 18 | gulp.task('webpack:dist', done => { |
| 19 | process.env.NODE_ENV = 'production'; |
| 20 | webpackWrapper(false, webpackDistConf, done); |
| 21 | }); |
| 22 | |
| 23 | function webpackWrapper(watch, conf, done) { |
| 24 | const webpackBundler = webpack(conf); |
| 25 | |
| 26 | const webpackChangeHandler = (err, stats) => { |
| 27 | if (err) { |
| 28 | gulpConf.errorHandler('Webpack')(err); |
| 29 | } |
| 30 | gutil.log(stats.toString({ |
| 31 | colors: true, |
| 32 | chunks: false, |
| 33 | hash: false, |
| 34 | version: false |
| 35 | })); |
| 36 | if (done) { |
| 37 | done(); |
| 38 | done = null; |
| 39 | } else { |
| 40 | browsersync.reload(); |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | if (watch) { |
| 45 | webpackBundler.watch(200, webpackChangeHandler); |
| 46 | } else { |
| 47 | webpackBundler.run(webpackChangeHandler); |
| 48 | } |
| 49 | } |