Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -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 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 23 | gulp.task('webpack:dist:watch', done => { |
| 24 | process.env.NODE_ENV = 'production'; |
| 25 | webpackWrapper(true, webpackDistConf, done); |
| 26 | }); |
| 27 | |
Matteo Scandolo | d819c92 | 2016-12-02 14:06:14 -0800 | [diff] [blame] | 28 | function webpackWrapper(watch, conf, done) { |
| 29 | const webpackBundler = webpack(conf); |
| 30 | |
| 31 | const webpackChangeHandler = (err, stats) => { |
| 32 | if (err) { |
| 33 | gulpConf.errorHandler('Webpack')(err); |
| 34 | } |
| 35 | gutil.log(stats.toString({ |
| 36 | colors: true, |
| 37 | chunks: false, |
| 38 | hash: false, |
| 39 | version: false |
| 40 | })); |
| 41 | if (done) { |
| 42 | done(); |
| 43 | done = null; |
| 44 | } else { |
| 45 | browsersync.reload(); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | if (watch) { |
| 50 | webpackBundler.watch(200, webpackChangeHandler); |
| 51 | } else { |
| 52 | webpackBundler.run(webpackChangeHandler); |
| 53 | } |
| 54 | } |