diff --git a/lib/index-basic.js b/lib/index-basic.js index 670ce848e90..590f545eec0 100644 --- a/lib/index-basic.js +++ b/lib/index-basic.js @@ -14,7 +14,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-cartesian.js b/lib/index-cartesian.js index 085283666eb..db9855a26b6 100644 --- a/lib/index-cartesian.js +++ b/lib/index-cartesian.js @@ -23,7 +23,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-finance.js b/lib/index-finance.js index d88c44bb8fc..695fdf31cdc 100644 --- a/lib/index-finance.js +++ b/lib/index-finance.js @@ -21,7 +21,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-geo.js b/lib/index-geo.js index 35302c4e32a..3dff88a6fd2 100644 --- a/lib/index-geo.js +++ b/lib/index-geo.js @@ -14,7 +14,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-gl2d.js b/lib/index-gl2d.js index e71369e7019..1fd5d28dd5f 100644 --- a/lib/index-gl2d.js +++ b/lib/index-gl2d.js @@ -17,7 +17,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-gl3d.js b/lib/index-gl3d.js index 37b556c1b88..a965c91d2d4 100644 --- a/lib/index-gl3d.js +++ b/lib/index-gl3d.js @@ -19,7 +19,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-mapbox.js b/lib/index-mapbox.js index 8d2d99344be..ddb69b93430 100644 --- a/lib/index-mapbox.js +++ b/lib/index-mapbox.js @@ -15,7 +15,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index-strict.js b/lib/index-strict.js index 93f145adfd1..aa541fd970c 100644 --- a/lib/index-strict.js +++ b/lib/index-strict.js @@ -48,7 +48,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/lib/index.js b/lib/index.js index 97a15b89cbb..a447211d9ab 100644 --- a/lib/index.js +++ b/lib/index.js @@ -57,7 +57,7 @@ Plotly.register([ require('./sort'), // components - require('./calendars') + require('./calendars'), ]); module.exports = Plotly; diff --git a/tasks/extra_bundles.js b/tasks/extra_bundles.js index 958d185a62c..eb0967a00e8 100644 --- a/tasks/extra_bundles.js +++ b/tasks/extra_bundles.js @@ -15,7 +15,9 @@ for(var i = 0; i < partialBundlePaths.length; i++) { index: opts.index, dist: opts.dist, distMin: opts.distMin, - traceList: opts.traceList + traceList: opts.traceList, + transformList: opts.transformList, + calendars: opts.calendars }); } diff --git a/tasks/partial_bundle.js b/tasks/partial_bundle.js index 42859b4578a..3208ab35d78 100644 --- a/tasks/partial_bundle.js +++ b/tasks/partial_bundle.js @@ -1,4 +1,5 @@ var path = require('path'); +var minimist = require('minimist'); var runSeries = require('run-series'); var prependFile = require('prepend-file'); @@ -7,41 +8,78 @@ var common = require('./util/common'); var _bundle = require('./util/browserify_wrapper'); var header = constants.licenseDist + '\n'; +var allTransforms = constants.allTransforms; var allTraces = constants.allTraces; var mainIndex = constants.mainIndex; -var argv = process.argv; - -if(argv.length > 2) { - // command line - - var traceList = ['scatter']; // added by default - var name; - for(var i = 2; i < argv.length; i++) { - var a = argv[i]; - +function createList(outList, inList, allList, type) { + for(var i = 0; i < inList.length; i++) { + var t = inList[i]; if( - allTraces.indexOf(a) !== -1 && // requested - traceList.indexOf(a) === -1 // not added before + outList.indexOf(t) === -1 // not added before ) { - traceList.push(a); + if(allList.indexOf(t) === -1) { + console.error(t, 'is not a valid ' + type + '!', 'Valid ' + type + 's are:', allList); + } else { + outList.push(t); + } } - if(a.indexOf('--name=') === 0) name = a.replace('--name=', ''); } - if(!name) name = 'custom'; - traceList = traceList.sort(); + + return outList.sort(); +} + +function isFalse(a) { + return ( + a === 'none' || + a === 'false' + ); +} + +function inputBoolean(a, dflt) { + return !a ? dflt : !isFalse(a); +} + +function inputArray(a, dflt) { + dflt = dflt.slice(); + + return ( + isFalse(a) ? [] : + !a || a === 'all' ? dflt : + a.split(',') + ); +} + +if(process.argv.length > 2) { + // command line + + var args = minimist(process.argv.slice(2), {}); + + // parse arguments + var unminified = inputBoolean(args.unminified, false); + var out = args.out ? args.out : 'custom'; + var traces = inputArray(args.traces, allTraces); + var transforms = inputArray(args.transforms, allTransforms); var opts = { - traceList: traceList, - name: name, + traceList: createList(['scatter'], traces, allTraces, 'trace'), + transformList: createList([], transforms, allTransforms, 'transform'), - index: path.join(constants.pathToBuild, 'index-' + name + '.js'), - dist: path.join(constants.pathToDist, 'plotly-' + name + '.js'), - distMin: path.join(constants.pathToDist, 'plotly-' + name + '.min.js') + name: out, + index: path.join(constants.pathToLib, 'index-' + out + '.js') }; + if(unminified) { + opts.dist = path.join(constants.pathToDist, 'plotly-' + out + '.js'); + } else { + opts.distMin = path.join(constants.pathToDist, 'plotly-' + out + '.min.js'); + } + console.log(opts); + opts.calendars = true; + opts.deleteIndex = true; + var tasks = []; partialBundle(tasks, opts); @@ -55,43 +93,55 @@ if(argv.length > 2) { function partialBundle(tasks, opts) { var name = opts.name; var index = opts.index; + var deleteIndex = opts.deleteIndex; var dist = opts.dist; var distMin = opts.distMin; var traceList = opts.traceList; + var transformList = opts.transformList; + var calendars = opts.calendars; tasks.push(function(done) { var partialIndex = mainIndex; - allTraces.forEach(function(trace) { - if(traceList.indexOf(trace) === -1) { - var WHITESPACE_BEFORE = '\\s*'; - // remove require - var newCode = partialIndex.replace( - new RegExp( - WHITESPACE_BEFORE + - 'require\\(\'\\./' + trace + '\'\\),', - 'g'), '' - ); - - // test removal - if(newCode === partialIndex) throw 'Unable to find and drop require for trace: "' + trace + '"'; - - partialIndex = newCode; + + var all = ['calendars'].concat(allTransforms).concat(allTraces); + var includes = (calendars ? ['calendars'] : []).concat(transformList).concat(traceList); + var excludes = all.filter(function(e) { return includes.indexOf(e) === -1; }); + + excludes.forEach(function(t) { + var WHITESPACE_BEFORE = '\\s*'; + // remove require + var newCode = partialIndex.replace( + new RegExp( + WHITESPACE_BEFORE + + 'require\\(\'\\./' + t + '\'\\),', + 'g'), '' + ); + + // test removal + if(newCode === partialIndex) { + console.error('Unable to find and drop require for ' + t); + throw 'Error generating index for partial bundle!'; } + + partialIndex = newCode; }); common.writeFile(index, partialIndex, done); }); tasks.push(function(done) { - _bundle(index, dist, { + var bundleOpts = { standalone: 'Plotly', + deleteIndex: deleteIndex, pathToMinBundle: distMin - }, function() { + }; + + _bundle(index, dist, bundleOpts, function() { var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')'); var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)'); - prependFile(dist, headerDist, common.throwOnError); - prependFile(distMin, headerDistMin, common.throwOnError); + if(dist) prependFile(dist, headerDist, common.throwOnError); + if(distMin) prependFile(distMin, headerDistMin, common.throwOnError); done(); }); diff --git a/tasks/util/browserify_wrapper.js b/tasks/util/browserify_wrapper.js index 7a4e49113b4..b0364a6682b 100644 --- a/tasks/util/browserify_wrapper.js +++ b/tasks/util/browserify_wrapper.js @@ -6,7 +6,6 @@ var minify = require('minify-stream'); var derequire = require('derequire'); var through = require('through2'); -var constants = require('./constants'); var strictD3 = require('./strict_d3'); /** Convenience browserify wrapper @@ -22,9 +21,8 @@ var strictD3 = require('./strict_d3'); * - noCompress {boolean} skip attribute meta compression? * @param {function} cb callback * - * 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. + * Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted. + * Otherwise outputs two file: one un-minified bundle and one minified bundle. * * Logs basename of bundle when completed. */ @@ -47,10 +45,17 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) { } var b = browserify(pathToIndex, browserifyOpts); - var pending = pathToMinBundle ? 2 : 1; + var pending = (pathToMinBundle && pathToBundle) ? 2 : 1; function done() { - if(cb && --pending === 0) cb(null); + if(cb && --pending === 0) { + if(opts.deleteIndex) { + console.log('delete', pathToIndex); + fs.unlinkSync(pathToIndex, {}); + } + + cb(null); + } } var bundleStream = b.bundle(function(err) { @@ -61,9 +66,20 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) { }); if(pathToMinBundle) { + var minifyOpts = { + ecma: 5, + mangle: true, + output: { + beautify: false, + ascii_only: true + }, + + sourceMap: false + }; + bundleStream .pipe(applyDerequire()) - .pipe(minify(constants.uglifyOptions)) + .pipe(minify(minifyOpts)) .pipe(fs.createWriteStream(pathToMinBundle)) .on('finish', function() { logger(pathToMinBundle); @@ -71,13 +87,15 @@ module.exports = function _bundle(pathToIndex, pathToBundle, opts, cb) { }); } - bundleStream - .pipe(applyDerequire()) - .pipe(fs.createWriteStream(pathToBundle)) - .on('finish', function() { - logger(pathToBundle); - done(); - }); + if(pathToBundle) { + bundleStream + .pipe(applyDerequire()) + .pipe(fs.createWriteStream(pathToBundle)) + .on('finish', function() { + logger(pathToBundle); + done(); + }); + } }; function logger(pathToOutput) { diff --git a/tasks/util/constants.js b/tasks/util/constants.js index 920ab8c0e33..08a808b4a06 100644 --- a/tasks/util/constants.js +++ b/tasks/util/constants.js @@ -10,10 +10,19 @@ var pathToStrictD3Module = path.join(pathToRoot, 'test/strict-d3.js'); var pathToDist = path.join(pathToRoot, 'dist/'); var pathToBuild = path.join(pathToRoot, 'build/'); +function startsWithLowerCase(v) { + return v.charAt(0) !== v.charAt(0).toUpperCase(); +} + var pathToPlotlyIndex = path.join(pathToLib, 'index.js'); var mainIndex = fs.readFileSync(pathToPlotlyIndex, 'utf-8'); -var pathToPlotlyTraces = path.join(pathToSrc, 'traces'); -var allTraces = fs.readdirSync(pathToPlotlyTraces); +var allTraces = fs.readdirSync(path.join(pathToSrc, 'traces')) + .filter(startsWithLowerCase); +var allTransforms = fs.readdirSync(path.join(pathToSrc, 'transforms')) + .filter(function(v) { + return startsWithLowerCase(v) && v !== 'helpers.js'; + }) + .map(function(e) { return e.replace('.js', ''); }); var pathToTopojsonSrc; try { @@ -140,6 +149,8 @@ function makePartialBundleOpts(name) { return { name: name, traceList: partialBundleTraces[name], + transformList: allTransforms, + calendars: true, index: path.join(pathToLib, 'index-' + name + '.js'), dist: path.join(pathToDist, 'plotly-' + name + '.js'), distMin: path.join(pathToDist, 'plotly-' + name + '.min.js') @@ -157,10 +168,10 @@ module.exports = { pathToBuild: pathToBuild, pathToDist: pathToDist, + allTransforms: allTransforms, allTraces: allTraces, mainIndex: mainIndex, pathToPlotlyIndex: pathToPlotlyIndex, - pathToPlotlyTraces: pathToPlotlyTraces, pathToPlotlyCore: path.join(pathToSrc, 'core.js'), pathToPlotlyVersion: path.join(pathToSrc, 'version.js'), pathToPlotlyBuild: path.join(pathToBuild, 'plotly.js'), @@ -208,17 +219,6 @@ module.exports = { testContainerUrl: 'http://localhost:9010/', testContainerHome: '/var/www/streambed/image_server/plotly.js', - uglifyOptions: { - ecma: 5, - mangle: true, - output: { - beautify: false, - ascii_only: true - }, - - sourceMap: false - }, - licenseDist: [ '/**', '* plotly.js v' + pkg.version, diff --git a/tasks/util/wrap_locale.js b/tasks/util/wrap_locale.js index e2a106cb044..2f5440edd77 100644 --- a/tasks/util/wrap_locale.js +++ b/tasks/util/wrap_locale.js @@ -4,8 +4,6 @@ var path = require('path'); var minify = require('minify-stream'); var intoStream = require('into-stream'); -var constants = require('./constants'); - var prefix = 'var locale='; var suffix = ';if(typeof Plotly === \'undefined\') {window.PlotlyLocales = window.PlotlyLocales || []; window.PlotlyLocales.push(locale);} else {Plotly.register(locale);}'; @@ -26,7 +24,16 @@ module.exports = function wrapLocale(pathToInput, pathToOutput) { var rawOut = prefix + data.substr(moduleStart, moduleEnd - moduleStart) + suffix; intoStream(rawOut) - .pipe(minify(constants.uglifyOptions)) + .pipe(minify({ + ecma: 5, + mangle: true, + output: { + beautify: false, + ascii_only: true + }, + + sourceMap: false + })) .pipe(fs.createWriteStream(pathToOutput)) .on('finish', function() { logger(pathToOutput);