-
Notifications
You must be signed in to change notification settings - Fork 46
/
esbuild.js
97 lines (80 loc) · 1.88 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
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
const esbuild = require('esbuild')
const { stat, writeFile, rm, mkdir } = require('fs/promises')
const pkgJson = require('./package.json')
const outputDir = 'build'
function cleanPkgJson (json) {
delete json.devDependencies
delete json.optionalDependencies
delete json.dependencies
delete json.workspaces
return json
}
async function emptyDir (dir) {
try {
await rm(dir, { recursive: true })
} catch (err) {
if (err.code !== 'ENOENT') {
throw err
}
}
await mkdir(dir)
}
async function printSize (fileName) {
const stats = await stat(fileName)
// print size in MB
console.log(`Bundle size: ${Math.round(stats.size / 10000) / 100}MB\n\n`)
}
async function main () {
const start = Date.now()
// clean build folder
await emptyDir(outputDir)
const outfile = `${outputDir}/index.js`
/** @type { import('esbuild').BuildOptions } */
const config = {
entryPoints: [pkgJson.bin.dotenvx],
bundle: true,
platform: 'node',
target: 'node18',
sourcemap: true,
outfile,
// suppress direct-eval warning
logOverride: {
'direct-eval': 'silent'
}
}
await esbuild.build(config)
console.log(`Build took ${Date.now() - start}ms`)
await printSize(outfile)
if (process.argv.includes('--minify')) {
// minify the file
await esbuild.build({
...config,
entryPoints: [outfile],
minify: true,
keepNames: true,
allowOverwrite: true,
outfile
})
console.log(`Minify took ${Date.now() - start}ms`)
await printSize(outfile)
}
// create main patched packege.json
cleanPkgJson(pkgJson)
pkgJson.scripts = {
start: 'node index.js'
}
pkgJson.bin = 'index.js'
pkgJson.pkg = {
assets: [
'*.map'
]
}
await writeFile(
`${outputDir}/package.json`,
JSON.stringify(pkgJson, null, 2)
)
}
main().catch(err => {
console.error(err)
process.exit(1)
})