diff --git a/tasks/util/browserify_wrapper.js b/tasks/util/browserify_wrapper.js index b0364a6682b..2b12ce28767 100644 --- a/tasks/util/browserify_wrapper.js +++ b/tasks/util/browserify_wrapper.js @@ -4,6 +4,7 @@ var path = require('path'); var browserify = require('browserify'); var minify = require('minify-stream'); var derequire = require('derequire'); +var substr2slice = require('./substr2slice'); var through = require('through2'); var strictD3 = require('./strict_d3'); @@ -78,7 +79,7 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) { }; bundleStream - .pipe(applyDerequire()) + .pipe(modify()) .pipe(minify(minifyOpts)) .pipe(fs.createWriteStream(pathToMinBundle)) .on('finish', function() { @@ -89,7 +90,7 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) { if(pathToBundle) { bundleStream - .pipe(applyDerequire()) + .pipe(modify()) .pipe(fs.createWriteStream(pathToBundle)) .on('finish', function() { logger(pathToBundle); @@ -103,13 +104,19 @@ function logger(pathToOutput) { console.log(log); } -function applyDerequire() { - var buf = ''; +function modify() { + var str = ''; return through(function(chunk, enc, next) { - buf += chunk.toString(); + str += chunk.toString(); next(); }, function(done) { - this.push(derequire(buf)); + this.push( + derequire( // require >> _dereq_ + substr2slice( // substr >> slice + str + ) + ) + ); done(); }); } diff --git a/tasks/util/substr2slice.js b/tasks/util/substr2slice.js new file mode 100644 index 00000000000..2ac033d0060 --- /dev/null +++ b/tasks/util/substr2slice.js @@ -0,0 +1,65 @@ +'use strict'; + +// Replace .substr(a, ?b) with .slice(a, (a)+b) +// +// String.prototype.substr() is deprecated! +// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr + +module.exports = function substr2slice(str) { + var i0 = 0; + while(i0 !== -1) { + // assuming there is no white space after substr + i0 = str.indexOf('.substr(', i0); + if(i0 === -1) return str; + + var args = []; + var text = ''; + var k = 0; + + // step into the function + var i = i0 + 7; + var p = 1; // open parentheses + while(p > 0) { + i++; + + var c = str.charAt(i); + if(!c) break; + if(p === 1 && ( + c === ',' || + c === ')' + )) { + args[k++] = text; + text = ''; + } else { + text += c; + } + + if(c === '(') p++; + if(c === ')') p--; + } + + // console.log(str.slice(i0, i + 1)); + // console.log(args); + + var startStr = args[0]; + var lengthStr = args[1]; + var out = '.slice(' + startStr; + + if(lengthStr !== undefined) { + out += ','; + if(+startStr !== 0) { + out += '(' + startStr + ') + '; + } + out += lengthStr; + } + + out += ')'; + + // console.log(out) + // console.log('__'); + + str = str.slice(0, i0) + out + str.slice(i + 1); + } + + return str; +}; diff --git a/tasks/util/watchified_bundle.js b/tasks/util/watchified_bundle.js index d9652bf2ce9..43dc2fa20ab 100644 --- a/tasks/util/watchified_bundle.js +++ b/tasks/util/watchified_bundle.js @@ -3,7 +3,9 @@ var fs = require('fs'); var browserify = require('browserify'); var watchify = require('watchify'); var prettySize = require('prettysize'); +var through = require('through2'); +var substr2slice = require('../../tasks/util/substr2slice'); var constants = require('./constants'); var common = require('./common'); @@ -50,6 +52,7 @@ module.exports = function makeWatchifiedBundle(strict, onFirstBundleCallback) { firstBundle = false; } }) + .pipe(modify()) .pipe( fs.createWriteStream(constants.pathToPlotlyBuild) ); @@ -81,3 +84,18 @@ function formatBundleMsg(b, bundleName) { console.log(msgParts.join(' ')); }); } + +function modify() { + var str = ''; + return through(function(chunk, enc, next) { + str += chunk.toString(); + next(); + }, function(done) { + this.push( + substr2slice( // substr >> slice + str + ) + ); + done(); + }); +}