Skip to content

Commit 91bf6af

Browse files
committed
adapt bundle & header tasks for partial bundles
1 parent 3616db9 commit 91bf6af

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

tasks/bundle.js

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require('fs');
2+
var path = require('path');
23

34
var browserify = require('browserify');
45
var UglifyJS = require('uglify-js');
@@ -35,41 +36,55 @@ catch(e) {
3536

3637

3738
// Browserify plotly.js
38-
browserify(constants.pathToPlotlyIndex, {
39-
debug: DEV,
39+
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
4040
standalone: 'Plotly',
41-
transform: [compressAttributes]
42-
})
43-
.bundle(function(err, buf) {
44-
if(err) throw err;
45-
46-
// generate plotly.min.js
47-
if(!DEV) {
48-
fs.writeFile(
49-
constants.pathToPlotlyDistMin,
50-
UglifyJS.minify(buf.toString(), constants.uglifyOptions).code
51-
);
52-
}
53-
})
54-
.pipe(fs.createWriteStream(constants.pathToPlotlyDist));
55-
41+
debug: DEV,
42+
pathToMinBundle: constants.pathToPlotlyDistMin
43+
});
5644

5745
// Browserify the geo assets
58-
browserify(constants.pathToPlotlyGeoAssetsSrc, {
46+
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
5947
standalone: 'PlotlyGeoAssets'
60-
})
61-
.bundle(function(err) {
62-
if(err) throw err;
63-
})
64-
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist));
65-
48+
});
6649

6750
// Browserify the plotly.js with meta
68-
browserify(constants.pathToPlotlyIndex, {
69-
debug: DEV,
70-
standalone: 'Plotly'
71-
})
72-
.bundle(function(err) {
73-
if(err) throw err;
74-
})
75-
.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta));
51+
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
52+
standalone: 'Plotly',
53+
debug: DEV
54+
});
55+
56+
// Browserify the plotly.js partial bundles
57+
constants.partialBundleNames.forEach(function(name) {
58+
var pathToIndex = path.join(constants.pathToLib, 'index-' + name + '.js'),
59+
pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
60+
pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');
61+
62+
_bundle(pathToIndex, pathToBundle, {
63+
standalone: 'Plotly',
64+
debug: DEV,
65+
pathToMinBundle: pathToMinBundle
66+
});
67+
});
68+
69+
function _bundle(pathToIndex, pathToBundle, opts) {
70+
opts = opts || {};
71+
72+
// do we output a minified file?
73+
var outputMinified = !!opts.pathToMinBundle && !opts.debug;
74+
75+
var browserifyOpts = {};
76+
browserifyOpts.standalone = opts.standalone;
77+
browserifyOpts.debug = opts.debug;
78+
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
79+
80+
browserify(pathToIndex, browserifyOpts).bundle(function(err, buf) {
81+
if(err) throw err;
82+
83+
if(outputMinified) {
84+
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
85+
86+
fs.writeFile(opts.pathToMinBundle, minifiedCode);
87+
}
88+
})
89+
.pipe(fs.createWriteStream(pathToBundle));
90+
}

tasks/header.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ var pathsDist = [
1717
constants.pathToPlotlyGeoAssetsDist
1818
];
1919

20+
constants.partialBundleNames.forEach(function(name) {
21+
var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
22+
pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');
23+
24+
pathsDist.push(pathToBundle, pathToMinBundle);
25+
});
26+
2027
function headerLicense(path) {
2128
prependFile(path, constants.licenseDist + '\n', function(err) {
2229
if(err) throw err;

tasks/util/constants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
pathToSrc: pathToSrc,
2121
pathToLib: pathToLib,
2222
pathToBuild: pathToBuild,
23+
pathToDist: pathToDist,
2324

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

32+
partialBundleNames: [
33+
'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox'
34+
],
35+
3136
pathToTopojsonSrc: pathToTopojsonSrc,
3237
pathToTopojsonDist: path.join(pathToDist, 'topojson/'),
3338
pathToPlotlyGeoAssetsSrc: path.join(pathToSrc, 'assets/geo_assets.js'),

0 commit comments

Comments
 (0)