-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
60 lines (57 loc) · 1.49 KB
/
build.ts
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
const { build } = require('esbuild');
const bs = require('browser-sync').create();
const historyApiFallback = require('connect-history-api-fallback')
const fse = require('fs-extra');
const isDev = process.argv.includes('--dev');
const outdir = 'dist';
try {
fse.rmSync(outdir, { recursive: true, force: true });
fse.mkdirSync(outdir);
fse.copy('src/public', outdir);
} catch (err) {
console.error(`initialize dist failed:`, err);
process.exit(1);
}
build({
define: { 'process.env.NODE_ENV': process.env.NODE_ENV },
target: 'es2015',
platform: 'browser',
entryPoints: ['src/main.tsx'],
inject: ['config/react-shim.ts'],
outdir,
bundle: true,
minify: !isDev,
sourcemap: isDev,
treeShaking: true,
watch: isDev && {
onRebuild(err, result) {
console.log(`${new Date().toLocaleString()}: Rebuild`)
if(err){
console.log(err?.errors);
}
if(result?.warnings?.length){
console.log(result?.warnings);
}
},
},
}).then(result => {
if (isDev) {
console.log(`${new Date().toLocaleString()}: Watching source files...`);
bs.init({
server: outdir,
middleware: [historyApiFallback()],
watch: true,
open: false,
}, (err) => {
if (err) {
console.error(`${new Date().toLocaleString()}: Serve Failed:`, err);
bs.exit();
}
});
} else {
console.log(`${new Date().toLocaleString()}: Build Finish`)
}
}).catch(err => {
console.log(`Error: ${JSON.stringify(err)}`)
process.exit(1);
});