Created
November 2, 2016 15:55
-
-
Save pigoz/f3ead97b369fb006d25a337524c5b304 to your computer and use it in GitHub Desktop.
webpack/gulp integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gulp = require('gulp'); | |
const gutil = require('gulp-util'); | |
const livereload = require('gulp-livereload'); | |
const webpack = require('webpack'); | |
const notify = require('node-notifier'); | |
const config = require('../config'); | |
const manifest = require('../manifest'); | |
const webpackConfigDefault = require('../webpack.config'); | |
const webpackConfigTest = require('../webpack.config.test'); | |
const statsConfig = { | |
colors: true, | |
chunkModules: false, | |
}; | |
function handleStats(stats) { | |
if (stats.hasErrors()) { | |
gutil.log(stats.toString(statsConfig)); | |
notify.notify({ title: 'webpack', message: 'js error' }); | |
} else { | |
gutil.log(stats.toString(statsConfig)); | |
notify.notify({ title: 'webpack', message: 'js compiled successfully!' }); | |
} | |
} | |
function runWebpack(webpackConfig, done) { | |
const compiler = webpack(webpackConfig); | |
const cb = (err, stats) => { | |
if (err) { | |
throw err; | |
} | |
handleStats(stats); | |
livereload.changed(config.paths.webpackMainCss); | |
if (!done.called) { | |
/* eslint-disable no-param-reassign */ | |
done.called = true; | |
done(); | |
} | |
}; | |
if (webpackConfig.watch) { | |
compiler.watch({}, cb); | |
} else { | |
compiler.run(cb); | |
} | |
} | |
gulp.task('webpack', (done) => { | |
runWebpack(webpackConfigDefault, done); | |
}); | |
gulp.task('webpack:test', (done) => { | |
runWebpack(webpackConfigTest, done); | |
}); | |
gulp.task('webpack-bundle', ['webpack'], () => { | |
const src = [ | |
config.dst(config.paths.webpackMain), | |
config.dst(config.paths.webpackMainCss), | |
]; | |
return gulp.src(src, { base: config.dst() }) | |
.pipe(manifest.write()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ffranz