UNPKG

4.62 kBJavaScriptView Raw
1'use strict';
2
3var gutil = require('gulp-util');
4var File = require('vinyl');
5var MemoryFileSystem = require('memory-fs');
6var through = require('through');
7var ProgressPlugin = require('webpack/lib/ProgressPlugin');
8
9var defaultStatsOptions = {
10 colors: gutil.colors.supportsColor,
11 hash: false,
12 timings: false,
13 chunks: false,
14 chunkModules: false,
15 modules: false,
16 children: true,
17 version: true,
18 cached: false,
19 cachedAssets: false,
20 reasons: false,
21 source: false,
22 errorDetails: false
23};
24
25module.exports = function (options, wp, done) {
26 options = clone(options) || {};
27 if (typeof done !== 'function') {
28 var callingDone = false;
29 done = function (err, stats) {
30 if (err) {
31 // The err is here just to match the API but isnt used
32 return;
33 }
34 stats = stats || {};
35 if (options.quiet || callingDone) {
36 return;
37 }
38 // Debounce output a little for when in watch mode
39 if (options.watch) {
40 callingDone = true;
41 setTimeout(function () {
42 callingDone = false;
43 }, 500);
44 }
45 if (options.verbose) {
46 gutil.log(stats.toString({
47 colors: gutil.colors.supportsColor
48 }));
49 } else {
50 var statsOptions = options && options.stats || {};
51
52 Object.keys(defaultStatsOptions).forEach(function (key) {
53 if (typeof statsOptions[key] === 'undefined') {
54 statsOptions[key] = defaultStatsOptions[key];
55 }
56 });
57
58 gutil.log(stats.toString(statsOptions));
59 }
60 };
61 }
62
63 var webpack = wp || require('webpack');
64 var entry = [];
65 var entries = Object.create(null);
66
67 var stream = through(function (file) {
68 if (file.isNull()) {
69 return;
70 }
71 if ('named' in file) {
72 if (!Array.isArray(entries[file.named])) {
73 entries[file.named] = [];
74 }
75 entries[file.named].push(file.path);
76 } else {
77 entry = entry || [];
78 entry.push(file.path);
79 }
80 }, function () {
81 var self = this;
82 options.output = options.output || {};
83
84 // Determine pipe'd in entry
85 if (Object.keys(entries).length > 0) {
86 entry = entries;
87 if (!options.output.filename) {
88 // Better output default for multiple chunks
89 options.output.filename = '[name].js';
90 }
91 } else if (entry.length < 2) {
92 entry = entry[0] || entry;
93 }
94
95 options.entry = options.entry || entry;
96 options.output.path = options.output.path || process.cwd();
97 options.output.filename = options.output.filename || '[hash].js';
98 entry = [];
99
100 if (!options.entry || options.entry.length < 1) {
101 gutil.log('webpack-stream - No files given; aborting compilation');
102 return self.emit('end');
103 }
104
105 var compiler = webpack(options, function (err, stats) {
106 if (err) {
107 self.emit('error', new gutil.PluginError('webpack-stream', err));
108 }
109 if (!options.watch) {
110 self.queue(null);
111 }
112 done(err, stats);
113 if (options.watch && !options.quiet) {
114 gutil.log('webpack is watching for changes');
115 }
116 });
117
118 // In watch mode webpack returns a wrapper object so we need to get
119 // the underlying compiler
120 if (options.watch) {
121 compiler = compiler.compiler;
122 }
123
124 if (options.progress) {
125 compiler.apply(new ProgressPlugin(function (percentage, msg) {
126 percentage = Math.floor(percentage * 100);
127 msg = percentage + '% ' + msg;
128 if (percentage < 10) msg = ' ' + msg;
129 gutil.log('webpack', msg);
130 }));
131 }
132
133 var fs = compiler.outputFileSystem = new MemoryFileSystem();
134 compiler.plugin('after-emit', function (compilation, callback) {
135 Object.keys(compilation.assets).forEach(function (outname) {
136 if (compilation.assets[outname].emitted) {
137 var path = fs.join(compiler.outputPath, outname);
138 if (path.indexOf('?') !== -1) {
139 path = path.split('?')[0];
140 }
141 var contents = fs.readFileSync(path);
142 self.queue(new File({
143 base: compiler.outputPath,
144 path: path,
145 contents: contents
146 }));
147 }
148 });
149 callback();
150 });
151 });
152
153 // If entry point manually specified, trigger that
154 if (options.entry) {
155 stream.end();
156 }
157
158 return stream;
159};
160
161// Expose webpack if asked
162Object.defineProperty(module.exports, 'webpack', {
163 get: function () {
164 return require('webpack');
165 }
166});
167
168function clone (source) {
169 var target = {};
170 Object.keys(source).forEach(function (key) {
171 target[key] = source[key];
172 });
173
174 return target;
175}