-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (177 loc) · 6.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
// base utils
const _ = require('lodash');
const async = require('async');
const globby = require('globby');
// gulp utils
const gulp = require('gulp');
const gutil = require('gulp-util');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
// browserify utils
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const babelify = require('babelify');
const watchify = require('watchify');
const lessModulesify = require('less-modulesify');
class BBPack {
/**
* @param {Object} config - pack config
* @param {Array} config.transforms - browserify's transforms and plugins list
* @param {Boolean} config.sourceMap - using sourceMap or not
* @param {Boolean} config.uglify - uglify code or not
* @param {Boolean} config.watch - auto building when file changes or not
* @constructor
*/
constructor (config) {
if (!config) {
config = {};
}
// default support es6\7 and react and css-module in less
const defaultTransforms = [
{
plugin: lessModulesify,
config: {
sourceMap: config.sourceMap,
lessCompileOption: {}
}
},
{
transform: babelify,
config: {
presets: ['es2015', 'react', 'stage-3'],
plugins: ['syntax-export-extensions']
}
}
];
this._transforms = _.get(config, 'transforms', defaultTransforms);
this._sourceMap = _.get(config, 'sourceMap', false);
this._uglify = _.get(config, 'uglify', false);
this._watch = _.get(config, 'watch', false);
this._afterPipes = _.get(config, 'afterPipes', []);
}
/**
* browserify's transform
* @param {Object} stream - browserify stream
* @param {Object} savePath - target file's path(includes the filename)
* @return {Object} - gulp stream after transform
* @private
*/
_browserifyTransform (stream, savePath) {
let tmpStream = stream;
// using custom transform or default transform for browserify
_.forEach(this._transforms, (transform) => {
if (transform.transform) {
tmpStream = tmpStream.transform(transform.transform, transform.config);
} else if (transform.plugin) {
tmpStream = tmpStream.plugin(transform.plugin, transform.config);
}
});
// transform browserify formed stream to glup formed stream
tmpStream = tmpStream
.bundle()
.pipe(source(savePath))
.pipe(buffer());
// sourcemap and uglify
this._sourceMap && (tmpStream = tmpStream.pipe(sourcemaps.init({ loadMaps: true })));
this._uglify && (tmpStream = tmpStream.pipe(uglify()));
this._sourceMap && (tmpStream = tmpStream.pipe(sourcemaps.write('./')));
// custom after pipe, default do nothing
tmpStream = tmpStream.pipe(gulp.dest('./'));
_.forEach(this._afterPipes, (afterPipe) => {
tmpStream = tmpStream.pipe(afterPipe.stream(afterPipe.config));
});
return tmpStream;
}
/**
* multy streams' end-events together
* @param {String} taskName - stream's flag or custom name
* @param {Array} streams - target streams
* @param {Function} callback - callback function
* @private
*/
_streamsEndListening (taskName, streams, callback) {
if (!callback) {
callback = () => {};
}
async.each(streams, (stream, callback) => {
stream.on('end', () => {
return callback();
});
}, (err) => {
console.log(`${ taskName } finished`);
return callback(err);
});
}
/**
* packing the libs which are used in most parts
* @param {Object} config - pack config
* @public
*/
libsPack (config, callback) {
const libs = _.get(config, 'libs', []);
const savePath = _.get(config, 'savePath');
const streams = [];
let addListener = false;
const bundle = (stream) => {
let tmpStream = stream;
libs.forEach((lib) => {
tmpStream = tmpStream.require(lib.src, { expose: lib.expose });
});
tmpStream = this._browserifyTransform(tmpStream, savePath);
if (!addListener) {
streams.push(tmpStream);
addListener = true;
}
};
let stream = browserify({
debug: this._sourceMap
});
if (this._watch) {
stream = stream.plugin(watchify);
stream.on('update', () => bundle(stream));
stream.on('log', gutil.log);
}
bundle(stream);
this._streamsEndListening('libsPack', streams, callback);
}
/**
* packing the js files which are used for the pages
* @param {Object} config - pack config
* @public
*/
pagesPack (config, callback) {
const pages = _.get(config, 'pages', []);
const externals = _.get(config, 'externals', []);
const streams = [];
async.each(pages, (page, callback) => {
let addListener = false;
const bundle = (stream) => {
let tmpStream = stream;
tmpStream = tmpStream.external(externals);
tmpStream = this._browserifyTransform(tmpStream, page.path);
if (!addListener) {
streams.push(tmpStream);
addListener = true;
}
};
globby(page.parts).then((entries) => {
let stream = browserify({
entries: entries,
debug: this._sourceMap
});
if (this._watch) {
stream = stream.plugin(watchify);
stream.on('update', () => bundle(stream));
stream.on('log', gutil.log);
}
bundle(stream);
return callback();
});
}, () => {
this._streamsEndListening('pagesPack', streams, callback);
});
}
}
module.exports = BBPack;