UNPKG

1.22 kBJavaScriptView Raw
1/**
2 * Created by Rodey on 2017/8/5.
3 */
4'use strict';
5
6const through2 = require('through2'),
7 browserify = require('browserify'),
8 PluginError = require('plugin-error');
9
10const PLUGIN_NAME = 'gulpBrowserify';
11
12//将压缩后的内容替换到html中
13function 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
40module.exports = gulpBrowserify;