-
Notifications
You must be signed in to change notification settings - Fork 9
/
esbuild.config.cjs
57 lines (50 loc) · 1.91 KB
/
esbuild.config.cjs
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
const fs = require('fs')
const esbuild = require('esbuild')
// XXX taken from https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
// XXX `plugins: [makeAllPackagesExternalPlugin]` were supposed to be superceeded by
// XXX `external: ['./node_modules/*']` but...
// XXX ... we're blocked by what seems to be https://github.com/evanw/esbuild/issues/1958
const makeAllPackagesExternalPlugin = {
name: 'make-all-packages-external',
setup: (build) => {
const filter = /^[^./]|^\.[^./]|^\.\.[^/]/ // Must not start with "/" or "./" or "../"
build.onResolve({ filter }, (arguments_) => ({ external: true, path: arguments_.path }))
}
}
const catcher = (error) => {
console.error(error)
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1)
}
const targets = { Browser: 'browser', Node: 'node' }
const builder = (entryPoints, outdir = 'dist', platform = targets.Node) =>
esbuild
.build({
bundle: true,
entryPoints,
format: 'esm',
metafile: true,
minify: true,
outdir,
platform,
sourcemap: true,
sourcesContent: false,
...(platform === targets.Node
? { plugins: [makeAllPackagesExternalPlugin], target: 'node14' }
: {
inject: ['src/ui/react-shim.ts'],
jsxFactory: 'h',
jsxFragment: 'Fragment',
target: ['es2018', 'chrome58', 'firefox57', 'safari11', 'edge18', 'ios11']
})
})
.then((result) => esbuild.analyzeMetafile(result.metafile))
.then(console.info)
fs.mkdirSync('dist/homebridge-ui/public', { recursive: true })
fs.copyFileSync('src/ui/public/index.html', 'dist/homebridge-ui/public/index.html')
const deliverables = [
[['src/index.ts', 'src/pair.ts']],
[['src/ui/server.ts'], 'dist/homebridge-ui/'],
[['src/ui/index.tsx'], 'dist/homebridge-ui/public/', targets.Browser]
]
for (const payload of deliverables) builder(...payload).catch(catcher)