forked from openai-translator/openai-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
98 lines (85 loc) · 3.01 KB
/
build.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
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
import archiver from 'archiver'
import esbuild from 'esbuild'
import fs from 'fs-extra'
import inlineImportPlugin from 'esbuild-plugin-inline-import'
const browserExtensionOutDir = 'dist/browser-extension'
const userscriptOutDir = 'dist/userscript'
async function esbuildBrowserExtension() {
await esbuild.build({
target: ['es2015', 'safari11'],
entryPoints: [
'src/content_script/index.tsx',
'src/background/index.ts',
'src/options/index.tsx',
'src/popup/index.tsx',
],
bundle: true,
outdir: browserExtensionOutDir,
treeShaking: true,
minify: true,
legalComments: 'none',
sourcemap: true,
loader: {
'.png': 'dataurl',
'.jpg': 'dataurl',
'.gif': 'dataurl',
},
plugins: [
inlineImportPlugin(),
],
})
}
async function zipFolder(dir) {
const output = fs.createWriteStream(`${dir}.zip`)
const archive = archiver('zip', {
zlib: { level: 9 },
})
archive.pipe(output)
archive.directory(dir, false)
await archive.finalize()
}
async function copyFiles(entryPoints, targetDir) {
await fs.ensureDir(targetDir)
await Promise.all(
entryPoints.map(async (entryPoint) => {
await fs.copy(entryPoint.src, `${targetDir}/${entryPoint.dst}`)
})
)
}
async function build() {
await esbuildBrowserExtension()
const commonFiles = [
{ src: `public/cld-min.js`, dst: 'js/cld-min.js' },
{ src: `${browserExtensionOutDir}/content_script/index.js`, dst: 'js/content_script.js' },
{ src: `${browserExtensionOutDir}/content_script/index.js.map`, dst: 'js/content_script.js.map' },
{ src: `${browserExtensionOutDir}/background/index.js`, dst: 'js/background.js' },
{ src: `${browserExtensionOutDir}/options/index.js`, dst: 'js/options.js' },
{ src: `${browserExtensionOutDir}/options/index.css`, dst: 'css/options.css' },
{ src: 'public/options.html', dst: 'options.html' },
{ src: `${browserExtensionOutDir}/popup/index.js`, dst: 'js/popup.js' },
{ src: `${browserExtensionOutDir}/popup/index.css`, dst: 'css/popup.css' },
{ src: 'public/popup.html', dst: 'popup.html' },
{ src: 'public/icon.png', dst: 'icon.png' },
]
// chromium
await copyFiles(
[...commonFiles, { src: 'public/manifest.json', dst: 'manifest.json' }],
`./${browserExtensionOutDir}/chromium`
)
await zipFolder(`./${browserExtensionOutDir}/chromium`)
// firefox
await copyFiles(
[...commonFiles, { src: 'public/manifest.firefox.json', dst: 'manifest.json' }],
`./${browserExtensionOutDir}/firefox`
)
await zipFolder(`./${browserExtensionOutDir}/firefox`)
// userscript
await copyFiles(
[
{ src: `${browserExtensionOutDir}/content_script/index.js`, dst: 'index.js' }
],
`./${userscriptOutDir}`
)
console.log('Build success.')
}
build()