-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.config.mjs
57 lines (50 loc) · 1.87 KB
/
esbuild.config.mjs
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
import { join } from 'path'
import { readFileSync, writeFileSync, existsSync, truncate } from 'fs'
import * as esbuild from 'esbuild'
const watch = process.argv.includes('--watch') || process.env.WATCH === 'true'
// Current options: chrome, firefox, safari, safari_ios
const target = 'chrome'
process.env.NODE_ENV ||= 'development'
const baseUrl = process.env.NODE_ENV === 'production' ? 'https://www.convus.org' : 'http://localhost:3009'
const version = process.env.npm_package_version
const replaceEnvValues = (str) => {
return str.replace(/{{baseUrl}}/g, baseUrl)
.replace(/{{target}}/g, target)
.replace(/{{version}}/g, version)
}
// HACK HACK HACK! building things by doing substitution
// TODO: make this a better process
const htmlContent = readFileSync('src/index.html', 'utf8')
writeFileSync('dist/index.html', replaceEnvValues(htmlContent))
// manifest
const manifestContent = readFileSync('src/manifest_v3.json', 'utf8')
writeFileSync('dist/manifest.json', replaceEnvValues(manifestContent))
// esbuild, go to town
const errorFilePath = 'esbuild_error'
const watchOptions = {
onRebuild (error, result) {
if (error) {
console.error('watch build failed:', error)
writeFileSync(errorFilePath, error.toString())
} else if (existsSync(errorFilePath)) {
console.log(`${target} - watch build succeeded:`, result)
truncate(errorFilePath, 0, () => { })
}
}
}
esbuild.build({
define: {
'process.env.baseUrl': `"${baseUrl}"`,
'process.env.browser_target': `"${target}"`,
'process.env.version': `"${version}"`,
'process.env.NODE_ENV': `"${process.env.NODE_ENV}"`
},
entryPoints: ['popup.js'],
bundle: true,
sourcemap: true,
outdir: join(process.cwd(), 'dist'),
absWorkingDir: join(process.cwd(), 'src'),
watch: watch && watchOptions,
plugins: []
})
.then((result) => console.log(`${target} - esbuild updated:`, result))