-
Notifications
You must be signed in to change notification settings - Fork 85
/
custom-vite-plugins.ts
56 lines (52 loc) · 1.61 KB
/
custom-vite-plugins.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
import fs from 'fs';
import { resolve } from 'path';
import type { PluginOption } from 'vite';
// plugin to remove dev icons from prod build
export function stripDevIcons (isDev: boolean) {
if (isDev) return null
return {
name: 'strip-dev-icons',
resolveId (source: string) {
return source === 'virtual-module' ? source : null
},
renderStart (outputOptions: any, inputOptions: any) {
const outDir = outputOptions.dir
fs.rm(resolve(outDir, 'dev-icon-32.png'), () => console.log(`Deleted dev-icon-32.png from prod build`))
fs.rm(resolve(outDir, 'dev-icon-128.png'), () => console.log(`Deleted dev-icon-128.png from prod build`))
}
}
}
// plugin to support i18n
export function crxI18n (options: { localize: boolean, src: string }): PluginOption {
if (!options.localize) return null
const getJsonFiles = (dir: string): Array<string> => {
const files = fs.readdirSync(dir, {recursive: true}) as string[]
return files.filter(file => !!file && file.endsWith('.json'))
}
const entry = resolve(__dirname, options.src)
const localeFiles = getJsonFiles(entry)
const files = localeFiles.map(file => {
return {
id: '',
fileName: file,
source: fs.readFileSync(resolve(entry, file))
}
})
return {
name: 'crx-i18n',
enforce: 'pre',
buildStart: {
order: 'post',
handler() {
files.forEach((file) => {
const refId = this.emitFile({
type: 'asset',
source: file.source,
fileName: '_locales/'+file.fileName
})
file.id = refId
})
}
}
}
}