Skip to content

Commit b861883

Browse files
committed
use run-series in bundle script to limit memory consumption
... that can lead to `npm run build` blowing up (especially with browser-pack-flat - see next commit). - adapt opts.then to callback signatrue instead - make opts more granular and clearer
1 parent 011443e commit b861883

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
"prettysize": "1.1.0",
149149
"read-last-lines": "^1.1.0",
150150
"requirejs": "^2.3.1",
151+
"run-series": "^1.1.4",
151152
"through2": "^2.0.3",
152153
"true-case-path": "^1.0.2",
153154
"watchify": "^3.10.0",

tasks/bundle.js

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var path = require('path');
22
var glob = require('glob');
3+
var runSeries = require('run-series');
34

45
var constants = require('./util/constants');
56
var common = require('./util/common');
@@ -30,41 +31,60 @@ if(!doesFileExist(constants.pathToCSSBuild) || !doesFileExist(constants.pathToFo
3031
].join('\n'));
3132
}
3233

33-
// Browserify plotly.js
34-
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
35-
standalone: 'Plotly',
36-
debug: DEV,
37-
pathToMinBundle: constants.pathToPlotlyDistMin
34+
// "Browserify" the locales
35+
var localeGlob = path.join(constants.pathToLib, 'locales', '*.js');
36+
glob(localeGlob, function(err, files) {
37+
files.forEach(function(file) {
38+
var outName = 'plotly-locale-' + path.basename(file);
39+
var outPath = path.join(constants.pathToDist, outName);
40+
wrapLocale(file, outPath);
41+
});
3842
});
3943

44+
// list of tasks to pass to run-series to not blow up
45+
// memory consumption.
46+
var tasks = [];
47+
48+
// Browserify plotly.js
49+
tasks.push(function(cb) {
50+
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
51+
standalone: 'Plotly',
52+
debug: DEV,
53+
compressAttrs: true,
54+
pathToMinBundle: constants.pathToPlotlyDistMin
55+
}, cb);
56+
});
4057

4158
// Browserify the geo assets
42-
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
43-
standalone: 'PlotlyGeoAssets'
59+
tasks.push(function(cb) {
60+
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
61+
standalone: 'PlotlyGeoAssets'
62+
}, cb);
4463
});
4564

4665
// Browserify the plotly.js with meta
47-
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
48-
standalone: 'Plotly',
49-
debug: DEV,
50-
then: makeSchema(constants.pathToPlotlyDistWithMeta, constants.pathToSchema)
66+
tasks.push(function(cb) {
67+
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
68+
standalone: 'Plotly',
69+
debug: DEV,
70+
}, function() {
71+
makeSchema(constants.pathToPlotlyDistWithMeta, constants.pathToSchema)();
72+
cb();
73+
});
5174
});
5275

5376
// Browserify the plotly.js partial bundles
5477
constants.partialBundlePaths.forEach(function(pathObj) {
55-
_bundle(pathObj.index, pathObj.dist, {
56-
standalone: 'Plotly',
57-
debug: DEV,
58-
pathToMinBundle: pathObj.distMin
78+
tasks.push(function(cb) {
79+
_bundle(pathObj.index, pathObj.dist, {
80+
standalone: 'Plotly',
81+
debug: DEV,
82+
compressAttrs: true,
83+
pathToMinBundle: pathObj.distMin
84+
}, cb);
5985
});
6086
});
6187

62-
// "Browserify" the locales
63-
var localeGlob = path.join(constants.pathToLib, 'locales', '*.js');
64-
glob(localeGlob, function(err, files) {
65-
files.forEach(function(file) {
66-
var outName = 'plotly-locale-' + path.basename(file);
67-
var outPath = path.join(constants.pathToDist, outName);
68-
wrapLocale(file, outPath);
69-
});
88+
runSeries(tasks, function(err) {
89+
if(err) throw err;
7090
});

tasks/util/browserify_wrapper.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,67 @@ var strictD3 = require('./strict_d3');
2121
*
2222
* Additional option:
2323
* - pathToMinBundle {string} path to destination minified bundle
24+
* - compressAttrs {boolean} do we compress attribute meta?
25+
* @param {function} cb callback
2426
*
2527
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted
2628
* or opts.debug is true. Otherwise outputs two file: one un-minified bundle and
2729
* one minified bundle.
2830
*
2931
* Logs basename of bundle when completed.
3032
*/
31-
module.exports = function _bundle(pathToIndex, pathToBundle, opts) {
33+
module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
3234
opts = opts || {};
3335

34-
// do we output a minified file?
3536
var pathToMinBundle = opts.pathToMinBundle;
36-
var outputMinified = !!pathToMinBundle;
3737

3838
var browserifyOpts = {};
3939
browserifyOpts.standalone = opts.standalone;
4040
browserifyOpts.debug = opts.debug;
41-
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
4241

42+
browserifyOpts.transform = [];
43+
if(opts.compressAttrs) {
44+
browserifyOpts.transform.push(compressAttributes);
45+
}
4346
if(opts.debug) {
4447
browserifyOpts.transform.push(strictD3);
4548
}
4649

4750
var b = browserify(pathToIndex, browserifyOpts);
4851

52+
53+
var pending = opts.pathToMinBundle ? 2 : 1;
54+
55+
function done() {
56+
if(cb && --pending === 0) cb(null);
57+
}
58+
4959
var bundleStream = b.bundle(function(err) {
50-
if(err) throw err;
60+
if(err) {
61+
if(cb) cb(err);
62+
else throw err;
63+
}
5164
});
5265

53-
if(outputMinified) {
66+
if(opts.pathToMinBundle) {
5467
bundleStream
5568
.pipe(minify(constants.uglifyOptions))
5669
.pipe(fs.createWriteStream(pathToMinBundle))
5770
.on('finish', function() {
5871
logger(pathToMinBundle);
72+
done();
5973
});
6074
}
6175

6276
bundleStream
6377
.pipe(fs.createWriteStream(pathToBundle))
6478
.on('finish', function() {
6579
logger(pathToBundle);
66-
if(opts.then) {
67-
opts.then();
68-
}
80+
done();
6981
});
7082
};
7183

7284
function logger(pathToOutput) {
7385
var log = 'ok ' + path.basename(pathToOutput);
74-
7586
console.log(log);
7687
}

0 commit comments

Comments
 (0)