-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbundle_wrapper.js
90 lines (72 loc) · 2.73 KB
/
bundle_wrapper.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
var path = require('path');
var webpack = require('webpack');
var config = require('../../webpack.config.js');
var nRules = config.module.rules.length;
/** Convenience bundle wrapper
*
* @param {string} pathToIndex path to index file to bundle
* @param {string} pathToBunlde path to destination bundle
* @param {object} opts
* Bundle options:
* - standalone {string}
* Additional option:
* - pathToMinBundle {string} path to destination minified bundle
* - noCompress {boolean} skip attribute meta compression?
* @param {function} cb callback
*
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted.
* Otherwise outputs two file: one un-minified bundle and one minified bundle.
*
* Logs basename of bundle when completed.
*/
module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
opts = opts || {};
var pathToMinBundle = opts.pathToMinBundle;
config.module.rules[nRules] = opts.noCompress ? {} : {
test: /\.js$/,
use: [
'transform-loader?' + path.resolve(__dirname, '../../tasks/compress_attributes.js')
]
};
config.entry = pathToIndex;
var pending = (pathToMinBundle && pathToBundle) ? 2 : 1;
var parsedPath;
parsedPath = path.parse(pathToBundle || pathToMinBundle);
config.output.path = parsedPath.dir;
config.output.filename = parsedPath.base;
config.output.library.name = opts.standalone || 'Plotly';
config.optimization = {
minimize: !!(pathToMinBundle && pending === 1)
};
var compiler = webpack(config);
compiler.run(function(err, stats) {
if(err) {
console.log('err:', err);
} if(stats.errors && stats.errors.length) {
console.log('stats.errors:', stats.errors);
} else {
console.log('success:', config.output.path + '/' + config.output.filename);
if(pending === 2) {
parsedPath = path.parse(pathToMinBundle);
config.output.path = parsedPath.dir;
config.output.filename = parsedPath.base;
config.optimization = {
minimize: true
};
compiler = webpack(config);
compiler.run(function(err, stats) {
if(err) {
console.log('err:', err);
} else if(stats.errors && stats.errors.length) {
console.log('stats.errors:', stats.errors);
} else {
console.log('success:', config.output.path + '/' + config.output.filename);
if(cb) cb();
}
});
} else {
if(cb) cb();
}
}
});
};