-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-globalize.js
204 lines (159 loc) · 4.93 KB
/
generate-globalize.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
const fs = require('fs');
const { join } = require('path');
const brotli = require('iltorb');
const zlib = require('zlib');
const compiler = require('globalize-compiler');
const { decimal } = require('./framework');
const { TIMEZONES } = require('./timezones');
const ALL_LOCALES = fs.readdirSync('./node_modules/cldr-data/main');
const LANGS = ['en', 'es', 'fr', 'de', 'it', 'pt', 'ja', 'ko', 'zh'];
const UNITS = [
'bit',
'byte',
'gigabit',
'gigabyte',
'kilobit',
'kilobyte',
'megabit',
'megabyte',
'terabit',
'terabyte'
];
const CURRENCIES = ['USD', 'EUR', 'GBP', 'JPY'];
const CURRENCY_OPTIONS = [
{ style: 'symbol' },
{ compact: 'short' }
];
const NUMBER_OPTIONS = [
{ style: 'decimal' },
{ style: 'percent' }
];
const DATETIME_OPTIONS = [
{ datetime: 'full' },
{ date: 'long' },
{ time: 'full' }
];
const UNIT_OPTIONS = [
{ form: 'short', compact: 'short' },
{ form: 'long' }
];
const options = (o) =>
'{' + Object.keys(o).map(k => `${k}: ${JSON.stringify(o[k])}`).join(', ') + '}';
const makeDateFormatter = (o) =>
`Globalize.dateFormatter(${options(o)});\n`;
const makeCurrencyFormatter = (code, o) =>
`Globalize.currencyFormatter('${code}', ${options(o)});\n`;
const makeNumberFormatter = (o) =>
`Globalize.numberFormatter(${options(o)});\n`;
const makeUnitFormatter = (unit, o) =>
`Globalize.unitFormatter('${unit}', ${options(o)});\n`;
const makeTimezoneFormatter = (tz) =>
`Globalize.dateFormatter(${options({ timeZone: tz })});\n`;
const build = (withtz) => {
let s = '';
UNITS.forEach(u => {
UNIT_OPTIONS.forEach(o => {
s += makeUnitFormatter(u, o);
});
});
DATETIME_OPTIONS.forEach(o => {
s += makeDateFormatter(o);
});
if (withtz) {
TIMEZONES.forEach(tz => {
s += makeTimezoneFormatter(tz);
});
}
CURRENCIES.forEach(code => {
CURRENCY_OPTIONS.forEach(o => {
s += makeCurrencyFormatter(code, o);
});
})
NUMBER_OPTIONS.forEach(o => {
s += makeNumberFormatter(o);
});
return s;
};
const buildLite = () => {
let s = '';
s += makeDateFormatter({ date: 'long' });
s += makeDateFormatter({ time: 'long' });
s += makeNumberFormatter({});
CURRENCIES.forEach(code => {
s += makeCurrencyFormatter(code, {});
});
s += makeUnitFormatter('megabyte', {});
return s;
};
const HEADER = `
import Globalize from 'globalize/dist/globalize-runtime';
`;
const FOOTER = `
export default Globalize;
`;
const compile = (name, langs, source) => {
const outdir = join(__dirname, 'globalize-output');
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir);
}
let path = join(outdir, `source-${source.name}.js`);
fs.writeFileSync(path, source.code, { encoding: 'utf-8' });
const start = process.hrtime();
const extracts = compiler.extract(source.code);
const locales = getlocales(new Set(langs));
let res = HEADER;
for (const locale of locales) {
res += compiler.compileExtracts({
extracts,
defaultLocale: locale,
template: props => `\n${props.code}\n`
});
}
res += FOOTER;
const buf = Buffer.from(res, 'utf-8');
const elapsed = process.hrtime(start);
const secs = elapsed[0] + (elapsed[1] / 1e9);
path = join(outdir, `compiled-${source.name}-${name}.js`);
fs.writeFileSync(path, res, { encoding: 'utf-8' });
console.log(`languages: ${langs.join(', ')} ${source.name}`);
console.log(` compile ${secs} secs`);
console.log(` size ${decimal(res.length)} characters ${decimal(buf.length)} bytes`);
let data = zlib.gzipSync(buf, { level: zlib.constants.Z_BEST_COMPRESSION });
console.log(` gzip ${decimal(data.length)} bytes`);
path = join(outdir, `compiled-${source.name}-${name}.js.gz`);
fs.writeFileSync(path, data, { encoding: 'binary' });
data = brotli.compressSync(buf, { });
console.log(` brotli ${decimal(data.length)} bytes`);
path = join(outdir, `compiled-${source.name}-${name}.js.br`);
fs.writeFileSync(path, data, { encoding: 'binary' });
console.log();
};
const getlocales = (langs) =>
ALL_LOCALES.filter(l => langs.has(l.split('-')[0]));
for (const lite of [true, false]) {
if (lite) {
const code = buildLite();
const source = { name: 'lite', code };
for (let i = 0; i < LANGS.length; i++) {
const langs = LANGS.slice(0, i + 1);
const name = langs.join('_')
compile(name, langs, source);
}
const allset = new Set(ALL_LOCALES.filter(l => l !== 'root').map(l => l.split('-')[0]));
let alllangs = [];
allset.forEach(l => alllangs.push(l));
compile('all', alllangs, source);
} else {
let source = { name: 'example-app', code: build(false) };
for (let i = 0; i < LANGS.length; i++) {
const langs = LANGS.slice(0, i + 1);
const name = langs.join('_')
compile(name, langs, source);
}
source = { name: 'example-app-tz', code: build(true) };
// one with timezones
compile('en', ['en'], source);
// all locales + timezones
compile(LANGS.join('_'), LANGS, source);
}
}