-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-vite-plugins.ts
More file actions
66 lines (61 loc) · 1.76 KB
/
custom-vite-plugins.ts
File metadata and controls
66 lines (61 loc) · 1.76 KB
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
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: { dir?: string }, _inputOptions: unknown) {
const outDir = outputOptions.dir;
if (!outDir) return;
fs.rm(resolve(outDir, 'icons-dev/icon32.png'), () =>
console.log(`Deleted icon32.png from prod build`)
);
fs.rm(resolve(outDir, 'icons-dev/icon128.png'), () =>
console.log(`Deleted icon128.png from prod build`)
);
},
};
}
// plugin to support i18n
// eslint-disable-next-line sonarjs/function-return-type
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;
});
},
},
};
}