-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
73 lines (64 loc) · 1.91 KB
/
esbuild.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
const path = require('path')
const fs = require('fs')
const esbuild = require('esbuild')
const rmrf = require('rimraf')
rmrf.sync('gen')
require('zotero-plugin/copy-assets')
require('zotero-plugin/rdf')
require('zotero-plugin/version')
function js(src) {
return src.replace(/[.]ts$/, '.js')
}
async function bundle(config) {
config = {
bundle: true,
format: 'iife',
target: ['firefox60'],
inject: [],
treeShaking: true,
keepNames: true,
...config,
}
let target
if (config.outfile) {
target = config.outfile
}
else if (config.entryPoints.length === 1 && config.outdir) {
target = path.join(config.outdir, js(path.basename(config.entryPoints[0])))
}
else {
target = `${config.outdir} [${config.entryPoints.map(js).join(', ')}]`
}
const exportGlobals = config.exportGlobals
delete config.exportGlobals
if (exportGlobals) {
const esm = await esbuild.build({ ...config, logLevel: 'silent', format: 'esm', metafile: true, write: false })
if (Object.values(esm.metafile.outputs).length !== 1) throw new Error('exportGlobals not supported for multiple outputs')
for (const output of Object.values(esm.metafile.outputs)) {
if (output.entryPoint) {
config.globalName = escape(`{ ${output.exports.sort().join(', ')} }`).replace(/%/g, '$')
// make these var, not const, so they get hoisted and are available in the global scope.
}
}
}
console.log('* bundling', target)
await esbuild.build(config)
if (exportGlobals) {
await fs.promises.writeFile(
target,
(await fs.promises.readFile(target, 'utf-8')).replace(config.globalName, unescape(config.globalName.replace(/[$]/g, '%')))
)
}
}
async function build() {
await bundle({
exportGlobals: true,
entryPoints: [ 'bootstrap.ts' ],
outdir: 'build',
banner: { js: 'var Zotero;\n' },
})
}
build().catch(err => {
console.log(err)
process.exit(1)
})