-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.js
53 lines (46 loc) · 1.3 KB
/
esbuild.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
import * as esbuild from 'esbuild';
import { gzipSizeFromFileSync } from 'gzip-size';
[
{
format: 'esm',
outdir: 'dist/esm'
},
{
format: 'cjs',
outdir: 'dist/cjs',
},
{
format: 'iife',
keepNames: true,
outdir: 'dist/iife',
},
].forEach((config) => {
const result = esbuild.buildSync({
bundle: true,
entryNames: '[dir]/[name]',
entryPoints: ['index.js'],
metafile: true,
minify: true,
...config,
});
let total = 0;
let totalGzip = 0;
Object.keys(result.metafile.outputs).forEach((key) => {
total = total + result.metafile.outputs[key].bytes;
totalGzip = totalGzip + gzipSizeFromFileSync(key);
console.log(
key,
formatBytes(result.metafile.outputs[key].bytes),
'- gzip:',
formatBytes(gzipSizeFromFileSync(key))
);
});
console.log('Total:', formatBytes(total), '- gzip:', formatBytes(totalGzip), '\n');
});
// https://stackoverflow.com/a/18650828
function formatBytes(a, b = 2) {
if (0 === a) return '0 Bytes';
const c = 0 > b ? 0 : b,
d = Math.floor(Math.log(a) / Math.log(1024));
return parseFloat((a / Math.pow(1024, d)).toFixed(c)) + ' ' + ['B', 'KB', 'MB', 'GB'][d];
}