-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
bundle_esbuild.js
80 lines (76 loc) · 2.06 KB
/
bundle_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
import * as esbuild from 'esbuild'
import path from 'node:path'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// esbuild でバンドルするファイルのリスト
const files = [
// main code
'src/wnako3.mts',
// mjs
'src/wnako3webworker.mjs',
'src/plugin_webworker.mjs',
'src/plugin_kansuji.mjs',
'src/plugin_markup.mjs',
'src/plugin_datetime.mjs',
'src/plugin_caniuse.mjs',
// mts
'src/plugin_keigo.mts',
'src/plugin_three.mts',
'src/plugin_turtle.mts',
'src/plugin_weykturtle3d.mts',
'editor/edit_main.jsx',
'editor/version_main.jsx'
]
const outdir = path.join(__dirname, 'release')
const watchMode = process.argv.includes('--watch')
const filesFullpath = files.map((f) => path.join(__dirname, f))
// create outdir
if (!fs.existsSync(outdir)) {
fs.mkdirSync(outdir)
console.log(`[esbuild] created ${outdir}`)
}
// build options
if (!watchMode) {
// normal mode
for (const f of files) {
const outfile = path.join(outdir, path.basename(f).replace(/\.(mjs|mts|jsx)$/, '.js'))
const options = {
entryPoints: [f],
bundle: true,
outfile,
minify: true,
sourcemap: true,
}
console.log('[esbuild]', f)
await esbuild.build(options)
}
// 例外的なコピー
const src = path.join(outdir, 'edit_main.js')
if (fs.existsSync(src)) {
fs.copyFileSync(src, path.join(outdir, 'editor.js'))
fs.copyFileSync(path.join(outdir, 'version_main.js'), path.join(outdir, 'version.js'))
}
} else {
// watch mode
const ctxList = []
for (const f of files) {
const outfile = path.join(outdir, path.basename(f).replace(/\.(mjs|mts|jsx)$/, '.js'))
const options = {
entryPoints: [f],
bundle: true,
outfile,
minify: true,
sourcemap: true,
}
const c = await esbuild.context(options)
c.opt = options
ctxList.push(c)
}
for (const ctx of ctxList) {
ctx.watch().then(()=>{
console.log('[esbuild] watch', ctx.opt.entryPoints)
})
}
}