-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.config.ts
86 lines (75 loc) · 2.13 KB
/
build.config.ts
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
import fs from 'node:fs'
import path from 'node:path'
import coolConsole from 'kleur'
const entrypoints = [
'src/content/main.ts',
// 'src/popup/popup.ts',
'src/temp-tab/temp-tab.ts',
'src/sandbox/sandbox.ts',
'src/background/service-worker.ts',
].map((file) => path.join(process.cwd(), file))
const copyFiles = [
'manifest.json',
'src/sandbox/sandbox.html',
// 'src/popup/popup.html',
'src/temp-tab/temp-tab.html',
'src/style.css',
'assets',
]
const buildScripts = async () => {
// remove the dist folder
await Bun.$`rm -rf ./dist && rm -rf extension.zip && echo 'removed dist dir and extension.zip'`.nothrow()
const buildOutput = await Bun.build({
entrypoints,
outdir: './dist',
naming: '[name].[ext]',
loader: {
'.html': 'file',
},
target: 'browser',
sourcemap: 'external',
})
if (buildOutput.logs.length) {
console.error(buildOutput.logs)
throw new Error('build failed')
}
for (const file of copyFiles) {
const isDir = fs.statSync(file).isDirectory()
if (isDir) {
const dest = path.join('dist', file)
console.log(`copying directory ${file} to ${dest}`)
fs.mkdirSync(dest, { recursive: true })
fs.cpSync(file, dest, { recursive: true })
} else {
const dest = path.join('dist', path.basename(file))
console.log(`copying file ${file} to ${dest}`)
fs.copyFileSync(file, dest)
}
}
console.log(coolConsole.blue('build complete'))
if (!buildOutput.success) {
console.log(coolConsole.red('build failed'))
process.exit(1)
}
return buildOutput
}
if (import.meta.main) {
console.log(coolConsole.green('Building scripts'))
await buildScripts()
}
if (process.argv.includes('--watch')) {
const watcher = fs.watch(process.cwd(), { recursive: true })
watcher.addListener('change', async (_event, filename) => {
if (typeof filename !== 'string') return
const absolutePath = path.join(process.cwd(), filename)
if (absolutePath.includes('dist')) return
if (absolutePath.includes('node_modules')) return
if (absolutePath.includes('.git')) return
if (absolutePath.includes('.vscode')) return
await buildScripts()
})
process.on('SIGINT', () => {
watcher.close()
process.exit(0)
})
}