-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix minified dist bundles #1094
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2acb387
tasks: generalise patch minified task
etpinard e16d2f7
tasks: factor out _bundle wrapper in own tasks/util/ module
etpinard d4f216f
tasks: make cibundle output plotly.min.js
etpinard f958b4f
doc: improve comment in patch minified tasks util
etpinard 1e98161
tasks: use regex instead of loop over alphabet in patch minified step
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,24 @@ | ||
var fs = require('fs'); | ||
|
||
var browserify = require('browserify'); | ||
|
||
var constants = require('./util/constants'); | ||
var common = require('./util/common'); | ||
var compressAttributes = require('./util/compress_attributes'); | ||
var _bundle = require('./util/browserify_wrapper'); | ||
|
||
/* | ||
* Trimmed down version of ./bundle.js for CI testing | ||
* | ||
* Outputs plotly.js bundle in build/ and | ||
* plotly-geo-assets.js bundle in dist/ | ||
* in accordance with test/image/index.html | ||
* Outputs: | ||
* | ||
* - plotly.js bundle in build/ | ||
* - plotly-geo-assets.js bundle in dist/ (in accordance with test/image/index.html) | ||
* - plotly.min.js bundle in dist/ (for requirejs test) | ||
*/ | ||
|
||
|
||
// Browserify plotly.js | ||
browserify(constants.pathToPlotlyIndex, { | ||
// Browserify plotly.js and plotly.min.js | ||
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyBuild, { | ||
standalone: 'Plotly', | ||
transform: [compressAttributes] | ||
}) | ||
.bundle(common.throwOnError) | ||
.pipe(fs.createWriteStream(constants.pathToPlotlyBuild)); | ||
|
||
pathToMinBundle: constants.pathToPlotlyDistMin, | ||
}); | ||
|
||
// Browserify the geo assets | ||
browserify(constants.pathToPlotlyGeoAssetsSrc, { | ||
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, { | ||
standalone: 'PlotlyGeoAssets' | ||
}) | ||
.bundle(common.throwOnError) | ||
.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist)); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var browserify = require('browserify'); | ||
var UglifyJS = require('uglify-js'); | ||
|
||
var constants = require('./constants'); | ||
var compressAttributes = require('./compress_attributes'); | ||
var patchMinified = require('./patch_minified'); | ||
|
||
/** Convenience browserify wrapper | ||
* | ||
* @param {string} pathToIndex path to index file to bundle | ||
* @param {string} pathToBunlde path to destination bundle | ||
* | ||
* @param {object} opts | ||
* | ||
* Browserify options: | ||
* - standalone {string} | ||
* - debug {boolean} [optional] | ||
* | ||
* Additional option: | ||
* - pathToMinBundle {string} path to destination minified bundle | ||
* | ||
* 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. | ||
* | ||
* Logs basename of bundle when completed. | ||
*/ | ||
module.exports = function _bundle(pathToIndex, pathToBundle, opts) { | ||
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; | ||
minifiedCode = patchMinified(minifiedCode); | ||
|
||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
var STR_TO_REPLACE = 'require("+a(r)+");'; | ||
var STR_NEW = 'require("+ a(r) +");'; | ||
var PATTERN = /require\("\+(\w)\((\w)\)\+"\)/; | ||
var NEW_SUBSTR = 'require("+ $1($2) +")'; | ||
|
||
/* Uber hacky in-house fix to | ||
* | ||
* https://github.com/substack/webworkify/issues/29 | ||
* | ||
* so that plotly.min.js loads in Jupyter NBs, more info here: | ||
* | ||
* https://github.com/plotly/plotly.py/pull/545 | ||
* - https://github.com/plotly/plotly.py/pull/545 | ||
* - https://github.com/plotly/plotly.js/pull/914 | ||
* - https://github.com/plotly/plotly.js/pull/1094 | ||
* | ||
* For example, this routine replaces | ||
* 'require("+o(s)+")' -> 'require("+ o(s) +")' | ||
* | ||
* But works for any 1-letter variable that uglify-js may output. | ||
* | ||
*/ | ||
module.exports = function patchMinified(minifiedCode) { | ||
return minifiedCode.replace(STR_TO_REPLACE, STR_NEW); | ||
return minifiedCode.replace(PATTERN, NEW_SUBSTR); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