Skip to content

Commit ed52205

Browse files
authored
Merge pull request #5527 from plotly/partial-bundle-API
Custom bundle script details
2 parents d0ddf03 + 3693985 commit ed52205

14 files changed

+158
-81
lines changed

lib/index-basic.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Plotly.register([
1414
require('./sort'),
1515

1616
// components
17-
require('./calendars')
17+
require('./calendars'),
1818
]);
1919

2020
module.exports = Plotly;

lib/index-cartesian.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Plotly.register([
2323
require('./sort'),
2424

2525
// components
26-
require('./calendars')
26+
require('./calendars'),
2727
]);
2828

2929
module.exports = Plotly;

lib/index-finance.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Plotly.register([
2121
require('./sort'),
2222

2323
// components
24-
require('./calendars')
24+
require('./calendars'),
2525
]);
2626

2727
module.exports = Plotly;

lib/index-geo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Plotly.register([
1414
require('./sort'),
1515

1616
// components
17-
require('./calendars')
17+
require('./calendars'),
1818
]);
1919

2020
module.exports = Plotly;

lib/index-gl2d.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Plotly.register([
1717
require('./sort'),
1818

1919
// components
20-
require('./calendars')
20+
require('./calendars'),
2121
]);
2222

2323
module.exports = Plotly;

lib/index-gl3d.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Plotly.register([
1919
require('./sort'),
2020

2121
// components
22-
require('./calendars')
22+
require('./calendars'),
2323
]);
2424

2525
module.exports = Plotly;

lib/index-mapbox.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Plotly.register([
1515
require('./sort'),
1616

1717
// components
18-
require('./calendars')
18+
require('./calendars'),
1919
]);
2020

2121
module.exports = Plotly;

lib/index-strict.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Plotly.register([
4848
require('./sort'),
4949

5050
// components
51-
require('./calendars')
51+
require('./calendars'),
5252
]);
5353

5454
module.exports = Plotly;

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Plotly.register([
5757
require('./sort'),
5858

5959
// components
60-
require('./calendars')
60+
require('./calendars'),
6161
]);
6262

6363
module.exports = Plotly;

tasks/extra_bundles.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ for(var i = 0; i < partialBundlePaths.length; i++) {
1515
index: opts.index,
1616
dist: opts.dist,
1717
distMin: opts.distMin,
18-
traceList: opts.traceList
18+
traceList: opts.traceList,
19+
transformList: opts.transformList,
20+
calendars: opts.calendars
1921
});
2022
}
2123

tasks/partial_bundle.js

