UNPKG

6.35 kBMarkdownView Raw
1# webpack-stream [![Build Status](http://img.shields.io/travis/shama/webpack-stream.svg)](https://travis-ci.org/shama/webpack-stream)
2
3Run [webpack](https://github.com/webpack/webpack) as a stream to conveniently integrate with gulp.
4
5[![NPM](https://nodei.co/npm/webpack-stream.png?downloads=true)](https://nodei.co/npm/webpack-stream/)
6
7## Usage
8
9```js
10var gulp = require('gulp');
11var webpack = require('webpack-stream');
12gulp.task('default', function() {
13 return gulp.src('src/entry.js')
14 .pipe(webpack())
15 .pipe(gulp.dest('dist/'));
16});
17```
18
19The 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).
20
21You can pass webpack options in with the first argument, including `watch` which will greatly decrease compilation times:
22
23```js
24return gulp.src('src/entry.js')
25 .pipe(webpack({
26 watch: true,
27 module: {
28 loaders: [
29 { test: /\.css$/, loader: 'style!css' },
30 ],
31 },
32 }))
33 .pipe(gulp.dest('dist/'));
34```
35
36Or just pass in your `webpack.config.js`:
37
38```js
39return gulp.src('src/entry.js')
40 .pipe(webpack( require('./webpack.config.js') ))
41 .pipe(gulp.dest('dist/'));
42```
43
44If you would like to use a different version of webpack than the one this plugin uses, pass in an optional 2nd argument:
45
46```js
47var gulp = require('gulp');
48var webpack = require('webpack');
49var gulpWebpack = require('webpack-stream');
50gulp.task('default', function() {
51 return gulp.src('src/entry.js')
52 .pipe(gulpWebpack({}, webpack))
53 .pipe(gulp.dest('dist/'));
54});
55```
56
57Pass in 3rd argument if you want to access the stats outputted from webpack when the compilation is done:
58
59
60```js
61var gulp = require('gulp');
62var webpack = require('webpack-stream');
63gulp.task('default', function() {
64 return gulp.src('src/entry.js')
65 .pipe(webpack({
66 /* config */
67 }, null, function(err, stats) {
68 /* Use stats to do more things if needed */
69 }))
70 .pipe(gulp.dest('dist/'));
71});
72```
73
74#### Multiple Entry Points
75
76A 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:
77
78```js
79var gulp = require('gulp');
80var webpack = require('webpack-stream');
81gulp.task('default', function() {
82 return gulp.src('src/entry.js')
83 .pipe(webpack({
84 entry: {
85 app: 'src/app.js',
86 test: 'test/test.js',
87 },
88 output: {
89 filename: '[name].js',
90 },
91 }))
92 .pipe(gulp.dest('dist/'));
93});
94```
95
96Or pipe files through a stream that names the chunks. A convenient library for this is [vinyl-named](https://github.com/shama/vinyl-named):
97
98```js
99var gulp = require('gulp');
100var webpack = require('webpack-stream');
101var named = require('vinyl-named');
102gulp.task('default', function() {
103 return gulp.src(['src/app.js', 'test/test.js'])
104 .pipe(named())
105 .pipe(webpack())
106 .pipe(gulp.dest('dist/'));
107});
108```
109
110The 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.
111
112#### Source Maps
113
114Source maps are built into webpack, specify a [devtool](https://webpack.github.io/docs/configuration.html#devtool):
115
116```js
117var gulp = require('gulp');
118var webpack = require('webpack-stream');
119var named = require('vinyl-named');
120gulp.task('default', function() {
121 return gulp.src(['src/app.js', 'test/test.js'])
122 .pipe(named())
123 .pipe(webpack({
124 devtool: 'source-map'
125 }))
126 .pipe(gulp.dest('dist/'));
127});
128```
129
130Now the appropriate `.map` files will be emitted. Or set to `inline-source-map`
131to inline the source maps into the files.
132
133If you need further special handling of source maps, such as using with
134[gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) then just pipe
135to a stream and handle the source map files emitted by webpack:
136
137```js
138var gulp = require('gulp');
139var webpack = require('webpack-stream');
140var named = require('vinyl-named');
141var through = require('through2');
142var sourcemaps = require('gulp-sourcemaps');
143gulp.task('default', function() {
144 return gulp.src(['src/app.js', 'test/test.js'])
145 .pipe(named())
146 .pipe(webpack({
147 devtool: 'source-map'
148 }))
149 .pipe(sourcemaps.init({loadMaps: true}))
150 .pipe(through.obj(function (file, enc, cb) {
151 // Dont pipe through any source map files as it will be handled
152 // by gulp-sourcemaps
153 var isSourceMap = /\.map$/.test(file.path);
154 if (!isSourceMap) this.push(file);
155 cb();
156 }))
157 .pipe(sourcemaps.write('.'))
158 .pipe(gulp.dest('dist/'));
159});
160```
161
162## Release History
163* 3.1.0 - Better error output (@hi-q).
164* 3.0.1 - Fix fonts being passed through streams (@mattlewis92).
165* 3.0.0 - Remove special handling of source maps. Update dependencies.
166* 2.3.0 - Emit stats.compilation.errors as `error` (@JakeChampion).
167* 2.2.0 - Add support for source maps (@OliverJAsh).
168* 2.1.0 - Avoid modifying options by reference (@shinuza). Replace log with correct package name (@vinnymac).
169* 2.0.0 - Rename to webpack-stream and now it's totally not a gulp plugin.
170* 1.5.0 - Update webpack to 1.9.x (@nmccready). Update other dependencies.
171* 1.4.0 - Update webpack to 1.8.x (@Zolmeister).
172* 1.3.2 - Fix another place with ? in name (@raphaelluchini).
173* 1.3.1 - Fix for paths with ? in their name (@raphaelluchini).
174* 1.3.0 - Updating to webpack >= 1.7.
175* 1.2.0 - Updating to webpack >= 1.5, vinyl >= 0.4, memory-fs >= 0.2.
176* 1.1.2 - Fixes to default stats for logging (@mdreizin).
177* 1.1.1 - Add additional stats to default logging (@mdreizin).
178* 1.1.0 - Exposes internal webpack if asked via `require('webpack-stream').webpack`
179* 1.0.0 - Support named chunks pipe'd in for multiple entry points.
180* 0.4.1 - Fixed regression for multiple entry point support.
181* 0.4.0 - Display an error message if there are no input files (@3onyc). Add message on why task is not finishing, Add ability to track compilation progress, Add ability to configure stats output via options (@kompot). Bump webpack version (@koistya).
182* 0.3.0 - Update deps (@kompot). Fixes to determining entry (@btipling and @abergs).
183* 0.2.0 - Support for `watch` mode (@ampedandwired).
184* 0.1.0 - Initial release
185
186## License
187Copyright (c) 2015 Kyle Robinson Young
188Licensed under the MIT license.