-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathbundle.js
104 lines (82 loc) · 2.75 KB
/
bundle.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
var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var UglifyJS = require('uglify-js');
var compressAttributes = require('./util/compress_attributes');
var constants = require('./util/constants');
/*
* This script takes one argument
*
* Run `npm run build -- dev` or `npm run build -- --dev`
* to include source map in the plotly.js bundle
*
* N.B. This script is meant for dist builds; the output bundles are placed
* in plotly.js/dist/.
* Use `npm run watch` for dev builds.
*/
var arg = process.argv[2];
var DEV = (arg === 'dev') || (arg === '--dev');
// Check if style and font build files are there
try {
fs.statSync(constants.pathToCSSBuild).isFile();
fs.statSync(constants.pathToFontSVGBuild).isFile();
}
catch(e) {
throw new Error([
'build/ is missing one or more files',
'Please run `npm run preprocess` first'
].join('\n'));
}
// Browserify plotly.js
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
standalone: 'Plotly',
debug: DEV,
pathToMinBundle: constants.pathToPlotlyDistMin
});
// Browserify the geo assets
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
standalone: 'PlotlyGeoAssets'
});
// Browserify the plotly.js with meta
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
standalone: 'Plotly',
debug: DEV
});
// Browserify the plotly.js partial bundles
constants.partialBundlePaths.forEach(function(pathObj) {
_bundle(pathObj.index, pathObj.dist, {
standalone: 'Plotly',
debug: DEV,
pathToMinBundle: pathObj.distMin
});
});
function _bundle(pathToIndex, pathToBundle, opts) {
opts = opts || {};
// do we output a minified file?
var pathToMinBundle = opts.pathToMinBundle,
outputMinified = !!pathToMinBundle && !opts.debug;
var browserifyOpts = {};
browserifyOpts.standalone = opts.standalone;
browserifyOpts.debug = opts.debug;
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
var b = browserify(pathToIndex, browserifyOpts),
bundleWriteStream = fs.createWriteStream(pathToBundle);
bundleWriteStream.on('finish', function() {
logger(pathToBundle);
});
b.bundle(function(err, buf) {
if(err) throw err;
if(outputMinified) {
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
if(err) throw err;
logger(pathToMinBundle);
});
}
})
.pipe(bundleWriteStream);
}
function logger(pathToOutput) {
var log = 'ok ' + path.basename(pathToOutput);
console.log(log);
}