-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
79 lines (69 loc) · 1.99 KB
/
build.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
74
75
76
77
78
79
// @ts-check
const fs = require('fs-extra')
const sass = require('sass')
const esbuild = require('esbuild')
const archiver = require('archiver')
const inputDir = 'src'
const outputDir = 'dist/extension'
/**
* @param {{ path: string; outfile: string; }} options
*/
const zip = (options) => {
const output = fs.createWriteStream(options.outfile)
const archive = archiver('zip', {
zlib: { level: 9 },
})
archive.pipe(output)
archive.directory(options.path, false)
archive.finalize()
}
const build = async () => {
if (fs.existsSync(outputDir)) {
fs.removeSync(outputDir)
}
fs.mkdirSync(outputDir, { recursive: true })
fs.copySync(`${inputDir}/manifest.json`, `${outputDir}/manifest.json`)
fs.copySync(`${inputDir}/assets`, `${outputDir}/assets`)
fs.copySync(`${inputDir}/styles`, `${outputDir}/styles`)
fs.copySync(`${inputDir}/popup/index.html`, `${outputDir}/popup/index.html`)
fs.copySync(
`${inputDir}/side_panel/index.html`,
`${outputDir}/side_panel/index.html`
)
// CSS
const cssPaths = [
`${inputDir}/popup/style/index.scss`,
`${inputDir}/side_panel/style/index.scss`,
]
for (const path of cssPaths) {
const { css } = sass.compile(path)
const filePath = path
.replace(/^src(?=\/)/, outputDir)
.replace(/(?<=.+)\/index(?=\.[a-z]+$)/i, '')
.replace(/(?<=\.)(sass|scss)$/, 'css')
fs.writeFileSync(filePath, css)
}
// JavaScript
const jsPaths = [
`${inputDir}/content_script/index.ts`,
`${inputDir}/background/index.ts`,
`${inputDir}/popup/script/index.ts`,
`${inputDir}/side_panel/script/index.ts`,
]
const { outputFiles } = await esbuild.build({
entryPoints: jsPaths,
outdir: outputDir,
bundle: true,
write: false,
})
for (const file of outputFiles) {
const filePath = file.path.replace(/(?<=.+)\/index(?=\.[a-z]+$)/i, '')
fs.createFileSync(filePath)
fs.writeFileSync(filePath, file.text)
}
zip({
path: outputDir,
outfile: `${outputDir}.zip`,
})
}
build()