-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Introducing partial dist bundles #740
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
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3616db9
add partial bundle index files
etpinard 91bf6af
adapt bundle & header tasks for partial bundles
etpinard 23fa754
add logger after bundling is done
etpinard af27a0e
lint: rename Core -> Plotly (consistent with README)
etpinard c4f9292
ignore all of dist/extras on npm
etpinard dcb1229
tasks: fix typo in bundle error msg
etpinard 34460c3
tasks: dry up paths to partial bundles
etpinard 9bead06
tasks: wrap add-header-in-src-files sub-task in function
etpinard f95f9aa
tasks: add partial bundle name to dist headers
etpinard 5bc1167
tasks: add stats script
etpinard 7b0a017
tasks: fix URL to partial bundle headers
etpinard 7a43781
lint: sub Core -> Plotly in gl2d index
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 |
---|---|---|
@@ -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', | ||
'<script type="text/javascript" src="plotly.min.js"></script>', | ||
'```', | ||
'', | ||
'or the un-minified version as:', | ||
'', | ||
'```html', | ||
'<script type="text/javascript" src="plotly.js" charset="utf-8"></script>', | ||
'```', | ||
'', | ||
'To support IE9, put:', | ||
'', | ||
'```html', | ||
'<script>if(typeof window.Int16Array !== \'function\')document.write("<scri"+"pt src=\'extras/typedarray.min.js\'></scr"+"ipt>");</script>', | ||
'```', | ||
'', | ||
'before the plotly.js script tag.', | ||
'', | ||
'To add MathJax, put', | ||
'', | ||
'```html', | ||
'<script type="text/javascript" src="mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>', | ||
'```', | ||
'', | ||
'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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it. 🍻 |
||
}; | ||
|
||
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(' '); | ||
} |
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.
see preview -> https://gist.github.com/etpinard/05c37e9bc8f1f3078eb88f2b016153b4
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.
As a fail-(slightly-)safe(r), can you include an html comment at the top of the markdown: