1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const through2 = require('through2'),
|
7 | browserify = require('browserify'),
|
8 | PluginError = require('plugin-error');
|
9 |
|
10 | const PLUGIN_NAME = 'gulpBrowserify';
|
11 |
|
12 |
|
13 | function gulpBrowserify(options) {
|
14 | options = options || {};
|
15 |
|
16 | return through2.obj(function(file, enc, next) {
|
17 | if (file.isStream()) {
|
18 | this.emit('error', new PluginError(PLUGIN_NAME, 'Stream content is not supported'));
|
19 | return next(null, file);
|
20 | }
|
21 |
|
22 | if (file.isBuffer()) {
|
23 | let bw = browserify(options).add(file.path);
|
24 | if (Array.isArray(options.external)) {
|
25 | bw = bw.external(options.external || []);
|
26 | }
|
27 | bw.bundle((err, src) => {
|
28 | if (!err) {
|
29 | file.contents = new Buffer(src);
|
30 | this.push(file);
|
31 | return next();
|
32 | } else {
|
33 | this.emit('error', new PluginError(PLUGIN_NAME, err.message || 'browserify bundle error'));
|
34 | }
|
35 | });
|
36 | }
|
37 | });
|
38 | }
|
39 |
|
40 | module.exports = gulpBrowserify;
|