Skip to content

Introducing partial dist bundles #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 22, 2016
19 changes: 19 additions & 0 deletions lib/index-basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';


var Core = require('./core');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth an inline comment to note that Core pulls in scatter. If I were looking to bundle my own, I'd start by copy/pasting this file, and I had to track down the inclusion of scatter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(To clarify, I had to stop and think whether core pulled it in or whether it was something pulled in indirectly by bar/pie.)


Core.register([
require('./bar'),
require('./pie')
]);

module.exports = Core;
25 changes: 25 additions & 0 deletions lib/index-cartesian.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Core = require('./core');

Core.register([
require('./bar'),
require('./box'),
require('./heatmap'),
require('./histogram'),
require('./histogram2d'),
require('./histogram2dcontour'),
require('./pie'),
require('./contour'),
require('./scatterternary')
]);

module.exports = Core;
18 changes: 18 additions & 0 deletions lib/index-geo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Core = require('./core');

Core.register([
require('./scattergeo'),
require('./choropleth')
]);

module.exports = Core;
20 changes: 20 additions & 0 deletions lib/index-gl2d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Core = require('./core');

// Load all trace modules
Core.register([
require('./scattergl'),
require('./heatmapgl'),
require('./contourgl')
]);

module.exports = Core;
19 changes: 19 additions & 0 deletions lib/index-gl3d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Core = require('./core');

Core.register([
require('./scatter3d'),
require('./surface'),
require('./mesh3d')
]);

module.exports = Core;
17 changes: 17 additions & 0 deletions lib/index-mapbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var Core = require('./core');

Core.register([
require('./scattermapbox')
]);

module.exports = Core;
97 changes: 65 additions & 32 deletions tasks/bundle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');

var browserify = require('browserify');
var UglifyJS = require('uglify-js');
Expand Down Expand Up @@ -35,41 +36,73 @@ catch(e) {


// Browserify plotly.js
browserify(constants.pathToPlotlyIndex, {
debug: DEV,
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
standalone: 'Plotly',
transform: [compressAttributes]
})
.bundle(function(err, buf) {
if(err) throw err;

// generate plotly.min.js
if(!DEV) {
fs.writeFile(
constants.pathToPlotlyDistMin,
UglifyJS.minify(buf.toString(), constants.uglifyOptions).code
);
}
})
.pipe(fs.createWriteStream(constants.pathToPlotlyDist));

debug: DEV,
pathToMinBundle: constants.pathToPlotlyDistMin
});

// Browserify the geo assets
browserify(constants.pathToPlotlyGeoAssetsSrc, {
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
standalone: 'PlotlyGeoAssets'
})
.bundle(function(err) {
if(err) throw err;
})
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist));

});

// Browserify the plotly.js with meta
browserify(constants.pathToPlotlyIndex, {
debug: DEV,
standalone: 'Plotly'
})
.bundle(function(err) {
if(err) throw err;
})
.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta));
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
standalone: 'Plotly',
debug: DEV
});

// Browserify the plotly.js partial bundles
constants.partialBundleNames.forEach(function(name) {
var pathToIndex = path.join(constants.pathToLib, 'index-' + name + '.js'),
pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');

_bundle(pathToIndex, pathToBundle, {
standalone: 'Plotly',
debug: DEV,
pathToMinBundle: pathToMinBundle
});
});

function _bundle(pathToIndex, pathToBundle, opts) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: what about moving this bundle function to its own file and exporting it? Then tasks/bundle.js would require and use _bundle. Which is all the same, except that it would make room for a standalone bundler script to also just use this function. As long as there were a stable, guaranteed location for this script, a standalone bundler repo would probably just require this script and not have to reimplement it.

Other alternative is that a standalone bundler would implement this and get required by plotly.js as a dev dependency to do the bundling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: what about moving this bundle function to its own file and exporting it?

I thought about too. But I think it's worth spending more time perfecting our bundling scripts until we decide if/when/how we expose a bundler module.

Other alternative is that a standalone bundler would implement this and get required by plotly.js as a dev dependency to do the bundling.

This would get my vote at the moment.

opts = opts || {};

// do we output a minified file?
var pathToMinBundle = opts.pathToMinBundle,
outputMinified = !!pathToMinBundle && !opts.debug;

var browserifyOpts = {};
browserifyOpts.standalone = opts.standalone;
browserifyOpts.debug = opts.debug;
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];

var b = browserify(pathToIndex, browserifyOpts),
bundleWriteStream = fs.createWriteStream(pathToBundle);

bundleWriteStream.on('finish', function() {
logger(pathToBundle);
});

b.bundle(function(err, buf) {
if(err) throw err;

if(outputMinified) {
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;

fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
if(err) throw err;

logger(pathToMinBundle);
});
}
})
.pipe(bundleWriteStream);
}

function logger(pathToOutput) {
var log = 'ok ' + path.basename(pathToOutput);

console.log(log);
}
7 changes: 7 additions & 0 deletions tasks/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ var pathsDist = [
constants.pathToPlotlyGeoAssetsDist
];

constants.partialBundleNames.forEach(function(name) {
var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');

pathsDist.push(pathToBundle, pathToMinBundle);
});

function headerLicense(path) {
prependFile(path, constants.licenseDist + '\n', function(err) {
if(err) throw err;
Expand Down
5 changes: 5 additions & 0 deletions tasks/util/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
pathToSrc: pathToSrc,
pathToLib: pathToLib,
pathToBuild: pathToBuild,
pathToDist: pathToDist,

pathToPlotlyIndex: path.join(pathToLib, 'index.js'),
pathToPlotlyCore: path.join(pathToSrc, 'core.js'),
Expand All @@ -28,6 +29,10 @@ module.exports = {
pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'),
pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'),

partialBundleNames: [
'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox'
],

pathToTopojsonSrc: pathToTopojsonSrc,
pathToTopojsonDist: path.join(pathToDist, 'topojson/'),
pathToPlotlyGeoAssetsSrc: path.join(pathToSrc, 'assets/geo_assets.js'),
Expand Down