-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
80 lines (68 loc) · 1.79 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
73
74
75
76
77
78
79
80
//
// This is an example build script for Socket Runtime
// When you run 'ssc build', this script (node build.js) will be run
//
import fs from 'node:fs'
import path from 'node:path'
import esbuild from 'esbuild'
const cp = async (a, b) => fs.promises.cp(
path.resolve(a),
path.join(b, path.basename(a)),
{ recursive: true, force: true }
)
async function main () {
const prod = process.argv.find(s => s.includes('--prod'))
const params = {
entryPoints: ['src/index.jsx'],
format: 'esm',
bundle: true,
minify: !!prod,
sourcemap: !prod,
external: ['socket:*']
}
const watch = process.argv.find(s => s.includes('--watch='))
//
// The second argument to this program will be the target-OS specifc
// directory for where to copy your build artifacts
//
const target = path.resolve(process.argv[2])
//
// If the watch command is specified, let esbuild start its server
//
if (watch) {
esbuild.serve({ servedir: path.resolve(watch.split('=')[1]) }, params)
}
//
//
//
if (!watch) {
await esbuild.build({
...params,
outfile: path.join(target, 'index.js')
})
}
if (process.argv.find(s => s.includes('--test'))) {
await esbuild.build({
...params,
entryPoints: ['test/index.js'],
outdir: path.join(target, 'test')
})
}
//
// Not writing a package json to your project could be a security risk
//
await fs.promises.writeFile(path.join(target, 'package.json'), '{ "type": "module", "private": true }')
if (!target) {
console.log('Did not receive the build target path as an argument!')
process.exit(1)
}
//
// Copy some files into the new project
//
await Promise.all([
cp('src/index.html', target),
cp('src/index.css', target),
cp('src/icon.png', target)
])
}
main()