forked from Nagi-ovo/gemini-voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-vite-plugins.ts
More file actions
118 lines (106 loc) · 3.86 KB
/
custom-vite-plugins.ts
File metadata and controls
118 lines (106 loc) · 3.86 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import fs from 'fs';
import { resolve } from 'path';
import type { NormalizedInputOptions, NormalizedOutputOptions } from 'rollup';
import type { PluginOption } from 'vite';
const FIREFOX_OUT_DIR_MARKER = 'dist_firefox';
const CHANGELOG_PROMO_BANNERS = [
'changelog-promo-banner.png',
'changelog-promo-banner-cn.png',
'changelog-promo-banner-jp.png',
];
// 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: NormalizedOutputOptions, _inputOptions: NormalizedInputOptions) {
const outDir = outputOptions.dir ?? '';
const isFirefoxBuild = outDir.includes(FIREFOX_OUT_DIR_MARKER);
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`),
);
if (!isFirefoxBuild) {
CHANGELOG_PROMO_BANNERS.forEach((fileName) => {
fs.rm(resolve(outDir, fileName), () =>
console.log(`Deleted ${fileName} from non-Firefox build`),
);
});
}
// Remove assets directory if it exists
const assetsDir = resolve(outDir, 'assets');
fs.rm(assetsDir, { recursive: true, force: true }, () =>
console.log(`Deleted assets/ directory from prod build`),
);
},
writeBundle(outputOptions: NormalizedOutputOptions) {
const outDir = outputOptions.dir ?? '';
// Remove .vite directory (Vite's internal manifest, not needed for extension)
const viteDir = resolve(outDir, '.vite');
fs.rm(viteDir, { recursive: true, force: true }, () =>
console.log(`Deleted .vite/ directory from prod build`),
);
},
};
}
type LocaleMessages = Record<string, { message: string; description?: string }>;
function stripDescriptions(raw: LocaleMessages): LocaleMessages {
return Object.fromEntries(Object.entries(raw).map(([k, v]) => [k, { message: v.message }]));
}
// plugin to strip `description` fields from locale JSON at build time.
// Runs before vite:json so we return stripped JSON; vite:json then converts it to ESM normally.
export function stripI18nDescriptions(isDev: boolean): PluginOption {
if (isDev) return null;
return {
name: 'strip-i18n-descriptions',
enforce: 'pre',
transform(code, id) {
if (!id.includes('/locales/') || !id.endsWith('messages.json')) return null;
const raw: LocaleMessages = JSON.parse(code);
return { code: JSON.stringify(stripDescriptions(raw)), map: null };
},
};
}
// plugin to support i18n
export function crxI18n(options: {
localize: boolean;
src: string;
stripDescriptions?: boolean;
}): 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) => {
const raw: LocaleMessages = JSON.parse(fs.readFileSync(resolve(entry, file), 'utf-8'));
const source = options.stripDescriptions
? JSON.stringify(stripDescriptions(raw))
: JSON.stringify(raw);
return { id: '', fileName: file, source };
});
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;
});
},
},
};
}