-
Notifications
You must be signed in to change notification settings - Fork 33
/
build.js
77 lines (68 loc) · 1.38 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
/**
* build.js
* @ndaidong
**/
import { readFileSync, writeFileSync, rmSync, mkdirSync } from 'fs'
import { buildSync } from 'esbuild'
const pkg = JSON.parse(readFileSync('./package.json', { encoding: 'utf-8' }))
rmSync('dist', {
force: true,
recursive: true
})
mkdirSync('dist')
const buildTime = (new Date()).toISOString()
const comment = [
`// ${pkg.name}@${pkg.version}, by ${pkg.author}`,
`built with esbuild at ${buildTime}`,
`published under ${pkg.license} license`
].join(' - ')
/**
* @type {import('esbuild').BuildOptions}
* */
const baseOpt = {
entryPoints: ['src/main.js'],
bundle: true,
charset: 'utf8',
target: ['es2020', 'node14'],
minify: true,
write: true,
sourcemap: 'external',
external: ['canvas']
}
/**
* @type {import('esbuild').BuildOptions}
*/
const cjsVersion = {
...baseOpt,
platform: 'node',
format: 'cjs',
mainFields: ['main'],
outfile: `dist/cjs/${pkg.name}.js`,
banner: {
js: comment
}
}
buildSync(cjsVersion)
const cjspkg = {
name: pkg.name + '-cjs',
version: pkg.version,
main: `./${pkg.name}.js`
}
writeFileSync(
'dist/cjs/package.json',
JSON.stringify(cjspkg, null, ' '),
'utf8'
)
/**
* @type {import('esbuild').BuildOptions}
*/
const browserVersion = {
...baseOpt,
platform: 'browser',
format: 'esm',
outfile: `dist/${pkg.name}.browser.js`,
banner: {
js: comment
}
}
buildSync(browserVersion)