From 3616db959cd7f2c5754e56b97b3bfbe612b1db25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Tue, 12 Jul 2016 11:34:42 -0400 Subject: [PATCH 01/12] add partial bundle index files --- lib/index-basic.js | 19 +++++++++++++++++++ lib/index-cartesian.js | 25 +++++++++++++++++++++++++ lib/index-geo.js | 18 ++++++++++++++++++ lib/index-gl2d.js | 20 ++++++++++++++++++++ lib/index-gl3d.js | 19 +++++++++++++++++++ lib/index-mapbox.js | 17 +++++++++++++++++ 6 files changed, 118 insertions(+) create mode 100644 lib/index-basic.js create mode 100644 lib/index-cartesian.js create mode 100644 lib/index-geo.js create mode 100644 lib/index-gl2d.js create mode 100644 lib/index-gl3d.js create mode 100644 lib/index-mapbox.js diff --git a/lib/index-basic.js b/lib/index-basic.js new file mode 100644 index 00000000000..8bec896cd11 --- /dev/null +++ b/lib/index-basic.js @@ -0,0 +1,19 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + + +var Core = require('./core'); + +Core.register([ + require('./bar'), + require('./pie') +]); + +module.exports = Core; diff --git a/lib/index-cartesian.js b/lib/index-cartesian.js new file mode 100644 index 00000000000..da5038539b0 --- /dev/null +++ b/lib/index-cartesian.js @@ -0,0 +1,25 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Core = require('./core'); + +Core.register([ + require('./bar'), + require('./box'), + require('./heatmap'), + require('./histogram'), + require('./histogram2d'), + require('./histogram2dcontour'), + require('./pie'), + require('./contour'), + require('./scatterternary') +]); + +module.exports = Core; diff --git a/lib/index-geo.js b/lib/index-geo.js new file mode 100644 index 00000000000..fc0e6830c03 --- /dev/null +++ b/lib/index-geo.js @@ -0,0 +1,18 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Core = require('./core'); + +Core.register([ + require('./scattergeo'), + require('./choropleth') +]); + +module.exports = Core; diff --git a/lib/index-gl2d.js b/lib/index-gl2d.js new file mode 100644 index 00000000000..5527c45dbe3 --- /dev/null +++ b/lib/index-gl2d.js @@ -0,0 +1,20 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Core = require('./core'); + +// Load all trace modules +Core.register([ + require('./scattergl'), + require('./heatmapgl'), + require('./contourgl') +]); + +module.exports = Core; diff --git a/lib/index-gl3d.js b/lib/index-gl3d.js new file mode 100644 index 00000000000..87ef88e0bdc --- /dev/null +++ b/lib/index-gl3d.js @@ -0,0 +1,19 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Core = require('./core'); + +Core.register([ + require('./scatter3d'), + require('./surface'), + require('./mesh3d') +]); + +module.exports = Core; diff --git a/lib/index-mapbox.js b/lib/index-mapbox.js new file mode 100644 index 00000000000..035acaec547 --- /dev/null +++ b/lib/index-mapbox.js @@ -0,0 +1,17 @@ +/** +* Copyright 2012-2016, Plotly, Inc. +* All rights reserved. +* +* This source code is licensed under the MIT license found in the +* LICENSE file in the root directory of this source tree. +*/ + +'use strict'; + +var Core = require('./core'); + +Core.register([ + require('./scattermapbox') +]); + +module.exports = Core; From 91bf6af03d50e8aef483b43bb7fe7bee340246e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Tue, 12 Jul 2016 11:35:10 -0400 Subject: [PATCH 02/12] adapt bundle & header tasks for partial bundles --- tasks/bundle.js | 79 ++++++++++++++++++++++++----------------- tasks/header.js | 7 ++++ tasks/util/constants.js | 5 +++ 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/tasks/bundle.js b/tasks/bundle.js index 45de5f03649..639e76db69e 100644 --- a/tasks/bundle.js +++ b/tasks/bundle.js @@ -1,4 +1,5 @@ var fs = require('fs'); +var path = require('path'); var browserify = require('browserify'); var UglifyJS = require('uglify-js'); @@ -35,41 +36,55 @@ catch(e) { // Browserify plotly.js -browserify(constants.pathToPlotlyIndex, { - debug: DEV, +_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, { standalone: 'Plotly', - transform: [compressAttributes] -}) -.bundle(function(err, buf) { - if(err) throw err; - - // generate plotly.min.js - if(!DEV) { - fs.writeFile( - constants.pathToPlotlyDistMin, - UglifyJS.minify(buf.toString(), constants.uglifyOptions).code - ); - } -}) -.pipe(fs.createWriteStream(constants.pathToPlotlyDist)); - + debug: DEV, + pathToMinBundle: constants.pathToPlotlyDistMin +}); // Browserify the geo assets -browserify(constants.pathToPlotlyGeoAssetsSrc, { +_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, { standalone: 'PlotlyGeoAssets' -}) -.bundle(function(err) { - if(err) throw err; -}) -.pipe(fs.createWriteStream(constants.pathToPlotlyGeoAssetsDist)); - +}); // Browserify the plotly.js with meta -browserify(constants.pathToPlotlyIndex, { - debug: DEV, - standalone: 'Plotly' -}) -.bundle(function(err) { - if(err) throw err; -}) -.pipe(fs.createWriteStream(constants.pathToPlotlyDistWithMeta)); +_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, { + standalone: 'Plotly', + debug: DEV +}); + +// Browserify the plotly.js partial bundles +constants.partialBundleNames.forEach(function(name) { + var pathToIndex = path.join(constants.pathToLib, 'index-' + name + '.js'), + pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'), + pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js'); + + _bundle(pathToIndex, pathToBundle, { + standalone: 'Plotly', + debug: DEV, + pathToMinBundle: pathToMinBundle + }); +}); + +function _bundle(pathToIndex, pathToBundle, opts) { + opts = opts || {}; + + // do we output a minified file? + var outputMinified = !!opts.pathToMinBundle && !opts.debug; + + var browserifyOpts = {}; + browserifyOpts.standalone = opts.standalone; + browserifyOpts.debug = opts.debug; + browserifyOpts.transform = outputMinified ? [compressAttributes] : []; + + browserify(pathToIndex, browserifyOpts).bundle(function(err, buf) { + if(err) throw err; + + if(outputMinified) { + var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code; + + fs.writeFile(opts.pathToMinBundle, minifiedCode); + } + }) + .pipe(fs.createWriteStream(pathToBundle)); +} diff --git a/tasks/header.js b/tasks/header.js index 9733d9b1bf2..2e63b2d2bbb 100644 --- a/tasks/header.js +++ b/tasks/header.js @@ -17,6 +17,13 @@ var pathsDist = [ constants.pathToPlotlyGeoAssetsDist ]; +constants.partialBundleNames.forEach(function(name) { + var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'), + pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js'); + + pathsDist.push(pathToBundle, pathToMinBundle); +}); + function headerLicense(path) { prependFile(path, constants.licenseDist + '\n', function(err) { if(err) throw err; diff --git a/tasks/util/constants.js b/tasks/util/constants.js index f4a190e6192..e14411bb10b 100644 --- a/tasks/util/constants.js +++ b/tasks/util/constants.js @@ -20,6 +20,7 @@ module.exports = { pathToSrc: pathToSrc, pathToLib: pathToLib, pathToBuild: pathToBuild, + pathToDist: pathToDist, pathToPlotlyIndex: path.join(pathToLib, 'index.js'), pathToPlotlyCore: path.join(pathToSrc, 'core.js'), @@ -28,6 +29,10 @@ module.exports = { pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'), pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'), + partialBundleNames: [ + 'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox' + ], + pathToTopojsonSrc: pathToTopojsonSrc, pathToTopojsonDist: path.join(pathToDist, 'topojson/'), pathToPlotlyGeoAssetsSrc: path.join(pathToSrc, 'assets/geo_assets.js'), From 23fa754047091ae794b05ad31d5f8d396b013c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Tue, 12 Jul 2016 12:26:05 -0400 Subject: [PATCH 03/12] add logger after bundling is done --- tasks/bundle.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tasks/bundle.js b/tasks/bundle.js index 639e76db69e..fac3e911bb1 100644 --- a/tasks/bundle.js +++ b/tasks/bundle.js @@ -70,21 +70,39 @@ function _bundle(pathToIndex, pathToBundle, opts) { opts = opts || {}; // do we output a minified file? - var outputMinified = !!opts.pathToMinBundle && !opts.debug; + var pathToMinBundle = opts.pathToMinBundle, + outputMinified = !!pathToMinBundle && !opts.debug; var browserifyOpts = {}; browserifyOpts.standalone = opts.standalone; browserifyOpts.debug = opts.debug; browserifyOpts.transform = outputMinified ? [compressAttributes] : []; - browserify(pathToIndex, browserifyOpts).bundle(function(err, buf) { + 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; - fs.writeFile(opts.pathToMinBundle, minifiedCode); + fs.writeFile(pathToMinBundle, minifiedCode, function(err) { + if(err) throw err; + + logger(pathToMinBundle); + }); } }) - .pipe(fs.createWriteStream(pathToBundle)); + .pipe(bundleWriteStream); +} + +function logger(pathToOutput) { + var log = 'ok ' + path.basename(pathToOutput); + + console.log(log); } From af27a0e4b5bb22371e3ed235f37ae3c608294e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Wed, 20 Jul 2016 16:20:24 -0400 Subject: [PATCH 04/12] lint: rename Core -> Plotly (consistent with README) --- lib/index-basic.js | 7 +++---- lib/index-cartesian.js | 6 +++--- lib/index-geo.js | 6 +++--- lib/index-gl3d.js | 6 +++--- lib/index-mapbox.js | 6 +++--- lib/index.js | 11 +++-------- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/index-basic.js b/lib/index-basic.js index 8bec896cd11..f56ffe8c6b6 100644 --- a/lib/index-basic.js +++ b/lib/index-basic.js @@ -8,12 +8,11 @@ 'use strict'; +var Plotly = require('./core'); -var Core = require('./core'); - -Core.register([ +Plotly.register([ require('./bar'), require('./pie') ]); -module.exports = Core; +module.exports = Plotly; diff --git a/lib/index-cartesian.js b/lib/index-cartesian.js index da5038539b0..1bd25000b40 100644 --- a/lib/index-cartesian.js +++ b/lib/index-cartesian.js @@ -8,9 +8,9 @@ 'use strict'; -var Core = require('./core'); +var Plotly = require('./core'); -Core.register([ +Plotly.register([ require('./bar'), require('./box'), require('./heatmap'), @@ -22,4 +22,4 @@ Core.register([ require('./scatterternary') ]); -module.exports = Core; +module.exports = Plotly; diff --git a/lib/index-geo.js b/lib/index-geo.js index fc0e6830c03..731e14789f5 100644 --- a/lib/index-geo.js +++ b/lib/index-geo.js @@ -8,11 +8,11 @@ 'use strict'; -var Core = require('./core'); +var Plotly = require('./core'); -Core.register([ +Plotly.register([ require('./scattergeo'), require('./choropleth') ]); -module.exports = Core; +module.exports = Plotly; diff --git a/lib/index-gl3d.js b/lib/index-gl3d.js index 87ef88e0bdc..c77a840b61a 100644 --- a/lib/index-gl3d.js +++ b/lib/index-gl3d.js @@ -8,12 +8,12 @@ 'use strict'; -var Core = require('./core'); +var Plotly = require('./core'); -Core.register([ +Plotly.register([ require('./scatter3d'), require('./surface'), require('./mesh3d') ]); -module.exports = Core; +module.exports = Plotly; diff --git a/lib/index-mapbox.js b/lib/index-mapbox.js index 035acaec547..ffe777f98b1 100644 --- a/lib/index-mapbox.js +++ b/lib/index-mapbox.js @@ -8,10 +8,10 @@ 'use strict'; -var Core = require('./core'); +var Plotly = require('./core'); -Core.register([ +Plotly.register([ require('./scattermapbox') ]); -module.exports = Core; +module.exports = Plotly; diff --git a/lib/index.js b/lib/index.js index 631fed56656..a870b3897c5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,14 +8,9 @@ 'use strict'; -/* - * This file is browserify'ed into a standalone 'Plotly' object. - */ +var Plotly = require('./core'); -var Core = require('./core'); - -// Load all trace modules -Core.register([ +Plotly.register([ require('./bar'), require('./box'), require('./heatmap'), @@ -33,4 +28,4 @@ Core.register([ require('./scatterternary') ]); -module.exports = Core; +module.exports = Plotly; From c4f92925a58228d77680c256c8b65c1195664e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:07:26 -0400 Subject: [PATCH 05/12] ignore all of dist/extras on npm --- .npmignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.npmignore b/.npmignore index ee62c8a2a7b..9115c4d428c 100644 --- a/.npmignore +++ b/.npmignore @@ -1,12 +1,11 @@ build/* - !build/plotcss.js !build/ploticon.js !build/README.md devtools test -dist/extras/mathjax +dist/extras circle.yml docker-compose.yml From dcb1229b038ab0468c39f4ea143593fb1730f08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:07:56 -0400 Subject: [PATCH 06/12] tasks: fix typo in bundle error msg --- tasks/bundle.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/bundle.js b/tasks/bundle.js index fac3e911bb1..456556f125d 100644 --- a/tasks/bundle.js +++ b/tasks/bundle.js @@ -29,7 +29,7 @@ try { } catch(e) { throw new Error([ - 'build/ is missing a or more files', + 'build/ is missing one or more files', 'Please run `npm run preprocess` first' ].join('\n')); } From 34460c3ba6037033fdc10b663e703151b614e73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:08:30 -0400 Subject: [PATCH 07/12] tasks: dry up paths to partial bundles --- tasks/bundle.js | 10 +++------- tasks/util/constants.js | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tasks/bundle.js b/tasks/bundle.js index 456556f125d..a3b8da2211d 100644 --- a/tasks/bundle.js +++ b/tasks/bundle.js @@ -54,15 +54,11 @@ _bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, { }); // Browserify the plotly.js partial bundles -constants.partialBundleNames.forEach(function(name) { - var pathToIndex = path.join(constants.pathToLib, 'index-' + name + '.js'), - pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'), - pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js'); - - _bundle(pathToIndex, pathToBundle, { +constants.partialBundlePaths.forEach(function(pathObj) { + _bundle(pathObj.index, pathObj.dist, { standalone: 'Plotly', debug: DEV, - pathToMinBundle: pathToMinBundle + pathToMinBundle: pathObj.distMin }); }); diff --git a/tasks/util/constants.js b/tasks/util/constants.js index e14411bb10b..d597e4e041e 100644 --- a/tasks/util/constants.js +++ b/tasks/util/constants.js @@ -1,5 +1,4 @@ var path = require('path'); - var pkg = require('../../package.json'); var pathToRoot = path.join(__dirname, '../../'); @@ -13,6 +12,19 @@ var pathToTopojsonSrc = path.join( path.dirname(require.resolve('sane-topojson')), 'dist/' ); +var partialBundleNames = [ + 'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox' +]; + +var partialBundlePaths = partialBundleNames.map(function(name) { + return { + name: name, + index: path.join(pathToLib, 'index-' + name + '.js'), + dist: path.join(pathToDist, 'plotly-' + name + '.js'), + distMin: path.join(pathToDist, 'plotly-' + name + '.min.js') + }; +}); + var year = (new Date()).getFullYear(); module.exports = { @@ -29,9 +41,8 @@ module.exports = { pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'), pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'), - partialBundleNames: [ - 'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox' - ], + partialBundleNames: partialBundleNames, + partialBundlePaths: partialBundlePaths, pathToTopojsonSrc: pathToTopojsonSrc, pathToTopojsonDist: path.join(pathToDist, 'topojson/'), From 9bead06affb07fbc3ac68d0a1d6f45e50a11c247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:09:22 -0400 Subject: [PATCH 08/12] tasks: wrap add-header-in-src-files sub-task in function --- tasks/header.js | 83 +++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/tasks/header.js b/tasks/header.js index 2e63b2d2bbb..20208eb824f 100644 --- a/tasks/header.js +++ b/tasks/header.js @@ -7,6 +7,7 @@ var glob = require('glob'); var constants = require('./util/constants'); +updateHeadersInSrcFiles(); // add headers to dist files @@ -30,61 +31,61 @@ function headerLicense(path) { }); } +// add or update header to src/ lib/ files +function updateHeadersInSrcFiles() { + var srcGlob = path.join(constants.pathToSrc, '**/*.js'); + var libGlob = path.join(constants.pathToLib, '**/*.js'); pathsDist.forEach(headerLicense); + // remove leading '/*' and trailing '*/' for comparison with falafel output + var licenseSrc = constants.licenseSrc; + var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2); -// add or update header to src files + glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) { + files.forEach(function(file) { + fs.readFile(file, 'utf-8', function(err, code) { -// remove leading '/*' and trailing '*/' for comparison with falafel output -var licenseSrc = constants.licenseSrc; -var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2); + // parse through code string while keeping track of comments + var comments = []; + falafel(code, {onComment: comments, locations: true}, function() {}); -var srcGlob = path.join(constants.pathToSrc, '**/*.js'); -var libGlob = path.join(constants.pathToLib, '**/*.js'); -glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) { - files.forEach(function(file) { - fs.readFile(file, 'utf-8', function(err, code) { + var header = comments[0]; - // parse through code string while keeping track of comments - var comments = []; - falafel(code, {onComment: comments, locations: true}, function() {}); + // error out if no header is found + if(!header || header.loc.start.line > 1) { + throw new Error(file + ' : has no header information.'); + } - var header = comments[0]; + // if header and license are the same, do nothing + if(isCorrect(header)) return; - // error out if no header is found - if(!header || header.loc.start.line > 1) { - throw new Error(file + ' : has no header information.'); - } + // if header and license only differ by date, update header + else if(hasWrongDate(header)) { + var codeLines = code.split('\n'); - // if header and license are the same, do nothing - if(isCorrect(header)) return; + codeLines.splice(header.loc.start.line - 1, header.loc.end.line); - // if header and license only differ by date, update header - else if(hasWrongDate(header)) { - var codeLines = code.split('\n'); + var newCode = licenseSrc + '\n' + codeLines.join('\n'); - codeLines.splice(header.loc.start.line - 1, header.loc.end.line); - - var newCode = licenseSrc + '\n' + codeLines.join('\n'); - - fs.writeFile(file, newCode, function(err) { - if(err) throw err; - }); - } - else { - // otherwise, throw an error - throw new Error(file + ' : has wrong header information.'); - } + fs.writeFile(file, newCode, function(err) { + if(err) throw err; + }); + } + else { + // otherwise, throw an error + throw new Error(file + ' : has wrong header information.'); + } + }); }); }); -}); -function isCorrect(header) { - return (header.value === licenseStr); -} + function isCorrect(header) { + return (header.value === licenseStr); + } -function hasWrongDate(header) { - var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g; + function hasWrongDate(header) { + var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g; - return (header.value.replace(regex, '') === licenseStr.replace(regex, '')); + return (header.value.replace(regex, '') === licenseStr.replace(regex, '')); + } } From f95f9aa682fb826aa257e588a17b3b3102f3f7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:09:48 -0400 Subject: [PATCH 09/12] tasks: add partial bundle name to dist headers --- tasks/header.js | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/tasks/header.js b/tasks/header.js index 20208eb824f..a877b3f26b2 100644 --- a/tasks/header.js +++ b/tasks/header.js @@ -7,27 +7,38 @@ var glob = require('glob'); var constants = require('./util/constants'); +// main +addHeadersInDistFiles(); updateHeadersInSrcFiles(); // add headers to dist files +function addHeadersInDistFiles() { + function _prepend(path, header) { + prependFile(path, header + '\n', function(err) { + if(err) throw err; + }); + } -var pathsDist = [ - constants.pathToPlotlyDistMin, - constants.pathToPlotlyDist, - constants.pathToPlotlyDistWithMeta, - constants.pathToPlotlyGeoAssetsDist -]; - -constants.partialBundleNames.forEach(function(name) { - var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'), - pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js'); + // add header to main dist bundles + var pathsDist = [ + constants.pathToPlotlyDistMin, + constants.pathToPlotlyDist, + constants.pathToPlotlyDistWithMeta, + constants.pathToPlotlyGeoAssetsDist + ]; + pathsDist.forEach(function(path) { + _prepend(path, constants.licenseDist); + }); - pathsDist.push(pathToBundle, pathToMinBundle); -}); + // add header and bundle name to partial bundle + constants.partialBundlePaths.forEach(function(pathObj) { + var headerDist = constants.licenseDist + .replace('plotly.js', 'plotly.js (' + pathObj.name + ')'); + _prepend(pathObj.dist, headerDist); -function headerLicense(path) { - prependFile(path, constants.licenseDist + '\n', function(err) { - if(err) throw err; + var headerDistMin = constants.licenseDist + .replace('plotly.js', 'plotly.js (' + pathObj.name + ' - minified)'); + _prepend(pathObj.distMin, headerDistMin); }); } @@ -35,7 +46,6 @@ function headerLicense(path) { function updateHeadersInSrcFiles() { var srcGlob = path.join(constants.pathToSrc, '**/*.js'); var libGlob = path.join(constants.pathToLib, '**/*.js'); -pathsDist.forEach(headerLicense); // remove leading '/*' and trailing '*/' for comparison with falafel output var licenseSrc = constants.licenseSrc; From 5bc1167ed23be06b9b5d2f9cc3de184c3a4dd8fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:14:07 -0400 Subject: [PATCH 10/12] tasks: add stats script - which generated the dist/README.md on `npm run build` - bundle specific info such as the included (trace) module and size (including gzipped size) are generated on every build --- package.json | 4 +- tasks/stats.js | 206 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 tasks/stats.js diff --git a/package.json b/package.json index 9f8b9bf10b2..172a138727b 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "preprocess": "node tasks/preprocess.js", "bundle": "node tasks/bundle.js", "header": "node tasks/header.js", - "build": "npm run preprocess && npm run bundle && npm run header", + "stats": "node tasks/stats.js", + "build": "npm run preprocess && npm run bundle && npm run header && npm run stats", "cibuild": "npm run preprocess && node tasks/cibundle.js", "watch": "node tasks/watch_plotly.js", "lint": "eslint . || true", @@ -95,6 +96,7 @@ "fs-extra": "^0.30.0", "fuse.js": "^2.2.0", "glob": "^7.0.0", + "gzip-size": "^3.0.0", "jasmine-core": "^2.4.1", "karma": "^1.1.0", "karma-browserify": "^5.0.1", diff --git a/tasks/stats.js b/tasks/stats.js new file mode 100644 index 00000000000..71470a38074 --- /dev/null +++ b/tasks/stats.js @@ -0,0 +1,206 @@ +var path = require('path'); +var fs = require('fs'); + +var falafel = require('falafel'); +var gzipSize = require('gzip-size'); +var prettySize = require('prettysize'); + +var constants = require('./util/constants'); +var pkg = require('../package.json'); + +var pathDistREADME = path.join(constants.pathToDist, 'README.md'); +var cdnRoot = 'https://cdn.plot.ly/plotly-'; +var coreModules = ['scatter']; + +var ENC = 'utf-8'; +var JS = '.js'; +var MINJS = '.min.js'; + + +// general info about distributed files +var infoContent = [ + '# Using distributed files', + '', + 'All plotly.js dist bundles inject an object `Plotly` into the global scope.', + '', + 'Import plotly.js as:', + '', + '```html', + '', + '```', + '', + 'or the un-minified version as:', + '', + '```html', + '', + '```', + '', + 'To support IE9, put:', + '', + '```html', + '', + '```', + '', + 'before the plotly.js script tag.', + '', + 'To add MathJax, put', + '', + '```html', + '', + '```', + '', + 'before the plotly.js script tag. You can grab the relevant MathJax files in `./dist/extras/mathjax/`.', + '' +]; + +// add bundle content/size info +var mainSizes = findSizes({ + dist: constants.pathToPlotlyDist, + distMin: constants.pathToPlotlyDistMin, + withMeta: constants.pathToPlotlyDistWithMeta +}); +var bundleInfo = [ + '# Bundle information', + '', + 'The main plotly.js bundle includes all the official (non-beta) trace modules.', + '', + 'It be can imported as minified javascript', + '- using dist file `dist/plotly.min.js`', + '- using CDN URL ' + cdnRoot + 'plotly-latest.min.js OR ' + cdnRoot + 'plotly-' + pkg.version + MINJS, + '', + 'or as raw javascript:', + '- using dist file `dist/plotly.js`', + '- using CDN URL ' + cdnRoot + 'plotly-latest.js OR ' + cdnRoot + 'plotly-' + pkg.version + JS, + '- using CommonJS with `require(\'plotly.js\')`', + '', + 'If you would like to have access to the attribute meta information ' + + '(including attribute descriptions as on the [schema reference page](https://plot.ly/javascript/reference/)), ' + + 'use dist file `dist/plotly-with-meta.js`', + '', + 'The main plotly.js bundle weights in at:', + '', + '| plotly.js | plotly.min.js | plotly.min.js + gzip | plotly-with-meta.js |', + '|-----------|---------------|----------------------|---------------------|', + '| ' + mainSizes.raw + ' | ' + mainSizes.minified + ' | ' + mainSizes.gzipped + ' | ' + mainSizes.withMeta + ' |', + '', + '## Partial bundles', + '', + 'Starting in `v1.15.0`, plotly.js also ships with several _partial_ bundles:', + '', + constants.partialBundlePaths.map(makeBundleHeaderInfo).join('\n'), + '' +] +.concat( + constants.partialBundlePaths.map(makeBundleInfo) +); + +// footer info +var footer = [ + '----------------', + '', + '_This file is auto-generated by `npm run stats`. ' + + 'Please do not edit this file directly._' +]; + +var content = infoContent.concat(bundleInfo).concat(footer); + +fs.writeFile(pathDistREADME, content.join('\n'), function(err) { + if(err) throw err; +}); + +function makeBundleHeaderInfo(pathObj) { + var name = pathObj.name; + return '- [' + name + '](#plotly.js-' + name + ')'; +} + +function makeBundleInfo(pathObj) { + var name = pathObj.name; + var sizes = findSizes(pathObj); + var moduleList = coreModules.concat(scrapeContent(pathObj)); + + return [ + '### plotly.js ' + name, + '', + formatBundleInfo(name, moduleList), + '', + '| Way to import | Location |', + '|---------------|----------|', + '| dist bundle | ' + '`dist/plotly-' + name + JS + '` |', + '| dist bundle (minified) | ' + '`dist/plotly-' + name + MINJS + '` |', + '| CDN URL (latest) | ' + cdnRoot + name + '-latest' + JS + ' |', + '| CDN URL (latest minified) | ' + cdnRoot + name + '-latest' + MINJS + ' |', + '| CDN URL (tagged) | ' + cdnRoot + name + '-' + pkg.version + JS + ' |', + '| CDN URL (tagged minified) | ' + cdnRoot + name + '-' + pkg.version + MINJS + ' |', + '| CommonJS | ' + '`require(\'plotly.js/lib/' + 'index-' + name + '\')`' + ' |', + '', + '| Raw size | Minified size | Minified + gzip size |', + '|------|-----------------|------------------------|', + '| ' + sizes.raw + ' | ' + sizes.minified + ' | ' + sizes.gzipped + ' | ', + '' + ].join('\n'); +} + +function findSizes(pathObj) { + var codeDist = fs.readFileSync(pathObj.dist, ENC), + codeDistMin = fs.readFileSync(pathObj.distMin, ENC); + + var sizes = { + raw: prettySize(codeDist.length), + minified: prettySize(codeDistMin.length), + gzipped: prettySize(gzipSize.sync(codeDistMin)) + }; + + if(pathObj.withMeta) { + var codeWithMeta = fs.readFileSync(pathObj.withMeta, ENC); + sizes.withMeta = prettySize(codeWithMeta.length); + } + + return sizes; +} + +function scrapeContent(pathObj) { + var code = fs.readFileSync(pathObj.index, ENC); + var moduleList = []; + + falafel(code, function(node) { + if(isModuleNode(node)) { + var moduleName = node.value.replace('./', ''); + moduleList.push(moduleName); + } + }); + + return moduleList; +} + +function isModuleNode(node) { + return ( + node.type === 'Literal' && + node.parent && + node.parent.type === 'CallExpression' && + node.parent.callee && + node.parent.callee.type === 'Identifier' && + node.parent.callee.name === 'require' && + node.parent.parent && + node.parent.parent.type === 'ArrayExpression' + ); +} + +function formatBundleInfo(bundleName, moduleList) { + var enumeration = moduleList.map(function(moduleName, i) { + var len = moduleList.length, + ending; + + if(i === len - 2) ending = 'and'; + else if(i < len - 1) ending = ','; + else ending = ''; + + return '`' + moduleName + '`' + ending; + }); + + return [ + 'The', '`' + bundleName + '`', + 'partial bundle contains the', + enumeration.join(' '), + 'trace modules.' + ].join(' '); +} From 7b0a0174434366f1abb5b6e8b37968b436fc92af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 11:16:50 -0400 Subject: [PATCH 11/12] tasks: fix URL to partial bundle headers --- tasks/stats.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/stats.js b/tasks/stats.js index 71470a38074..ac90e6b3de6 100644 --- a/tasks/stats.js +++ b/tasks/stats.js @@ -110,7 +110,7 @@ fs.writeFile(pathDistREADME, content.join('\n'), function(err) { function makeBundleHeaderInfo(pathObj) { var name = pathObj.name; - return '- [' + name + '](#plotly.js-' + name + ')'; + return '- [' + name + '](#plotlyjs-' + name + ')'; } function makeBundleInfo(pathObj) { From 7a4378184201115f72c252d95bd40938ee37e5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Thu, 21 Jul 2016 12:43:50 -0400 Subject: [PATCH 12/12] lint: sub Core -> Plotly in gl2d index --- lib/index-gl2d.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/index-gl2d.js b/lib/index-gl2d.js index 5527c45dbe3..b5e0df006a4 100644 --- a/lib/index-gl2d.js +++ b/lib/index-gl2d.js @@ -8,13 +8,12 @@ 'use strict'; -var Core = require('./core'); +var Plotly = require('./core'); -// Load all trace modules -Core.register([ +Plotly.register([ require('./scattergl'), require('./heatmapgl'), require('./contourgl') ]); -module.exports = Core; +module.exports = Plotly;