From 23cc09b1a7f113ade5afe6db73d9ea7dfbcf6409 Mon Sep 17 00:00:00 2001 From: API Test Bot Date: Wed, 4 Feb 2026 10:18:20 +0700 Subject: [PATCH] fix(build): add post-build script to fix HTML paths for Chrome extension - Create scripts/fix-build-paths.js to convert absolute paths to relative - Update build script to run fix-build-paths.js after plasmo build - Add build:raw script for raw plasmo build without fix Problem: Plasmo generates HTML with absolute paths (href="/file.css") which causes ERR_FILE_NOT_FOUND in Chrome extensions. Solution: Post-build script replaces /path with ./path --- surfsense_browser_extension/package.json | 3 +- .../scripts/fix-build-paths.js | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 surfsense_browser_extension/scripts/fix-build-paths.js diff --git a/surfsense_browser_extension/package.json b/surfsense_browser_extension/package.json index bca1c35ba..d1f92a641 100644 --- a/surfsense_browser_extension/package.json +++ b/surfsense_browser_extension/package.json @@ -21,7 +21,8 @@ }, "scripts": { "dev": "plasmo dev", - "build": "plasmo build", + "build": "plasmo build && node scripts/fix-build-paths.js", + "build:raw": "plasmo build", "package": "plasmo package" }, "dependencies": { diff --git a/surfsense_browser_extension/scripts/fix-build-paths.js b/surfsense_browser_extension/scripts/fix-build-paths.js new file mode 100644 index 000000000..2576ad680 --- /dev/null +++ b/surfsense_browser_extension/scripts/fix-build-paths.js @@ -0,0 +1,63 @@ +#!/usr/bin/env node +/** + * Post-build script to fix absolute paths in Plasmo-generated HTML files. + * + * Problem: Plasmo generates HTML files with absolute paths (e.g., href="/file.css") + * which don't work in Chrome extensions (ERR_FILE_NOT_FOUND). + * + * Solution: Replace absolute paths with relative paths (e.g., href="./file.css") + */ + +const fs = require('fs'); +const path = require('path'); + +const BUILD_DIR = path.join(__dirname, '..', 'build', 'chrome-mv3-prod'); +const HTML_FILES = ['popup.html', 'sidepanel.html']; + +function fixPaths(filePath) { + if (!fs.existsSync(filePath)) { + console.log(`⚠️ File not found: ${filePath}`); + return false; + } + + let content = fs.readFileSync(filePath, 'utf8'); + const originalContent = content; + + // Replace absolute paths with relative paths + // href="/something" -> href="./something" + // src="/something" -> src="./something" + content = content.replace(/href="\/([^"]+)"/g, 'href="./$1"'); + content = content.replace(/src="\/([^"]+)"/g, 'src="./$1"'); + + if (content !== originalContent) { + fs.writeFileSync(filePath, content, 'utf8'); + console.log(`✅ Fixed paths in: ${path.basename(filePath)}`); + return true; + } else { + console.log(`ℹ️ No changes needed: ${path.basename(filePath)}`); + return false; + } +} + +function main() { + console.log('🔧 Fixing build paths for Chrome extension...\n'); + + if (!fs.existsSync(BUILD_DIR)) { + console.error(`❌ Build directory not found: ${BUILD_DIR}`); + console.error(' Run "pnpm build" first.'); + process.exit(1); + } + + let fixedCount = 0; + for (const htmlFile of HTML_FILES) { + const filePath = path.join(BUILD_DIR, htmlFile); + if (fixPaths(filePath)) { + fixedCount++; + } + } + + console.log(`\n✨ Done! Fixed ${fixedCount} file(s).`); +} + +main(); +