-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild.js
More file actions
78 lines (70 loc) · 2.09 KB
/
build.js
File metadata and controls
78 lines (70 loc) · 2.09 KB
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
import fs from 'node:fs/promises'
import esbuild from 'esbuild'
import { minify } from '@swc/core'
import { gzipSizeSync } from 'gzip-size'
import * as prettier from 'prettier'
await fs.rm('dist', { recursive: true, force: true })
// build the dist.dev.js (for development)
{
const filepath = 'dist/dist.dev.js'
await esbuild.build({
entryPoints: ['src/web.js'],
outfile: filepath,
bundle: true,
legalComments: 'eof',
minifyWhitespace: true,
drop: ['debugger'],
})
const formatted = await prettier.format(await fs.readFile(filepath, 'utf8'), {
parser: 'babel',
tabWidth: 2,
semi: false,
})
await fs.writeFile(filepath, formatted)
console.log(` 📄 ${filepath} (${await filesize(filepath)})`)
}
// build the dist.js
{
{
const filepath = 'dist/dist.js'
await esbuild.build({
entryPoints: ['src/web.js'],
outfile: filepath,
bundle: true,
legalComments: 'eof',
minifyWhitespace: true,
drop: ['debugger', 'console'],
dropLabels: ['DEV'],
})
const formatted = await prettier.format(await fs.readFile(filepath, 'utf8'), {
parser: 'babel',
tabWidth: 2,
semi: false,
})
await fs.writeFile(filepath, formatted)
console.log(` 📄 ${filepath} (${await filesize(filepath)})`)
}
}
// build the dist.min.js
{
const minified = await minify(await fs.readFile('dist/dist.js', { encoding: 'utf-8' }), {
format: {
comments: false,
},
compress: {
drop_console: true,
},
mangle: true,
})
await fs.writeFile('dist/dist.min.js', minified.code)
console.log(
` 📄 dist/dist.min.js (${await filesize('dist/dist.min.js')} ~= ${gzipsize(minified.code)} gzip)`
)
}
async function filesize(filename) {
const stats = await fs.stat(filename)
return (stats.size / 1000).toFixed(2) + 'kb'
}
function gzipsize(code) {
return (gzipSizeSync(code) / 1000).toFixed(3) + 'kb'
}