usage with gulp
Zearin edited this page Sep 5, 2016
·
20 revisions
Pages 52
- Home
- amd
- api in modules
- build performance
- changelog
- cli
- code splitting
- commonjs
- comparison
- configuration
- contents
- context
- dev tools
- examples
- FAQ
- hot module replacement
- hot module replacement with webpack
- how to write a loader
- how to write a plugin
- ideas
- internal webpack plugins
- library and externals
- list of hints
- list of loaders
- list of plugins
- list of tutorials
- loader conventions
- loaders
- long term caching
- Motivation
- multiple entry points
- node.js api
- optimization
- plugins
- resolving
- roadmap
- shimming modules
- testing
- TODO
- troubleshooting
- usage
- usage with bower
- usage with grunt
- usage with gulp
- usage with karma
- Usage with Visual Studio
- using loaders
- using plugins
- webpack dev middleware
- webpack dev server
- webpack for browserify users
- what is webpack
- Show 37 more pages…
Clone this wiki locally
Using webpack with gulp is as easy as using the node.js API.
Using webpack-stream
var gulp = require('gulp');
var webpack = require('webpack-stream');
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
(a webpack-generated hash of the build).
Or just pass in your webpack.config.js
:
return gulp.src('src/entry.js')
.pipe(webpack( require('./webpack.config.js') ))
.pipe(gulp.dest('dist/'));
See webpack-stream for more options and details.
Without webpack-stream
var gulp = require("gulp");
var gutil = require("gulp-util");
var webpack = require("webpack");
var WebpackDevServer = require("webpack-dev-server");
Normal compilation
gulp.task("webpack", function(callback) {
// run webpack
webpack({
// configuration
}, function(err, stats) {
if(err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
// output options
}));
callback();
});
});
webpack-dev-server
Don't be too lazy to integrate the
webpack-dev-server
into your development process. It's an important tool for productivity.
gulp.task("webpack-dev-server", function(callback) {
// Start a webpack-dev-server
var compiler = webpack({
// configuration
});
new WebpackDevServer(compiler, {
// server and middleware options
}).listen(8080, "localhost", function(err) {
if(err) throw new gutil.PluginError("webpack-dev-server", err);
// Server listening
gutil.log("[webpack-dev-server]", "http://localhost:8080/webpack-dev-server/index.html");
// keep the server alive or continue?
// callback();
});
});
Example
Take a look at an example gulpfile. It covers three modes:
- webpack-dev-server
- build - watch cycle
- production build
webpack 👍