Skip to content

Commit

Permalink
Merge pull request #5527 from plotly/partial-bundle-API
Browse files Browse the repository at this point in the history
Custom bundle script details
  • Loading branch information
archmoj authored Mar 2, 2021
2 parents d0ddf03 + 3693985 commit ed52205
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/index-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-cartesian.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-gl2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-gl3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Plotly.register([
require('./sort'),

// components
require('./calendars')
require('./calendars'),
]);

module.exports = Plotly;
4 changes: 3 additions & 1 deletion tasks/extra_bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ for(var i = 0; i < partialBundlePaths.length; i++) {
index: opts.index,
dist: opts.dist,
distMin: opts.distMin,
traceList: opts.traceList
traceList: opts.traceList,
transformList: opts.transformList,
calendars: opts.calendars
});
}

Expand Down
130 changes: 90 additions & 40 deletions tasks/partial_bundle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var path = require('path');
var minimist = require('minimist');
var runSeries = require('run-series');
var prependFile = require('prepend-file');

Expand All @@ -7,41 +8,78 @@ 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;

var argv = process.argv;

if(argv.length > 2) {
// command line

var traceList = ['scatter']; // added by default
var name;
for(var i = 2; i < argv.length; i++) {
var a = argv[i];

function createList(outList, inList, allList, type) {
for(var i = 0; i < inList.length; i++) {
var t = inList[i];
if(
allTraces.indexOf(a) !== -1 && // requested
traceList.indexOf(a) === -1 // not added before
outList.indexOf(t) === -1 // not added before
) {
traceList.push(a);
if(allList.indexOf(t) === -1) {
console.error(t, 'is not a valid ' + type + '!', 'Valid ' + type + 's are:', allList);
} else {
outList.push(t);
}
}
if(a.indexOf('--name=') === 0) name = a.replace('--name=', '');
}
if(!name) name = 'custom';
traceList = traceList.sort();

return outList.sort();
}

function isFalse(a) {
return (
a === 'none' ||
a === 'false'
);
}

function inputBoolean(a, dflt) {
return !a ? dflt : !isFalse(a);
}

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 unminified = inputBoolean(args.unminified, false);
var out = args.out ? args.out : 'custom';
var traces = inputArray(args.traces, allTraces);
var transforms = inputArray(args.transforms, allTransforms);

var opts = {
traceList: traceList,
name: name,
traceList: createList(['scatter'], traces, allTraces, 'trace'),
transformList: createList([], transforms, allTransforms, 'transform'),

index: path.join(constants.pathToBuild, 'index-' + name + '.js'),
dist: path.join(constants.pathToDist, 'plotly-' + name + '.js'),
distMin: path.join(constants.pathToDist, 'plotly-' + name + '.min.js')
name: out,
index: path.join(constants.pathToLib, 'index-' + out + '.js')
};

if(unminified) {
opts.dist = path.join(constants.pathToDist, 'plotly-' + out + '.js');
} else {
opts.distMin = path.join(constants.pathToDist, 'plotly-' + out + '.min.js');
}

console.log(opts);

opts.calendars = true;
opts.deleteIndex = true;

var tasks = [];

partialBundle(tasks, opts);
Expand All @@ -55,43 +93,55 @@ if(argv.length > 2) {
function partialBundle(tasks, opts) {
var name = opts.name;
var index = opts.index;
var deleteIndex = opts.deleteIndex;
var dist = opts.dist;
var distMin = opts.distMin;
var traceList = opts.traceList;
var transformList = opts.transformList;
var calendars = opts.calendars;

tasks.push(function(done) {
var partialIndex = mainIndex;
allTraces.forEach(function(trace) {
if(traceList.indexOf(trace) === -1) {
var WHITESPACE_BEFORE = '\\s*';
// remove require
var newCode = partialIndex.replace(
new RegExp(
WHITESPACE_BEFORE +
'require\\(\'\\./' + trace + '\'\\),',
'g'), ''
);

// test removal
if(newCode === partialIndex) throw 'Unable to find and drop require for trace: "' + trace + '"';

partialIndex = newCode;

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 + '\'\\),',
'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) {
_bundle(index, dist, {
var bundleOpts = {
standalone: 'Plotly',
deleteIndex: deleteIndex,
pathToMinBundle: distMin
}, function() {
};

_bundle(index, dist, bundleOpts, function() {
var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')');
var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)');

prependFile(dist, headerDist, common.throwOnError);
prependFile(distMin, headerDistMin, common.throwOnError);
if(dist) prependFile(dist, headerDist, common.throwOnError);
if(distMin) prependFile(distMin, headerDistMin, common.throwOnError);

done();
});
Expand Down
46 changes: 32 additions & 14 deletions tasks/util/browserify_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var minify = require('minify-stream');
var derequire = require('derequire');
var through = require('through2');

var constants = require('./constants');
var strictD3 = require('./strict_d3');

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

var b = browserify(pathToIndex, browserifyOpts);
var pending = pathToMinBundle ? 2 : 1;
var pending = (pathToMinBundle && pathToBundle) ? 2 : 1;

function done() {
if(cb && --pending === 0) cb(null);
if(cb && --pending === 0) {
if(opts.deleteIndex) {
console.log('delete', pathToIndex);
fs.unlinkSync(pathToIndex, {});
}

cb(null);
}
}

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

if(pathToMinBundle) {
var minifyOpts = {
ecma: 5,
mangle: true,
output: {
beautify: false,
ascii_only: true
},

sourceMap: false
};

bundleStream
.pipe(applyDerequire())
.pipe(minify(constants.uglifyOptions))
.pipe(minify(minifyOpts))
.pipe(fs.createWriteStream(pathToMinBundle))
.on('finish', function() {
logger(pathToMinBundle);
done();
});
}

bundleStream
.pipe(applyDerequire())
.pipe(fs.createWriteStream(pathToBundle))
.on('finish', function() {
logger(pathToBundle);
done();
});
if(pathToBundle) {
bundleStream
.pipe(applyDerequire())
.pipe(fs.createWriteStream(pathToBundle))
.on('finish', function() {
logger(pathToBundle);
done();
});
}
};

function logger(pathToOutput) {
Expand Down
Loading

0 comments on commit ed52205

Please sign in to comment.