-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
87 lines (78 loc) · 2.04 KB
/
Copy pathbuild.js
File metadata and controls
87 lines (78 loc) · 2.04 KB
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
81
82
83
84
85
86
87
import fs from 'fs/promises';
import path from 'path';
import open from 'open';
import * as esbuild from 'esbuild';
const OUT_DIR = path.resolve('./dist');
const SERVE_DIR = path.resolve('./build');
const ENTRY_POINTS = [
'./src/index.js',
];
/**
* @param {Array<string>} args
*/
async function main(args) {
if (args.includes('--dev')) {
await dev();
} else {
await build();
}
}
main(process.argv);
async function dev() {
const isProduction = false;
// Clear output directory...
await fs.rm(SERVE_DIR, { force: true, recursive: true });
// Copy index.html
await fs.cp(path.join(path.dirname(ENTRY_POINTS[0]), 'index.html'), path.join(SERVE_DIR, '/index.html'));
// Serve it.
/** @type {esbuild.BuildOptions} */
const opts = {
entryPoints: ENTRY_POINTS,
outdir: SERVE_DIR,
bundle: true,
sourcemap: true,
loader: {
'.png': 'file',
'.json': 'json',
},
define: {
'window.IS_PRODUCTION': String(Boolean(isProduction)),
'process.platform': JSON.stringify('browser'),
},
plugins: []
};
let ctx = await esbuild.context(opts);
await ctx.watch();
let server = await ctx.serve({ servedir: SERVE_DIR });
const url = `http://${server.host}:${server.port}`;
console.log(`Hosted server at ${url} ...`);
await open(url, { wait: true });
console.log('...closing server.');
return;
}
async function build() {
const isProduction = true;
// Clear output directory...
await fs.rm(OUT_DIR, { force: true, recursive: true });
// Copy index.html
await fs.cp(path.join(path.dirname(ENTRY_POINTS[0]), '/index.html'), path.join(OUT_DIR, '/index.html'));
// Build it.
/** @type {esbuild.BuildOptions} */
const opts = {
entryPoints: ENTRY_POINTS,
outdir: OUT_DIR,
bundle: true,
minify: true,
sourcemap: true,
loader: {
'.png': 'file',
'.json': 'file',
},
define: {
'window.IS_PRODUCTION': String(Boolean(isProduction)),
'process.platform': JSON.stringify('browser'),
},
plugins: []
};
await esbuild.build(opts);
}