-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstats.js
221 lines (195 loc) · 6.91 KB
/
stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var path = require('path');
var fs = require('fs');
var falafel = require('falafel');
var gzipSize = require('gzip-size');
var prettySize = require('prettysize');
var common = require('./util/common');
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';
// main
var content = getContent();
common.writeFile(pathDistREADME, content.join('\n'));
function getContent() {
return []
.concat(getInfoContent())
.concat(getMainBundleInfo())
.concat(getPartialBundleInfo())
.concat(getFooter());
}
// general info about distributed files
function getInfoContent() {
return [
'# 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/`.',
''
];
}
// info about main bundle
function getMainBundleInfo() {
var mainSizes = findSizes({
dist: constants.pathToPlotlyDist,
distMin: constants.pathToPlotlyDistMin,
withMeta: constants.pathToPlotlyDistWithMeta
});
return [
'# 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'),
''
];
}
// info about partial bundles
function getPartialBundleInfo() {
return constants.partialBundlePaths.map(makeBundleInfo);
}
// footer info
function getFooter() {
return [
'----------------',
'',
'_This file is auto-generated by `npm run stats`. ' +
'Please do not edit this file directly._'
];
}
function makeBundleHeaderInfo(pathObj) {
var name = pathObj.name;
return '- [' + name + '](#plotlyjs-' + 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(' ');
}