Skip to content

Commit 019fbcb

Browse files
authored
Merge pull request #1094 from plotly/fix-dist
Fix minified dist bundles
2 parents 5b4bee8 + 1e98161 commit 019fbcb

File tree

4 files changed

+95
-75
lines changed

4 files changed

+95
-75
lines changed

tasks/bundle.js

+2-51
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
var fs = require('fs');
2-
var path = require('path');
3-
4-
var browserify = require('browserify');
5-
var UglifyJS = require('uglify-js');
6-
71
var constants = require('./util/constants');
82
var common = require('./util/common');
9-
var compressAttributes = require('./util/compress_attributes');
10-
var patchMinified = require('./util/patch_minified');
11-
var doesFileExist = common.doesFileExist;
3+
var _bundle = require('./util/browserify_wrapper');
124

135
/*
146
* This script takes one argument
@@ -26,6 +18,7 @@ var DEV = (arg === 'dev') || (arg === '--dev');
2618

2719

2820
// Check if style and font build files are there
21+
var doesFileExist = common.doesFileExist;
2922
if(!doesFileExist(constants.pathToCSSBuild) || !doesFileExist(constants.pathToFontSVG)) {
3023
throw new Error([
3124
'build/ is missing one or more files',
@@ -59,45 +52,3 @@ constants.partialBundlePaths.forEach(function(pathObj) {
5952
pathToMinBundle: pathObj.distMin
6053
});
6154
});
62-
63-
function _bundle(pathToIndex, pathToBundle, opts) {
64-
opts = opts || {};
65-
66-
// do we output a minified file?
67-
var pathToMinBundle = opts.pathToMinBundle,
68-
outputMinified = !!pathToMinBundle && !opts.debug;
69-
70-
var browserifyOpts = {};
71-
browserifyOpts.standalone = opts.standalone;
72-
browserifyOpts.debug = opts.debug;
73-
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
74-
75-
var b = browserify(pathToIndex, browserifyOpts),
76-
bundleWriteStream = fs.createWriteStream(pathToBundle);
77-
78-
bundleWriteStream.on('finish', function() {
79-
logger(pathToBundle);
80-
});
81-
82-
b.bundle(function(err, buf) {
83-
if(err) throw err;
84-
85-
if(outputMinified) {
86-
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
87-
minifiedCode = patchMinified(minifiedCode);
88-
89-
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
90-
if(err) throw err;
91-
92-
logger(pathToMinBundle);
93-
});
94-
}
95-
})
96-
.pipe(bundleWriteStream);
97-
}
98-
99-
function logger(pathToOutput) {
100-
var log = 'ok ' + path.basename(pathToOutput);
101-
102-
console.log(log);
103-
}

tasks/cibundle.js

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
var fs = require('fs');
2-
3-
var browserify = require('browserify');
4-
51
var constants = require('./util/constants');
6-
var common = require('./util/common');
7-
var compressAttributes = require('./util/compress_attributes');
2+
var _bundle = require('./util/browserify_wrapper');
83

94
/*
105
* Trimmed down version of ./bundle.js for CI testing
116
*
12-
* Outputs plotly.js bundle in build/ and
13-
* plotly-geo-assets.js bundle in dist/
14-
* in accordance with test/image/index.html
7+
* Outputs:
158
*
9+
* - plotly.js bundle in build/
10+
* - plotly-geo-assets.js bundle in dist/ (in accordance with test/image/index.html)
11+
* - plotly.min.js bundle in dist/ (for requirejs test)
1612
*/
1713

1814

19-
// Browserify plotly.js
20-
browserify(constants.pathToPlotlyIndex, {
15+
// Browserify plotly.js and plotly.min.js
16+
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyBuild, {
2117
standalone: 'Plotly',
22-
transform: [compressAttributes]
23-
})
24-
.bundle(common.throwOnError)
25-
.pipe(fs.createWriteStream(constants.pathToPlotlyBuild));
26-
18+
pathToMinBundle: constants.pathToPlotlyDistMin,
19+
});
2720

2821
// Browserify the geo assets
29-
browserify(constants.pathToPlotlyGeoAssetsSrc, {
22+
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
3023
standalone: 'PlotlyGeoAssets'
31-
})
32-
.bundle(common.throwOnError)
33-
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist));
24+
});

tasks/util/browserify_wrapper.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
4+
var browserify = require('browserify');
5+
var UglifyJS = require('uglify-js');
6+
7+
var constants = require('./constants');
8+
var compressAttributes = require('./compress_attributes');
9+
var patchMinified = require('./patch_minified');
10+
11+
/** Convenience browserify wrapper
12+
*
13+
* @param {string} pathToIndex path to index file to bundle
14+
* @param {string} pathToBunlde path to destination bundle
15+
*
16+
* @param {object} opts
17+
*
18+
* Browserify options:
19+
* - standalone {string}
20+
* - debug {boolean} [optional]
21+
*
22+
* Additional option:
23+
* - pathToMinBundle {string} path to destination minified bundle
24+
*
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.
28+
*
29+
* Logs basename of bundle when completed.
30+
*/
31+
module.exports = function _bundle(pathToIndex, pathToBundle, opts) {
32+
opts = opts || {};
33+
34+
// do we output a minified file?
35+
var pathToMinBundle = opts.pathToMinBundle,
36+
outputMinified = !!pathToMinBundle && !opts.debug;
37+
38+
var browserifyOpts = {};
39+
browserifyOpts.standalone = opts.standalone;
40+
browserifyOpts.debug = opts.debug;
41+
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
42+
43+
var b = browserify(pathToIndex, browserifyOpts),
44+
bundleWriteStream = fs.createWriteStream(pathToBundle);
45+
46+
bundleWriteStream.on('finish', function() {
47+
logger(pathToBundle);
48+
});
49+
50+
b.bundle(function(err, buf) {
51+
if(err) throw err;
52+
53+
if(outputMinified) {
54+
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
55+
minifiedCode = patchMinified(minifiedCode);
56+
57+
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
58+
if(err) throw err;
59+
60+
logger(pathToMinBundle);
61+
});
62+
}
63+
})
64+
.pipe(bundleWriteStream);
65+
};
66+
67+
function logger(pathToOutput) {
68+
var log = 'ok ' + path.basename(pathToOutput);
69+
70+
console.log(log);
71+
}

tasks/util/patch_minified.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
var STR_TO_REPLACE = 'require("+a(r)+");';
2-
var STR_NEW = 'require("+ a(r) +");';
1+
var PATTERN = /require\("\+(\w)\((\w)\)\+"\)/;
2+
var NEW_SUBSTR = 'require("+ $1($2) +")';
33

44
/* Uber hacky in-house fix to
55
*
66
* https://github.com/substack/webworkify/issues/29
77
*
88
* so that plotly.min.js loads in Jupyter NBs, more info here:
99
*
10-
* https://github.com/plotly/plotly.py/pull/545
10+
* - https://github.com/plotly/plotly.py/pull/545
11+
* - https://github.com/plotly/plotly.js/pull/914
12+
* - https://github.com/plotly/plotly.js/pull/1094
13+
*
14+
* For example, this routine replaces
15+
* 'require("+o(s)+")' -> 'require("+ o(s) +")'
16+
*
17+
* But works for any 1-letter variable that uglify-js may output.
1118
*
1219
*/
1320
module.exports = function patchMinified(minifiedCode) {
14-
return minifiedCode.replace(STR_TO_REPLACE, STR_NEW);
21+
return minifiedCode.replace(PATTERN, NEW_SUBSTR);
1522
};

0 commit comments

Comments
 (0)