webpack
$ cnpm install gulp-webpack
var gulp = require('gulp');
var webpack = require('gulp-webpack');
gulp.task('default', function() {
return gulp.src('src/entry.js')
.pipe(webpack())
.pipe(gulp.dest('dist/'));
});
The above will compile src/entry.js
into assets with webpack into dist/
with the output filename of [hash].js
(webpack generated hash of the build).
You can pass webpack options in with the first argument, including watch
which will greatly increase compilation times:
return gulp.src('src/entry.js')
.pipe(webpack({
watch: true,
module: {
loaders: [
{ test: /\.css$/, loader: 'style!css' },
],
},
}))
.pipe(gulp.dest('dist/'));
Or just pass in your webpack.config.js
:
return gulp.src('src/entry.js')
.pipe(webpack( require('./webpack.config.js') ))
.pipe(gulp.dest('dist/'));
If you would like to use a different version of webpack than the one this plugin uses, pass in an optional 2nd argument:
var gulp = require('gulp');
var webpack = require('webpack');
var gulpWebpack = require('gulp-webpack');
gulp.task('default', function() {
return gulp.src('src/entry.js')
.pipe(gulpWebpack({}, webpack))
.pipe(gulp.dest('dist/'));
});
Pass in 3rd argument if you want to access the stats outputted from webpack when the compilation is done:
var gulp = require('gulp');
var webpack = require('gulp-webpack');
gulp.task('default', function() {
return gulp.src('src/entry.js')
.pipe(webpack({
/* config */
}, null, function(err, stats) {
/* Use stats to do more things if needed */
}))
.pipe(gulp.dest('dist/'));
});
A common request is how to handle multiple entry points. You can continue to pass in an entry
option in your typical webpack config like so:
var gulp = require('gulp');
var webpack = require('gulp-webpack');
gulp.task('default', function() {
return gulp.src('src/entry.js')
.pipe(webpack({
entry: {
app: 'src/app.js',
test: 'test/test.js',
},
output: {
filename: '[name].js',
},
}))
.pipe(gulp.dest('dist/'));
});
Or pipe files through a stream that names the chunks. A convenient library for this is vinyl-named:
var gulp = require('gulp');
var webpack = require('gulp-webpack');
var named = require('vinyl-named');
gulp.task('default', function() {
return gulp.src(['src/app.js', 'test/test.js'])
.pipe(named())
.pipe(webpack())
.pipe(gulp.dest('dist/'));
});
The above named()
stream will add a .named
property to the vinyl files passing through. The webpack()
stream will read those as entry points and even group entry points with common names together.
require('gulp-webpack').webpack
watch
mode (@ampedandwired).Copyright (c) 2015 Kyle Robinson Young
Licensed under the MIT license.
Copyright 2013 - present © cnpmjs.org