Skip to content

Commit 3ea255e

Browse files
JOHNJOHN
authored andcommitted
Fix content script build and chrome.storage errors
- Build content script as IIFE format (not ES modules) using separate vite config - Add chrome.storage availability check in logger to prevent errors in content scripts - Update package.json build script to run both vite builds
1 parent 4a2be88 commit 3ea255e

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

graphiti-extension.zip

109 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite build --watch --mode development",
8-
"build": "tsc && vite build",
8+
"build": "tsc && vite build && vite build --config vite.content.config.ts",
99
"build:analyze": "tsc && vite build && npm run analyze",
1010
"analyze": "npx vite-bundle-visualizer --open",
1111
"preview": "vite preview",

src/utils/logger.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@ class Logger {
4848
private static instance: Logger;
4949
private logBuffer: LogEntry[] = [];
5050
private maxBufferSize = 1000;
51+
private isStorageAvailable = false;
5152

5253
private constructor() {
53-
this.loadLogs();
54+
// Check if chrome.storage is available (not in content scripts)
55+
this.isStorageAvailable = typeof chrome !== 'undefined' &&
56+
typeof chrome.storage !== 'undefined' &&
57+
typeof chrome.storage.local !== 'undefined';
58+
if (this.isStorageAvailable) {
59+
this.loadLogs();
60+
}
5461
}
5562

5663
static getInstance(): Logger {
@@ -61,6 +68,7 @@ class Logger {
6168
}
6269

6370
private async loadLogs() {
71+
if (!this.isStorageAvailable) return;
6472
try {
6573
const result = await chrome.storage.local.get('debugLogs');
6674
if (result.debugLogs) {
@@ -72,6 +80,7 @@ class Logger {
7280
}
7381

7482
private async saveLogs() {
83+
if (!this.isStorageAvailable) return;
7584
try {
7685
await chrome.storage.local.set({ debugLogs: this.logBuffer });
7786
} catch (error) {
@@ -148,7 +157,9 @@ class Logger {
148157

149158
async clearLogs() {
150159
this.logBuffer = [];
151-
await chrome.storage.local.remove('debugLogs');
160+
if (this.isStorageAvailable) {
161+
await chrome.storage.local.remove('debugLogs');
162+
}
152163
this.info('Logger', 'Logs cleared');
153164
}
154165

vite.config.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,30 @@ export default defineConfig({
4040
build: {
4141
outDir: 'dist',
4242
sourcemap: true,
43-
minify: 'esbuild', // Use esbuild for faster minification
44-
target: 'es2020', // Target modern browsers
43+
minify: 'esbuild',
44+
target: 'es2020',
4545
rollupOptions: {
4646
input: {
4747
popup: resolve(__dirname, 'popup.html'),
4848
sidepanel: resolve(__dirname, 'sidepanel.html'),
4949
'profile-renderer': resolve(__dirname, 'src/profile/profile-renderer.html'),
5050
offscreen: resolve(__dirname, 'src/offscreen/offscreen.html'),
5151
background: resolve(__dirname, 'src/background/background.ts'),
52-
content: resolve(__dirname, 'src/content/content.ts'),
5352
},
5453
output: {
5554
entryFileNames: (chunkInfo) => {
5655
if (chunkInfo.name === 'background') {
5756
return 'background.js';
5857
}
59-
if (chunkInfo.name === 'content') {
60-
return 'content.js';
61-
}
6258
if (chunkInfo.name === 'offscreen') {
6359
return 'src/offscreen/offscreen.js';
6460
}
6561
return 'assets/[name]-[hash].js';
6662
},
6763
chunkFileNames: 'assets/[name]-[hash].js',
6864
assetFileNames: 'assets/[name]-[hash].[ext]',
69-
format: 'es', // Default format for most files
65+
format: 'es',
7066
manualChunks: (id) => {
71-
// Vendor chunks for better caching
7267
if (id.includes('node_modules')) {
7368
if (id.includes('react') || id.includes('react-dom')) {
7469
return 'vendor-react';

vite.content.config.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineConfig } from 'vite';
2+
import { resolve } from 'path';
3+
4+
// Separate config for content script - must be IIFE format for Chrome extension
5+
export default defineConfig({
6+
build: {
7+
outDir: 'dist',
8+
sourcemap: true,
9+
minify: 'esbuild',
10+
target: 'es2020',
11+
emptyOutDir: false, // Don't clear the dist folder (main build already created it)
12+
lib: {
13+
entry: resolve(__dirname, 'src/content/content.ts'),
14+
name: 'GraphitiContent',
15+
formats: ['iife'],
16+
fileName: () => 'content.js',
17+
},
18+
rollupOptions: {
19+
output: {
20+
// Ensure all code is inlined (no external imports)
21+
inlineDynamicImports: true,
22+
// Make sure globals don't conflict
23+
extend: true,
24+
},
25+
},
26+
},
27+
resolve: {
28+
alias: {
29+
'@': resolve(__dirname, './src'),
30+
},
31+
},
32+
});

0 commit comments

Comments
 (0)