SurfSense/surfsense_browser_extension/scripts/fix-build-paths.js
API Test Bot 23cc09b1a7 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
2026-02-04 10:18:20 +07:00

63 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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();