forked from GMOD/jbrowse-plugin-gwas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.mjs
More file actions
99 lines (92 loc) · 2.85 KB
/
esbuild.mjs
File metadata and controls
99 lines (92 loc) · 2.85 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
88
89
90
91
92
93
94
95
96
97
98
99
import fs from 'node:fs'
import http from 'node:http'
import * as esbuild from 'esbuild'
import { globalExternals } from '@fal-works/esbuild-plugin-global-externals'
import JBrowseReExports from '@jbrowse/core/ReExports/list'
import prettyBytes from 'pretty-bytes'
const isWatch = process.argv.includes('--watch')
const PORT = process.env.PORT ? +process.env.PORT : 9000
function createGlobalMap(jbrowseGlobals) {
const globalMap = {}
for (const global of jbrowseGlobals) {
globalMap[global] = {
varName: `JBrowseExports["${global}"]`,
type: 'cjs',
}
}
// Map @jbrowse/mobx-state-tree to mobx-state-tree for backwards compatibility
// In v4.0.0+, JBrowse uses @jbrowse/mobx-state-tree but exports it as 'mobx-state-tree'
// In v3.x, JBrowse used mobx-state-tree directly
globalMap['@jbrowse/mobx-state-tree'] = {
varName: `JBrowseExports["mobx-state-tree"]`,
type: 'cjs',
}
return globalMap
}
const rebuildLogPlugin = {
name: 'rebuild-log',
setup({ onStart, onEnd }) {
let time
onStart(() => {
time = Date.now()
})
onEnd(({ metafile, errors, warnings }) => {
console.log(
`Built in ${Date.now() - time} ms with ${errors.length} error(s) and ${warnings.length} warning(s)`,
)
if (metafile) {
for (const [file, metadata] of Object.entries(metafile.outputs)) {
console.log(`Wrote ${prettyBytes(metadata.bytes)} to ${file}`)
}
}
})
},
}
const globals = JBrowseReExports
const config = {
entryPoints: ['src/index.ts'],
bundle: true,
globalName: 'JBrowsePluginGWAS',
metafile: true,
plugins: [globalExternals(createGlobalMap(globals)), rebuildLogPlugin],
...(isWatch
? { outfile: 'dist/out.js' }
: {
outfile: 'dist/jbrowse-plugin-gwas.umd.production.min.js',
sourcemap: true,
minify: true,
}),
}
if (isWatch) {
const ctx = await esbuild.context(config)
const internalPort = PORT + 400
const { hosts } = await ctx.serve({ servedir: '.', port: internalPort })
http
.createServer((req, res) => {
const proxyReq = http.request(
{
hostname: hosts[0],
port: internalPort,
path: req.url,
method: req.method,
headers: req.headers,
},
proxyRes => {
// restore CORS after https://github.com/evanw/esbuild/releases/tag/v0.25.0 disabled it
res.writeHead(proxyRes.statusCode, {
...proxyRes.headers,
'Access-Control-Allow-Origin': '*',
})
proxyRes.pipe(res, { end: true })
},
)
req.pipe(proxyReq, { end: true })
})
.listen(PORT)
console.log(`Serving at http://${hosts[0]}:${PORT}`)
await ctx.watch()
console.log('Watching files...')
} else {
const result = await esbuild.build(config)
fs.writeFileSync('meta.json', JSON.stringify(result.metafile))
}