-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
72 lines (57 loc) · 1.59 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
const esbuild = require('esbuild')
const pluginVue = require('esbuild-plugin-vue3')
const fs = require('fs-extra')
const path = require('path')
const pug = require('pug')
const chokidar = require('chokidar')
const pkg = require('./package.json')
const outdir = 'build/res'
const watchPaths = [
'src/index.pug',
'src/admin.pug',
'src/login.pug',
'src/vue'
]
// Parse args
const args = process.argv.slice(2)
let watch = false
for (let arg of args)
if (arg == '--watch') watch = true
async function build() {
console.log('Building...')
await fs.remove(outdir)
await fs.copy('src/resources', outdir)
let result = await esbuild.build({
entryPoints: ['src/vue/main.js'],
bundle: true,
minify: true,
sourcemap: true,
outdir: outdir + '/http',
entryNames: '[name]-[hash]',
metafile: true,
plugins: [pluginVue()]
})
let vars = {version: pkg.version}
let outputs = Object.keys(result.metafile.outputs)
for (let output of outputs) {
let href = '/' + path.basename(output)
if (output.endsWith('.js')) vars.js = href
if (output.endsWith('.css')) vars.css = href
}
for (let name of ['index', 'admin', 'login']) {
let html = pug.compileFile('src/' + name + '.pug')(vars)
await fs.writeFile(outdir + '/http/' + name + '.html', html)
}
}
async function main() {
await build()
if (watch) {
console.log('Watching...')
chokidar.watch(watchPaths)
.on('change', async (path, stats) => {
console.log('Change detected in', path)
await build()
})
}
}
main().catch(e => console.log(e))