-
-
Notifications
You must be signed in to change notification settings - Fork 640
Expand file tree
/
Copy pathvite.config.safari.ts
More file actions
158 lines (149 loc) · 5.21 KB
/
Copy pathvite.config.safari.ts
File metadata and controls
158 lines (149 loc) · 5.21 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { ManifestV3Export, crx } from '@crxjs/vite-plugin';
import { build as buildWithEsbuild } from 'esbuild';
import { readFile, rename } from 'node:fs/promises';
import { resolve } from 'path';
import { Plugin, defineConfig, mergeConfig } from 'vite';
import manifest from './manifest.json';
import baseConfig, { baseBuildOptions, baseManifest } from './vite.config.base';
const outDir = resolve(__dirname, 'dist_safari');
function safariClassicBackground(): Plugin {
return {
name: 'safari-classic-background',
apply: 'build',
enforce: 'post',
async closeBundle() {
const manifestPath = resolve(outDir, 'manifest.json');
const builtManifest = JSON.parse(await readFile(manifestPath, 'utf8')) as {
background?: { scripts?: string[] };
};
const backgroundScript = builtManifest.background?.scripts?.[0];
if (!backgroundScript) throw new Error('Safari background script was not emitted');
const entryPath = resolve(outDir, backgroundScript);
const bundledPath = `${entryPath}.classic.js`;
await buildWithEsbuild({
entryPoints: [entryPath],
outfile: bundledPath,
bundle: true,
format: 'iife',
platform: 'browser',
target: 'safari14',
minify: true,
});
await rename(bundledPath, entryPath);
},
};
}
// Direct-download Safari builds do not have a browser-store updater. Keep the
// existing release reminder on by default; packagers can explicitly disable it.
const enableSafariUpdateCheck = process.env.ENABLE_SAFARI_UPDATE_CHECK !== 'false';
const safariMainWorldScripts = [
{
matches: ['https://gemini.google.com/*', 'https://business.gemini.google/*'],
js: ['public/fetchInterceptor.js'],
},
{
matches: ['https://gemini.google.com/*', 'https://business.gemini.google/*'],
js: ['public/usage-observer.js'],
},
{
matches: ['https://gemini.google.com/*', 'https://business.gemini.google/*'],
js: ['public/conversation-history-observer.js'],
},
{
matches: ['https://gemini.google.com/*', 'https://business.gemini.google/*'],
js: ['public/response-complete-observer.js'],
},
{
matches: ['https://gemini.google.com/*', 'https://business.gemini.google/*'],
js: ['public/prevent-auto-scroll.js'],
},
{
matches: [
'https://gemini.google.com/*',
'https://business.gemini.google/*',
'https://aistudio.google.com/*',
'https://aistudio.google.cn/*',
],
js: ['public/katex-config.js'],
},
].map((entry) => ({ ...entry, run_at: 'document_start' as const, world: 'MAIN' as const }));
const sharedContentScripts = (
baseManifest as unknown as { content_scripts?: Array<Record<string, unknown>> }
).content_scripts;
export const safariManifest = {
...baseManifest,
browser_specific_settings: {
safari: {
// Manifest V3 support starts at Safari 15.4. The Vite target controls
// syntax only, so the compatibility floor must also live in manifest.
strict_min_version: '15.4',
},
},
// Safari renders toolbar icons as template images (monochrome).
// Use a transparent-background version so it doesn't appear as a solid square.
action: {
...manifest.action,
default_icon: {
'16': 'icon-16-template.png',
'19': 'icon-19-template.png',
'32': 'icon-32-template.png',
'38': 'icon-38-template.png',
},
},
permissions: Array.from(
new Set([
...manifest.permissions.filter(
(permission) => permission !== 'notifications' && permission !== 'identity',
),
'nativeMessaging',
]),
),
optional_permissions: Array.from(
new Set([
...(manifest.optional_permissions ?? []).filter(
(permission) => permission !== 'notifications',
),
'unlimitedStorage',
]),
),
// Safari applies the page CSP to DOM-injected extension scripts. Native
// MAIN-world content scripts bypass that CSP and run early enough to observe
// Gemini's initial network requests. Keep one file per entry because Safari
// can partially execute multi-file MAIN-world registrations.
content_scripts: [...(sharedContentScripts ?? []), ...safariMainWorldScripts],
// Safari App Extensions load `background.scripts` as classic scripts. The
// post-build plugin below folds CRXJS's shared chunks into this one entry.
background: {
scripts: ['src/pages/background/index.ts'],
},
} as unknown as ManifestV3Export;
export default mergeConfig(
baseConfig,
defineConfig({
define: {
// Inject flag into the build
'import.meta.env.ENABLE_SAFARI_UPDATE_CHECK': JSON.stringify(
enableSafariUpdateCheck ? 'true' : 'false',
),
'import.meta.env.VOYAGER_BUILD_TARGET': JSON.stringify('safari'),
},
plugins: [
crx({
manifest: safariManifest,
browser: 'chrome', // Use 'chrome' mode as Safari uses WebKit
contentScripts: {
injectCss: true,
},
}),
safariClassicBackground(),
],
build: {
...baseBuildOptions,
outDir,
// Safari-specific build optimizations
// JavaScript syntax floor only. It does not imply Safari 14 can run MV3;
// runtime quota messaging uses the actual Safari product version.
target: 'safari14',
},
}),
);