-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstats.js
253 lines (227 loc) · 8.67 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
common.writeFile(pathDistREADME, getReadMeContent());
function getReadMeContent() {
return []
.concat(getInfoContent())
.concat(getMainBundleInfo())
.concat(getPartialBundleInfo())
.concat(getFooter())
.join('\n');
}
// 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 src="plotly.min.js"></script>',
'```',
'',
'or the un-minified version as:',
'',
'```html',
'<script src="plotly.js" charset="utf-8"></script>',
'```',
'',
'### To support IE9',
'',
'*Before* the plotly.js script tag, add:',
'',
'```html',
'<script>if(typeof window.Int16Array !== \'function\')document.write("<scri"+"pt src=\'extras/typedarray.min.js\'></scr"+"ipt>");</script>',
'<script>document.write("<scri"+"pt src=\'extras/request_animation_frame.js\'></scr"+"ipt>");</script>',
'```',
'',
'### To support MathJax',
'',
'*Before* the plotly.js script tag, add:',
'',
'```html',
'<script src="mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>',
'```',
'',
'You can grab the relevant MathJax files in `./dist/extras/mathjax/`.',
'',
'### To include localization',
'',
'Plotly.js defaults to US English (en-US) and includes British English (en) in the standard bundle.',
'This example uses Swiss-German (de-CH), but many other localizations are available (see the contents of this directory for the full list). All available locales have working date localizations, but only a subset will localize on-graph text (e.g., modebar controls). See [here](https://github.com/plotly/plotly.js/pulls?q=is%3Apr+label%3A%22type%3A+translation%22+is%3Aclosed) for a list of "fully-supported" locales.',
'',
'#### Loading and registering locales',
'',
'Some locales build on other locales (e.g., "de-CH" builds on "de"). In this case, first load the base locale (e.g., "de"), then load the actual locale (e.g., "de-CH"):',
'',
'```html',
'<script src="plotly-locale-de.js"></script>',
'<script src="plotly-locale-de-ch.js"></script>',
'```',
'',
'Locale bundles are also available on our CDN (e.g., ' + cdnRoot + 'locale-de-ch-latest.js OR ' + cdnRoot + 'locale-de-ch-' + pkg.version + '.js). Note that the file names here are all lowercase, but the region is uppercase when you apply a locale.',
'',
'#### Apply a locale',
'',
'After registering a locale, you can it as the default for all Plotly plots:',
'',
'```html',
'<script>Plotly.setPlotConfig({locale: \'de-CH\'})</script>',
'```',
'',
'Or, apply the (registered) locale to particular plot(s) as a `config` parameter:',
'',
'```js',
'Plotly.newPlot(graphDiv, data, layout, {locale: \'de-CH\'})',
'```'
];
}
// 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 + 'latest' + MINJS + ' OR ' + cdnRoot + pkg.version + MINJS,
'',
'or as raw javascript:',
'- using dist file `dist/plotly.js`',
'- using CDN URL ' + cdnRoot + 'latest' + JS + ' OR ' + cdnRoot + 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(' ');
}