-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
executable file
·40 lines (35 loc) · 1.21 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
#!/usr/bin/env node
import esbuild from 'esbuild'
import { nodeExternalsPlugin } from 'esbuild-node-externals'
const watch = process.argv.slice(1).includes('--watch')
const config = {
bundle: true,
minify: true,
sourcemap: true,
target: 'es2020',
plugins: [nodeExternalsPlugin()],
}
const builds = [
{ entryPoints: ['src/index.ts'], format: 'esm', outfile: 'dist/index.esm.js', platform: 'browser' },
{ entryPoints: ['src/index.ts'], format: 'cjs', outfile: 'dist/index.js', platform: 'browser' },
{ entryPoints: ['src/server.ts'], format: 'esm', outfile: 'dist/server.esm.js', platform: 'node' },
{ entryPoints: ['src/server.ts'], format: 'cjs', outfile: 'dist/server.js', platform: 'node' },
]
builds.forEach((build) => {
esbuild
.build({ ...config, ...build, ...watcher(build) })
.then(() => console.log(`${watch ? 'Watching' : 'Built'} ${build.entryPoints} (${build.format})…`))
.catch(() => process.exit(1))
})
function watcher(build) {
return watch
? {
watch: {
onRebuild: (error) =>
error
? console.error('Watch failed:', error)
: console.log(`Rebuilding ${build.entryPoints} (${build.format})…`),
},
}
: {}
}