-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathesbuild.js
56 lines (48 loc) · 1.6 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
const fs = require('fs')
const path = require('path')
const esbuild = require('esbuild')
const rmrf = require('rimraf')
const pug = require('pug')
const pretty = require('pretty')
rmrf.sync('gen')
require('zotero-plugin/copy-assets')
require('zotero-plugin/rdf')
require('zotero-plugin/version')
async function bundle(entry) {
const outdir = path.join('build', path.basename(path.dirname(entry)))
const config = {
entryPoints: [ entry ],
outdir,
bundle: true,
format: 'iife',
target: ['firefox60'],
treeShaking: true,
minify: false,
drop: ['console'],
external: [ 'zotero/itemTree' ]
}
const target = path.join(outdir, path.basename(entry).replace(/[.]ts$/, '.js'))
const esm = await esbuild.build({ ...config, logLevel: 'silent', format: 'esm', metafile: true, write: false })
const postfix = `$$${Date.now()}`
for (const output of Object.values(esm.metafile.outputs)) {
if (output.entryPoint) {
config.globalName = `${escape(`{ ${output.exports.join(', ')} }`).replace(/%/g, '$')}${postfix}`
console.log(config.globalName)
}
}
await esbuild.build(config)
await fs.promises.writeFile(
target,
(await fs.promises.readFile(target, 'utf-8')).replace(config.globalName, unescape(config.globalName.replace(postfix, '').replace(/[$]/g, '%')))
)
}
async function build() {
await bundle('bootstrap.ts')
await bundle('content/folder-import.ts')
await bundle('content/bulkimport.ts')
fs.writeFileSync('build/content/wizard.xhtml', pretty(pug.compileFile('content/wizard.pug')()))
}
build().catch(err => {
console.log(err)
process.exit(1)
})