-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
56 lines (51 loc) · 1.33 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
const { resolve } = require('path')
const { build, buildSync } = require('esbuild')
const buildWorkerScript = () => {
const result = buildSync({
format: 'iife',
entryPoints: [resolve(__dirname, './src/worker.ts')],
bundle: true,
minify: true,
write: false,
platform: 'browser',
loader: {
'.ts': 'ts',
},
tsconfig: resolve(__dirname, './tsconfig.json'),
})
return Buffer.from(result.outputFiles[0].contents).toString('utf-8')
}
const run = () => {
const workerScriptContent = JSON.stringify(buildWorkerScript())
const formats = {
cjs: 'index.js',
esm: 'index.esm.js',
iife: 'index.iife.js',
}
Object.keys(formats).forEach((format) => {
const fileName = formats[format]
build({
format,
globalName: 'funcWork',
entryPoints: [resolve(__dirname, './src/index.ts')],
outfile: resolve(__dirname, './dist/', fileName),
bundle: true,
minify: true,
sourcemap: true,
platform: 'browser',
loader: {
'.ts': 'ts',
},
define: {
__WORKER_SCRIPT__: workerScriptContent,
},
tsconfig: resolve(__dirname, './tsconfig.json'),
}).then(() => {
console.info(`— ${fileName} was built`)
}).catch((e) => {
console.info(`🚨 ${fileName} build error:`)
console.error(e)
})
})
}
run()