+90-40
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,41 +8,78 @@ 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;
14-
15-
if(argv.length > 2) {
16-
// command line
17-
18-
var traceList = ['scatter']; // added by default
19-
var name;
20-
for(var i = 2; i < argv.length; i++) {
21-
var a = argv[i];
22-
15+
function createList(outList, inList, allList, type) {
16+
for(var i = 0; i < inList.length; i++) {
17+
var t = inList[i];
2318
if(
24-
allTraces.indexOf(a) !== -1 && // requested
25-
traceList.indexOf(a) === -1 // not added before
19+
outList.indexOf(t) === -1 // not added before
2620
) {
27-
traceList.push(a);
21+
if(allList.indexOf(t) === -1) {
22+
console.error(t, 'is not a valid ' + type + '!', 'Valid ' + type + 's are:', allList);
23+
} else {
24+
outList.push(t);
25+
}
2826
}
29-
if(a.indexOf('--name=') === 0) name = a.replace('--name=', '');
3027
}
31-
if(!name) name = 'custom';
32-
traceList = traceList.sort();
28+
29+
return outList.sort();
30+
}
31+
32+
function isFalse(a) {
33+
return (
34+
a === 'none' ||
35+
a === 'false'
36+
);
37+
}
38+
39+
function inputBoolean(a, dflt) {
40+
return !a ? dflt : !isFalse(a);
41+
}
42+
43+
function inputArray(a, dflt) {
44+
dflt = dflt.slice();
45+
46+
return (
47+
isFalse(a) ? [] :
48+
!a || a === 'all' ? dflt :
49+
a.split(',')
50+
);
51+
}
52+
53+
if(process.argv.length > 2) {
54+
// command line
55+
56+
var args = minimist(process.argv.slice(2), {});
57+
58+
// parse arguments
59+
var unminified = inputBoolean(args.unminified, false);
60+
var out = args.out ? args.out : 'custom';
61+
var traces = inputArray(args.traces, allTraces);
62+
var transforms = inputArray(args.transforms, allTransforms);
3363

3464
var opts = {
35-
traceList: traceList,
36-
name: name,
65+
traceList: createList(['scatter'], traces, allTraces, 'trace'),
66+
transformList: createList([], transforms, allTransforms, 'transform'),
3767

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')
68+
name: out,
69+
index: path.join(constants.pathToLib, 'index-' + out + '.js')
4170
};
4271

72+
if(unminified) {
73+
opts.dist = path.join(constants.pathToDist, 'plotly-' + out + '.js');
74+
} else {
75+
opts.distMin = path.join(constants.pathToDist, 'plotly-' + out + '.min.js');
76+
}
77+
4378
console.log(opts);
4479

80+
opts.calendars = true;
81+
opts.deleteIndex = true;
82+
4583
var tasks = [];
4684

4785
partialBundle(tasks, opts);
@@ -55,43 +93,55 @@ if(argv.length > 2) {
5593
function partialBundle(tasks, opts) {
5694
var name = opts.name;
5795
var index = opts.index;
96+
var deleteIndex = opts.deleteIndex;
5897
var dist = opts.dist;
5998
var distMin = opts.distMin;
6099
var traceList = opts.traceList;
100+
var transformList = opts.transformList;
101+
var calendars = opts.calendars;
61102

62103
tasks.push(function(done) {
63104
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;
105+
106+
var all = ['calendars'].concat(allTransforms).concat(allTraces);
107+
var includes = (calendars ? ['calendars'] : []).concat(transformList).concat(traceList);
108+
var excludes = all.filter(function(e) { return includes.indexOf(e) === -1; });
109+
110+
excludes.forEach(function(t) {
111+
var WHITESPACE_BEFORE = '\\s*';
112+
// remove require
113+
var newCode = partialIndex.replace(
114+
new RegExp(
115+
WHITESPACE_BEFORE +
116+
'require\\(\'\\./' + t + '\'\\),',
117+
'g'), ''
118+
);
119+
120+
// test removal
121+
if(newCode === partialIndex) {
122+
console.error('Unable to find and drop require for ' + t);
123+
throw 'Error generating index for partial bundle!';
79124
}
125+
126+
partialIndex = newCode;
80127
});
81128

82129
common.writeFile(index, partialIndex, done);
83130
});
84131

85132
tasks.push(function(done) {
86-
_bundle(index, dist, {
133+
var bundleOpts = {
87134
standalone: 'Plotly',
135+
deleteIndex: deleteIndex,
88136
pathToMinBundle: distMin
89-
}, function() {
137+
};
138+
139+
_bundle(index, dist, bundleOpts, function() {
90140
var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')');
91141
var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)');
92142

93-
prependFile(dist, headerDist, common.throwOnError);
94-
prependFile(distMin, headerDistMin, common.throwOnError);
143+
if(dist) prependFile(dist, headerDist, common.throwOnError);
144+
if(distMin) prependFile(distMin, headerDistMin, common.throwOnError);
95145

96146
done();
97147
});

tasks/util/browserify_wrapper.js

+32-14
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var minify = require('minify-stream');
66
var derequire = require('derequire');
77
var through = require('through2');
88

9-
var constants = require('./constants');
109
var strictD3 = require('./strict_d3');
1110

1211
/** Convenience browserify wrapper
@@ -22,9 +21,8 @@ var strictD3 = require('./strict_d3');
2221
* - noCompress {boolean} skip attribute meta compression?
2322
* @param {function} cb callback
2423
*
25-
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted
26-
* or opts.debug is true. Otherwise outputs two file: one un-minified bundle and
27-
* one minified bundle.
24+
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted.
25+
* Otherwise outputs two file: one un-minified bundle and one minified bundle.
2826
*
2927
* Logs basename of bundle when completed.
3028
*/
@@ -47,10 +45,17 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
4745
}
4846

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

5250
function done() {
53-
if(cb && --pending === 0) cb(null);
51+
if(cb && --pending === 0) {
52+
if(opts.deleteIndex) {
53+
console.log('delete', pathToIndex);
54+
fs.unlinkSync(pathToIndex, {});
55+
}
56+
57+
cb(null);
58+
}
5459
}
5560

5661
var bundleStream = b.bundle(function(err) {
@@ -61,23 +66,36 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) {
6166
});
6267

6368
if(pathToMinBundle) {
69+
var minifyOpts = {
70+
ecma: 5,
71+
mangle: true,
72+
output: {
73+
beautify: false,
74+
ascii_only: true
75+
},
76+
77+
sourceMap: false
78+
};
79+
6480
bundleStream
6581
.pipe(applyDerequire())
66-
.pipe(minify(constants.uglifyOptions))
82+
.pipe(minify(minifyOpts))
6783
.pipe(fs.createWriteStream(pathToMinBundle))
6884
.on('finish', function() {
6985
logger(pathToMinBundle);
7086
done();
7187
});
7288
}
7389

74-
bundleStream
75-
.pipe(applyDerequire())
76-
.pipe(fs.createWriteStream(pathToBundle))
77-
.on('finish', function() {
78-
logger(pathToBundle);
79-
done();
80-
});
90+
if(pathToBundle) {
91+
bundleStream
92+
.pipe(applyDerequire())
93+
.pipe(fs.createWriteStream(pathToBundle))
94+
.on('finish', function() {
95+
logger(pathToBundle);
96+
done();
97+
});
98+
}
8199
};
82100

83101
function logger(pathToOutput) {

0 commit comments

Comments
 (0)