From 2acb38742c7073b06e5ff82afe390fea9c668c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 27 Oct 2016 15:05:30 -0400 Subject: [PATCH 1/5] tasks: generalise patch minified task - bumping to node v6 disrupted the "a(r)" assumption --- tasks/util/patch_minified.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tasks/util/patch_minified.js b/tasks/util/patch_minified.js index b03a6357990..0a230619c2d 100644 --- a/tasks/util/patch_minified.js +++ b/tasks/util/patch_minified.js @@ -1,5 +1,6 @@ -var STR_TO_REPLACE = 'require("+a(r)+");'; -var STR_NEW = 'require("+ a(r) +");'; +var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split(''); +var FRONT = 'require("+'; +var BACK = '+");'; /* Uber hacky in-house fix to * @@ -11,5 +12,20 @@ var STR_NEW = 'require("+ a(r) +");'; * */ module.exports = function patchMinified(minifiedCode) { - return minifiedCode.replace(STR_TO_REPLACE, STR_NEW); + for(var i = 0; i < ALPHABET.length; i++) { + var li = ALPHABET[i]; + + for(var j = 0; j < ALPHABET.length; j++) { + var lj = ALPHABET[j]; + + var MIDDLE = li + '(' + lj + ')'; + + var strOld = FRONT + MIDDLE + BACK, + strNew = FRONT + ' ' + MIDDLE + ' ' + BACK; + + minifiedCode = minifiedCode.replace(strOld, strNew); + } + } + + return minifiedCode; }; From e16d2f7813eb8defdf28d38295041a3018b918c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 27 Oct 2016 15:31:47 -0400 Subject: [PATCH 2/5] tasks: factor out _bundle wrapper in own tasks/util/ module - so that both bundle.js and cibundle.js can use it. --- tasks/bundle.js | 53 +----------------------- tasks/util/browserify_wrapper.js | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 tasks/util/browserify_wrapper.js diff --git a/tasks/bundle.js b/tasks/bundle.js index f86fa70335c..0fc7c7cd4d8 100644 --- a/tasks/bundle.js +++ b/tasks/bundle.js @@ -1,14 +1,6 @@ -var fs = require('fs'); -var path = require('path'); - -var browserify = require('browserify'); -var UglifyJS = require('uglify-js'); - var constants = require('./util/constants'); var common = require('./util/common'); -var compressAttributes = require('./util/compress_attributes'); -var patchMinified = require('./util/patch_minified'); -var doesFileExist = common.doesFileExist; +var _bundle = require('./util/browserify_wrapper'); /* * This script takes one argument @@ -26,6 +18,7 @@ var DEV = (arg === 'dev') || (arg === '--dev'); // Check if style and font build files are there +var doesFileExist = common.doesFileExist; if(!doesFileExist(constants.pathToCSSBuild) || !doesFileExist(constants.pathToFontSVG)) { throw new Error([ 'build/ is missing one or more files', @@ -59,45 +52,3 @@ constants.partialBundlePaths.forEach(function(pathObj) { pathToMinBundle: pathObj.distMin }); }); - -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); -} diff --git a/tasks/util/browserify_wrapper.js b/tasks/util/browserify_wrapper.js new file mode 100644 index 00000000000..0e13fa01874 --- /dev/null +++ b/tasks/util/browserify_wrapper.js @@ -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); +} From d4f216fe202fcce2b9a3f7f031f31cebf0c2be23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 27 Oct 2016 15:06:41 -0400 Subject: [PATCH 3/5] tasks: make cibundle output plotly.min.js - so that require.js test can be properly ran on each build! --- tasks/cibundle.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/tasks/cibundle.js b/tasks/cibundle.js index d9ec4f35790..cd5ff768a4c 100644 --- a/tasks/cibundle.js +++ b/tasks/cibundle.js @@ -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)); +}); From f958b4f5dc3abc9eb5bb811fa2ec7aa2b1dbd380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 27 Oct 2016 16:02:35 -0400 Subject: [PATCH 4/5] doc: improve comment in patch minified tasks util --- tasks/util/patch_minified.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tasks/util/patch_minified.js b/tasks/util/patch_minified.js index 0a230619c2d..d46b70f835a 100644 --- a/tasks/util/patch_minified.js +++ b/tasks/util/patch_minified.js @@ -8,7 +8,14 @@ var BACK = '+");'; * * 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) { From 1e98161b650933124f61762f3e6a4dc836f52af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 27 Oct 2016 16:32:02 -0400 Subject: [PATCH 5/5] tasks: use regex instead of loop over alphabet in patch minified step --- tasks/util/patch_minified.js | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/tasks/util/patch_minified.js b/tasks/util/patch_minified.js index d46b70f835a..e1388c71fa4 100644 --- a/tasks/util/patch_minified.js +++ b/tasks/util/patch_minified.js @@ -1,6 +1,5 @@ -var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'.split(''); -var FRONT = 'require("+'; -var BACK = '+");'; +var PATTERN = /require\("\+(\w)\((\w)\)\+"\)/; +var NEW_SUBSTR = 'require("+ $1($2) +")'; /* Uber hacky in-house fix to * @@ -19,20 +18,5 @@ var BACK = '+");'; * */ module.exports = function patchMinified(minifiedCode) { - for(var i = 0; i < ALPHABET.length; i++) { - var li = ALPHABET[i]; - - for(var j = 0; j < ALPHABET.length; j++) { - var lj = ALPHABET[j]; - - var MIDDLE = li + '(' + lj + ')'; - - var strOld = FRONT + MIDDLE + BACK, - strNew = FRONT + ' ' + MIDDLE + ' ' + BACK; - - minifiedCode = minifiedCode.replace(strOld, strNew); - } - } - - return minifiedCode; + return minifiedCode.replace(PATTERN, NEW_SUBSTR); };