-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathpartial_bundle.js
143 lines (114 loc) · 4.15 KB
/
partial_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var path = require('path');
var minimist = require('minimist');
var runSeries = require('run-series');
var prependFile = require('prepend-file');
var constants = require('./util/constants');
var common = require('./util/common');
var _bundle = require('./util/browserify_wrapper');
var header = constants.licenseDist + '\n';
var allTransforms = constants.allTransforms;
var allTraces = constants.allTraces;
var mainIndex = constants.mainIndex;
function createList(outList, inList, allList, type) {
for(var i = 0; i < inList.length; i++) {
var t = inList[i];
if(
outList.indexOf(t) === -1 // not added before
) {
if(allList.indexOf(t) === -1) {
console.error(t, 'is not a valid ' + type + '!', 'Valid ' + type + 's are:', allList);
} else {
outList.push(t);
}
}
}
return outList.sort();
}
function isFalse(a) {
return (
a === 'none' ||
a === 'false'
);
}
function inputArray(a, dflt) {
dflt = dflt.slice();
return (
isFalse(a) ? [] :
!a || a === 'all' ? dflt :
a.split(',')
);
}
if(process.argv.length > 2) {
// command line
var args = minimist(process.argv.slice(2), {});
// parse arguments
var out = args.out ? args.out : 'custom';
var traces = inputArray(args.traces, allTraces);
var transforms = inputArray(args.transforms, allTransforms);
var opts = {
traceList: createList(['scatter'], traces, allTraces, 'trace'),
transformList: createList([], transforms, allTransforms, 'transform'),
name: out,
index: path.join(constants.pathToLib, 'index-' + out + '.js'),
sourceMap: path.join(constants.pathToDist, 'plotly-' + out + '.js.map'),
dist: path.join(constants.pathToDist, 'plotly-' + out + '.js'),
distMin: path.join(constants.pathToDist, 'plotly-' + out + '.min.js')
};
console.log(opts);
opts.calendars = true;
var tasks = [];
partialBundle(tasks, opts);
runSeries(tasks, function(err) {
if(err) throw err;
});
}
// Browserify the plotly.js partial bundles
function partialBundle(tasks, opts) {
var name = opts.name;
var index = opts.index;
var dist = opts.dist;
var distMin = opts.distMin;
var traceList = opts.traceList;
var transformList = opts.transformList;
var calendars = opts.calendars;
var sourceMap = opts.sourceMap;
tasks.push(function(done) {
var partialIndex = mainIndex;
var all = ['calendars'].concat(allTransforms).concat(allTraces);
var includes = (calendars ? ['calendars'] : []).concat(transformList).concat(traceList);
var excludes = all.filter(function(e) { return includes.indexOf(e) === -1; });
excludes.forEach(function(t) {
var WHITESPACE_BEFORE = '\\s*';
// remove require
var newCode = partialIndex.replace(
new RegExp(
WHITESPACE_BEFORE +
'require\\(\'\\./' + t + '\'\\)' +
(t === 'calendars' ? '' : ','), // there is no comma after calendars require
'g'), ''
);
// test removal
if(newCode === partialIndex) {
console.error('Unable to find and drop require for ' + t);
throw 'Error generating index for partial bundle!';
}
partialIndex = newCode;
});
common.writeFile(index, partialIndex, done);
});
tasks.push(function(done) {
var bundleOpts = {
pathToSourceMap: sourceMap,
standalone: 'Plotly',
pathToMinBundle: distMin
};
_bundle(index, dist, bundleOpts, function() {
var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')');
var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)');
if(dist) prependFile(dist, headerDist, common.throwOnError);
if(distMin) prependFile(distMin, headerDistMin, common.throwOnError);
done();
});
});
}
module.exports = partialBundle;