-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-web.js
57 lines (49 loc) · 1.37 KB
/
build-web.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
'use strict'
const { build } = require('esbuild')
const { writeFile, readFile } = require('fs').promises
const { minify } = require('html-minifier-terser')
const { sassPlugin } = require('esbuild-sass-plugin')
const browserSync = require('browser-sync')
const port = process.env.PORT || 3333
const htmlOpts = {
collapseWhitespace: true,
keepClosingSlash: true,
minifyCSS: true,
removeComments: true,
removeRedundantAttributes: true
}
const config = {
entryPoints: ['./web/entry.js'],
bundle: true,
format: 'esm',
minify: process.env.NODE_ENV === 'production',
loader: {
'.js': 'jsx',
'.svg': 'text'
},
outfile: 'build/bundle.js',
plugins: [sassPlugin({ type: ['css-text'] })],
define: {
'process.env.BENCHMARK_HOSTNAME': JSON.stringify(process.env.BENCHMARK_HOSTNAME) || '""'
}
}
async function buildWeb () {
if (process.env.NODE_ENV === 'development') {
const bs = browserSync.create()
bs.init({ proxy: `localhost:${port}` })
config.sourcemap = 'inline'
config.watch = {
onRebuild (error) {
if (error) console.error('watch build failed:', error)
bs.reload()
}
}
}
await build(config)
const htmlFile = await readFile('./web/index.html', 'utf8')
await writeFile(
'build/index.html',
process.env.NODE_ENV === 'production' ? await minify(htmlFile, htmlOpts) : htmlFile
)
}
buildWeb()