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
This commit is contained in:
API Test Bot 2026-02-04 10:18:20 +07:00
parent 70226171d8
commit 23cc09b1a7
2 changed files with 65 additions and 1 deletions

View file

@ -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();