Skip to content

Commit 9fe3255

Browse files
committed
improve partial bundle script
1 parent d0ddf03 commit 9fe3255

File tree

3 files changed

+126
-46
lines changed

3 files changed

+126
-46
lines changed

tasks/partial_bundle.js

Lines changed: 101 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var path = require('path');
2+
var minimist = require('minimist');
23
var runSeries = require('run-series');
34
var prependFile = require('prepend-file');
45

@@ -7,39 +8,90 @@ var common = require('./util/common');
78
var _bundle = require('./util/browserify_wrapper');
89

910
var header = constants.licenseDist + '\n';
11+
var allTransforms = constants.allTransforms;
1012
var allTraces = constants.allTraces;
1113
var mainIndex = constants.mainIndex;
1214

13-
var argv = process.argv;
15+
function isFalse(a) {
16+
return (
17+
!a ||
18+
a === 'none' ||
19+
a === 'false'
20+
);
21+
}
22+
23+
function inputArray(a, allOptions) {
24+
return isFalse(a) ? [] : (
25+
a === 'all' || typeof a !== 'string'
26+
) ? allOptions.slice() : a.split(',');
27+
}
1428

15-
if(argv.length > 2) {
29+
if(process.argv.length > 2) {
1630
// command line
1731

18-
var traceList = ['scatter']; // added by default
19-
var name;
20-
for(var i = 2; i < argv.length; i++) {
21-
var a = argv[i];
32+
var args = minimist(process.argv.slice(2), {});
33+
34+
// parse arguments
35+
var out = args.out ? args.out : 'custom';
36+
var traces = inputArray(args.traces, allTraces);
37+
var transforms = inputArray(args.transforms, allTransforms);
38+
var calendars = isFalse(args.calendars) ? false : true;
39+
var sourcemaps = !isFalse(args.sourcemaps) ? true : false;
40+
var minified = isFalse(args.minified) ? false : true;
41+
var keepindex = isFalse(args.keepindex) ? false : true;
42+
43+
var i, t;
2244

45+
var traceList = ['scatter']; // added by default
46+
for(i = 0; i < traces.length; i++) {
47+
t = traces[i];
2348
if(
24-
allTraces.indexOf(a) !== -1 && // requested
25-
traceList.indexOf(a) === -1 // not added before
49+
traceList.indexOf(t) === -1 // not added before
2650
) {
27-
traceList.push(a);
51+
if(allTraces.indexOf(t) === -1) {
52+
console.error(t, 'is not a valid trace!', 'Valid traces are:', allTraces);
53+
} else {
54+
traceList.push(t);
55+
}
2856
}
29-
if(a.indexOf('--name=') === 0) name = a.replace('--name=', '');
3057
}
31-
if(!name) name = 'custom';
3258
traceList = traceList.sort();
3359

60+
var transformList = [];
61+
for(i = 0; i < transforms.length; i++) {
62+
t = transforms[i];
63+
if(
64+
transformList.indexOf(t) === -1 // not added before
65+
) {
66+
if(allTransforms.indexOf(t) === -1) {
67+
console.error(t, 'is not a valid transform!', 'Valid transforms are:', allTransforms);
68+
} else {
69+
transformList.push(t);
70+
}
71+
}
72+
}
73+
transformList = transformList.sort();
74+
3475
var opts = {
3576
traceList: traceList,
36-
name: name,
77+
transformList: transformList,
78+
calendars: calendars,
79+
sourcemaps: sourcemaps,
3780

38-
index: path.join(constants.pathToBuild, 'index-' + name + '.js'),
39-
dist: path.join(constants.pathToDist, 'plotly-' + name + '.js'),
40-
distMin: path.join(constants.pathToDist, 'plotly-' + name + '.min.js')
81+
name: out,
82+
index: path.join(constants.pathToLib, 'index-' + out + '.js')
4183
};
4284

85+
if(minified) {
86+
opts.distMin = path.join(constants.pathToDist, 'plotly-' + out + '.min.js');
87+
} else {
88+
opts.dist = path.join(constants.pathToDist, 'plotly-' + out + '.js');
89+
}
90+
91+
if(!keepindex) {
92+
opts.deleteIndex = true;
93+
}
94+
4395
console.log(opts);
4496

4597
var tasks = [];
@@ -58,40 +110,55 @@ function partialBundle(tasks, opts) {
58110
var dist = opts.dist;
59111
var distMin = opts.distMin;
60112
var traceList = opts.traceList;
113+
var transformList = opts.transformList;
114+
var calendars = opts.calendars;
115+
var sourcemaps = opts.sourcemaps;
116+
var deleteIndex = opts.deleteIndex;
61117

62118
tasks.push(function(done) {
63119
var partialIndex = mainIndex;
64-
allTraces.forEach(function(trace) {
65-
if(traceList.indexOf(trace) === -1) {
66-
var WHITESPACE_BEFORE = '\\s*';
67-
// remove require
68-
var newCode = partialIndex.replace(
69-
new RegExp(
70-
WHITESPACE_BEFORE +
71-
'require\\(\'\\./' + trace + '\'\\),',
72-
'g'), ''
73-
);
74-
75-
// test removal
76-
if(newCode === partialIndex) throw 'Unable to find and drop require for trace: "' + trace + '"';
77-
78-
partialIndex = newCode;
120+
121+
var all = ['calendars'].concat(allTransforms).concat(allTraces);
122+
var includes = (calendars ? ['calendars'] : []).concat(transformList).concat(traceList);
123+
var excludes = all.filter(function(e) { return includes.indexOf(e) === -1; });
124+
125+
excludes.forEach(function(t) {
126+
var WHITESPACE_BEFORE = '\\s*';
127+
// remove require
128+
var newCode = partialIndex.replace(
129+
new RegExp(
130+
WHITESPACE_BEFORE +
131+
'require\\(\'\\./' + t + '\'\\)' +
132+
(t === 'calendars' ? '' : ','), // there is no comma after calendars require
133+
'g'), ''
134+
);
135+
136+
// test removal
137+
if(newCode === partialIndex) {
138+
console.error('Unable to find and drop require for ' + t);
139+
throw 'Error generating index for partial bundle!';
79140
}
141+
142+
partialIndex = newCode;
80143
});
81144

82145
common.writeFile(index, partialIndex, done);
83146
});
84147

85148
tasks.push(function(done) {
86-
_bundle(index, dist, {
149+
var bundleOpts = {
150+
debug: sourcemaps,
151+
deleteIndex: deleteIndex,
87152
standalone: 'Plotly',
88153
pathToMinBundle: distMin
89-
}, function() {
154+
};
155+
156+
_bundle(index, dist, bundleOpts, function() {
90157
var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')');
91158
var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)');
92159

93-
prependFile(dist, headerDist, common.throwOnError);
94-
prependFile(distMin, headerDistMin, common.throwOnError);
160+
if(dist) prependFile(dist, headerDist, common.throwOnError);
161+
if(distMin) prependFile(distMin, headerDistMin, common.throwOnError);
95162

96163
done();
97164
});

tasks/util/browserify_wrapper.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
4747
}
4848

4949
var b = browserify(pathToIndex, browserifyOpts);
50-
var pending = pathToMinBundle ? 2 : 1;
50+
var pending = (pathToMinBundle && pathToBundle) ? 2 : 1;
5151

5252
function done() {
53-
if(cb && --pending === 0) cb(null);
53+
if(cb && --pending === 0) {
54+
if(opts.deleteIndex) {
55+
console.log('delete', pathToIndex);
56+
fs.unlinkSync(pathToIndex, {});
57+
}
58+
59+
cb(null);
60+
}
5461
}
5562

5663
var bundleStream = b.bundle(function(err) {
@@ -71,13 +78,15 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
7178
});
7279
}
7380

74-
bundleStream
75-
.pipe(applyDerequire())
76-
.pipe(fs.createWriteStream(pathToBundle))
77-
.on('finish', function() {
78-
logger(pathToBundle);
79-
done();
80-
});
81+
if(pathToBundle) {
82+
bundleStream
83+
.pipe(applyDerequire())
84+
.pipe(fs.createWriteStream(pathToBundle))
85+
.on('finish', function() {
86+
logger(pathToBundle);
87+
done();
88+
});
89+
}
8190
};
8291

8392
function logger(pathToOutput) {

tasks/util/constants.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ var pathToBuild = path.join(pathToRoot, 'build/');
1212

1313
var pathToPlotlyIndex = path.join(pathToLib, 'index.js');
1414
var mainIndex = fs.readFileSync(pathToPlotlyIndex, 'utf-8');
15-
var pathToPlotlyTraces = path.join(pathToSrc, 'traces');
16-
var allTraces = fs.readdirSync(pathToPlotlyTraces);
15+
var allTraces = fs.readdirSync(path.join(pathToSrc, 'traces'));
16+
var allTransforms = fs.readdirSync(path.join(pathToSrc, 'transforms'))
17+
.map(function(e) { return e.replace('.js', ''); });
18+
allTransforms.splice(allTransforms.indexOf('helpers'), 1);
1719

1820
var pathToTopojsonSrc;
1921
try {
@@ -140,6 +142,8 @@ function makePartialBundleOpts(name) {
140142
return {
141143
name: name,
142144
traceList: partialBundleTraces[name],
145+
transformList: allTransforms,
146+
calendars: true,
143147
index: path.join(pathToLib, 'index-' + name + '.js'),
144148
dist: path.join(pathToDist, 'plotly-' + name + '.js'),
145149
distMin: path.join(pathToDist, 'plotly-' + name + '.min.js')
@@ -157,10 +161,10 @@ module.exports = {
157161
pathToBuild: pathToBuild,
158162
pathToDist: pathToDist,
159163

164+
allTransforms: allTransforms,
160165
allTraces: allTraces,
161166
mainIndex: mainIndex,
162167
pathToPlotlyIndex: pathToPlotlyIndex,
163-
pathToPlotlyTraces: pathToPlotlyTraces,
164168
pathToPlotlyCore: path.join(pathToSrc, 'core.js'),
165169
pathToPlotlyVersion: path.join(pathToSrc, 'version.js'),
166170
pathToPlotlyBuild: path.join(pathToBuild, 'plotly.js'),

0 commit comments

Comments
 (0)