-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
230 lines (215 loc) · 7.08 KB
/
Copy pathvite.config.ts
File metadata and controls
230 lines (215 loc) · 7.08 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { defineConfig, build, type Plugin } from "vite";
import { resolve } from "path";
import { cpSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { execSync } from "child_process";
const isFirefox = process.env.VITE_BROWSER === "firefox";
const isDevBuild = process.env.TRULY_DEV_BUILD === "1";
// Build ID: epoch-ms + 7-char git SHA (+ "-dirty" if uncommitted changes).
// Stamped into every entry point via `define` so we can detect at runtime
// when Chrome's MV3 SW cache is serving stale code from a previous build.
function computeBuildId(): string {
const ts = Date.now();
let sha = "nogit";
let dirty = "";
try {
sha = execSync("git rev-parse --short=7 HEAD", { stdio: ["ignore", "pipe", "ignore"] })
.toString().trim();
const status = execSync("git status --porcelain", { stdio: ["ignore", "pipe", "ignore"] })
.toString().trim();
if (status.length > 0) dirty = "-dirty";
} catch { /* not a git repo */ }
return `${ts}-${sha}${dirty}`;
}
let activeBuildId = computeBuildId();
function buildIdReplacePlugin(opts: {
getBuildId: () => string;
refreshOnBuildStart?: boolean;
}): Plugin {
return {
name: "truly-build-id-replace",
buildStart() {
if (opts.refreshOnBuildStart) {
activeBuildId = computeBuildId();
console.log(`[vite] BUILD_ID=${activeBuildId}`);
}
},
renderChunk(code) {
return {
code: code.replace(/\b__TRULY_BUILD_ID__\b/g, JSON.stringify(opts.getBuildId())),
map: null,
};
},
};
}
function copyStaticAssets(): Plugin {
return {
name: "copy-static-assets",
closeBundle() {
const dist = resolve(__dirname, "dist");
const src = resolve(__dirname, "src");
writeManifest(`${src}/manifest.json`, `${dist}/manifest.json`);
cpSync(`${src}/popup/popup.html`, `${dist}/popup/popup.html`);
cpSync(`${src}/options/options.html`, `${dist}/options/options.html`);
cpSync(
`${src}/content_scripts/feed-filter.css`,
`${dist}/content_scripts/feed-filter.css`
);
cpSync(`${src}/_locales`, `${dist}/_locales`, { recursive: true });
mkdirSync(`${dist}/sidepanel`, { recursive: true });
cpSync(`${src}/sidepanel/sidepanel.html`, `${dist}/sidepanel/sidepanel.html`);
mkdirSync(`${dist}/icons`, { recursive: true });
try {
cpSync(`${src}/icons`, `${dist}/icons`, { recursive: true });
} catch {}
},
};
}
function writeManifest(sourcePath: string, targetPath: string) {
const manifest = JSON.parse(readFileSync(sourcePath, "utf8"));
if (isDevBuild) {
manifest.commands = {
...(manifest.commands ?? {}),
"reload-extension": {
suggested_key: {
default: "Alt+Shift+R",
mac: "Alt+Shift+R",
},
description: "Reload Truly during local development",
},
};
} else if (manifest.commands?.["reload-extension"]) {
delete manifest.commands["reload-extension"];
if (Object.keys(manifest.commands).length === 0) delete manifest.commands;
}
writeFileSync(targetPath, `${JSON.stringify(manifest, null, 2)}\n`);
}
function syncHeadsUpStyles(): Plugin {
return {
name: "sync-heads-up-styles",
buildStart() {
execSync("node scripts/sync-headsup-styles.mjs", { stdio: "inherit" });
},
};
}
// Build content script separately as IIFE (self-contained, no imports)
function buildContentScriptIIFE(): Plugin {
return {
name: "build-content-script-iife",
async closeBundle() {
const buildId = activeBuildId;
const buildIdPlugin = buildIdReplacePlugin({ getBuildId: () => buildId });
// Build content script (IIFE)
await build({
configFile: false,
plugins: [buildIdPlugin],
build: {
outDir: "dist/content_scripts",
emptyOutDir: false,
sourcemap: true,
lib: {
entry: resolve(__dirname, "src/content_scripts/feed-filter.ts"),
formats: ["iife"],
name: "Truly",
fileName: () => "feed-filter.js",
},
rollupOptions: {
output: {
inlineDynamicImports: true,
},
},
},
define: {
__BROWSER__: JSON.stringify(isFirefox ? "firefox" : "chrome"),
__TRULY_DEV_BUILD__: JSON.stringify(isDevBuild),
},
});
// Build GraphQL interceptor (IIFE, injected into MAIN world)
await build({
configFile: false,
plugins: [buildIdPlugin],
build: {
outDir: "dist/content_scripts",
emptyOutDir: false,
sourcemap: false,
lib: {
entry: resolve(
__dirname,
"src/content_scripts/graphql-interceptor.ts"
),
formats: ["iife"],
name: "TrulyInterceptor",
fileName: () => "graphql-interceptor.js",
},
},
define: {
__BROWSER__: JSON.stringify(isFirefox ? "firefox" : "chrome"),
__TRULY_DEV_BUILD__: JSON.stringify(isDevBuild),
},
});
// Build service worker (IIFE for non-module SW)
await build({
configFile: false,
plugins: [buildIdPlugin],
build: {
outDir: "dist/background",
emptyOutDir: false,
sourcemap: true,
lib: {
entry: resolve(__dirname, "src/background/service-worker.ts"),
formats: ["iife"],
name: "TrulyBackground",
fileName: () => "service-worker.js",
},
},
define: {
__BROWSER__: JSON.stringify(isFirefox ? "firefox" : "chrome"),
__TRULY_DEV_BUILD__: JSON.stringify(isDevBuild),
},
});
},
};
}
function writeBuildIdFile(): Plugin {
return {
name: "write-build-id-file",
closeBundle() {
const dist = resolve(__dirname, "dist");
// Write after all root and nested extension bundles are complete.
// dev-reload-server polls this file as the canonical reload signal.
writeFileSync(`${dist}/build-id.txt`, activeBuildId);
},
};
}
export default defineConfig({
build: {
outDir: "dist",
// Chrome loads the unpacked extension directly from `dist/` in dev.
// Emptying the directory at build start creates a brief missing-file
// window where Chrome can disable the extension during reload.
emptyOutDir: false,
sourcemap: true,
rollupOptions: {
input: {
"popup/popup": resolve(__dirname, "src/popup/popup.ts"),
"options/options": resolve(__dirname, "src/options/options.ts"),
"sidepanel/sidepanel": resolve(__dirname, "src/sidepanel/sidepanel.ts"),
},
output: {
entryFileNames: "[name].js",
chunkFileNames: "chunks/[name].js",
format: "es",
},
},
},
plugins: [
buildIdReplacePlugin({ getBuildId: () => activeBuildId, refreshOnBuildStart: true }),
syncHeadsUpStyles(),
copyStaticAssets(),
buildContentScriptIIFE(),
writeBuildIdFile(),
],
define: {
__BROWSER__: JSON.stringify(isFirefox ? "firefox" : "chrome"),
__TRULY_DEV_BUILD__: JSON.stringify(isDevBuild),
},
});