-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
build.js
136 lines (109 loc) · 3.46 KB
/
build.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
const esbuild = require('esbuild');
const fs = require('fs');
const path = require('path');
const _ = require('underscore');
const gzip = require('gzip-size');
const publishDateString = (new Date()).toISOString();
const config = getJSON(path.resolve(__dirname, '../package.json'));
const paths = {
entry: path.resolve(__dirname, '../src/two.js'),
umd: path.resolve(__dirname, '../build/two.js'),
esm: path.resolve(__dirname, '../build/two.module.js'),
min: path.resolve(__dirname, '../build/two.min.js'),
license: path.resolve(__dirname, '../LICENSE')
};
async function buildModules() {
esbuild.buildSync({
entryPoints: [paths.entry],
outfile: paths.umd,
bundle: true,
minify: false,
format: 'iife',
globalName: 'Two'
});
esbuild.buildSync({
entryPoints: [paths.entry],
outfile: paths.esm,
bundle: true,
target: 'es6',
format: 'esm'
});
esbuild.buildSync({
entryPoints: [paths.entry],
outfile: paths.min,
bundle: true,
minify: true,
format: 'iife',
globalName: 'Two'
});
const license = await fs.promises.readFile(paths.license, { encoding: 'utf-8' });
const licenseComment = ['/*', license.trim(), '*/'].join('\n');
const umdOutput = await fs.promises.readFile(paths.umd);
const esmOutput = await fs.promises.readFile(paths.esm);
const minOutput = await fs.promises.readFile(paths.min);
const moduleExports = `(function(){if(typeof exports==='object'&&typeof module!=='undefined'){module.exports=Two}})()`;
return Promise.all([
fs.promises.writeFile(paths.umd, [licenseComment, template(umdOutput, true), moduleExports].join('\n')),
fs.promises.writeFile(paths.esm, [licenseComment, template(esmOutput, false)].join('\n')),
fs.promises.writeFile(paths.min, [licenseComment, template(minOutput, true), moduleExports].join('\n'))
]);
}
function template(buffer, isExposed) {
const code = buffer.toString();
const generate = _.template(code);
let result = generate({
version: config.version,
publishDate: publishDateString
});
if (isExposed) {
result = result.replace(/\}\)\(\);/, '})().default;');
}
return result;
}
function publishModule() {
let size;
const result = {};
size = getFileSize(path.resolve(__dirname, '../build/two.js'));
result.development = formatFileSize(size);
size = getFileSize(path.resolve(__dirname, '../build/two.min.js'));
result.production = formatFileSize(size);
const contents = JSON.stringify(result);
const outputPath = path.resolve(__dirname, './file-sizes.json');
return fs.promises.writeFile(outputPath, contents);
}
function getFileSize(filepath) {
const file = fs.readFileSync(filepath);
return gzip.sync(file);
}
function formatFileSize(v) {
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
let iterations = 0;
while (v > 1000) {
v *= 0.001;
iterations++;
}
return [Math.round(v), sizes[iterations]].join('');
}
async function build() {
let startTime, elapsed;
startTime = Date.now();
try {
await buildModules();
} catch (error) {
console.log(error);
}
elapsed = Date.now() - startTime;
console.log('Built and minified Two.js:', elapsed / 1000, 'seconds');
elapsed = Date.now() - startTime;
try {
await publishModule();
} catch (error) {
console.log(error);
}
console.log('Published additional statistics to wiki:', elapsed / 1000, 'seconds');
}
function getJSON(filepath) {
const buffer = fs.readFileSync(filepath);
return JSON.parse(buffer);
}
build();