-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathfix-asset-paths.js
More file actions
executable file
·70 lines (57 loc) · 2.15 KB
/
fix-asset-paths.js
File metadata and controls
executable file
·70 lines (57 loc) · 2.15 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
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const buildDir = path.join(__dirname, "build", "ui");
const indexPath = path.join(buildDir, "index.html");
if (!fs.existsSync(indexPath)) {
console.error("Build index.html not found at:", indexPath);
process.exit(1);
}
let html = fs.readFileSync(indexPath, "utf-8");
// 1. Patterns to find Vite's original tags
const scriptPattern = /<script type="module"[^>]*src="\.\/assets\/([^"]+)"[^>]*><\/script>/g;
const linkPattern = /<link rel="stylesheet"[^>]*href="\.\/assets\/([^"]+)"[^>]*>/g;
const scriptAssets = [];
const linkAssets = [];
// 2. Extract and remove original tags
html = html.replace(scriptPattern, (match, filename) => {
scriptAssets.push(filename);
return "";
});
html = html.replace(linkPattern, (match, filename) => {
linkAssets.push(filename);
return "";
});
// 3. Inject into the existing IIFE
if (scriptAssets.length > 0 || linkAssets.length > 0) {
const dynamicAssetsCode = [
...scriptAssets.map((filename) => {
const varName = `script_${filename.replace(/[^a-zA-Z0-9]/g, "_")}`;
return `
const ${varName} = document.createElement('script');
${varName}.type = 'module';
${varName}.crossOrigin = '';
${varName}.src = window.ROOT_PATH + '/ui/assets/${filename}';
document.head.appendChild(${varName});`;
}),
...linkAssets.map((filename) => {
const varName = `link_${filename.replace(/[^a-zA-Z0-9]/g, "_")}`;
return `
const ${varName} = document.createElement('link');
${varName}.rel = 'stylesheet';
${varName}.crossOrigin = '';
${varName}.href = window.ROOT_PATH + '/ui/assets/${filename}';
document.head.appendChild(${varName});`;
}),
].join("\n");
/**
* We look for the end of the existing IIFE (the last "})();")
* and insert our code just before it.
*/
html = html.replace("})();", `${dynamicAssetsCode}\n })();`);
}
fs.writeFileSync(indexPath, html, "utf-8");
console.log("Fixed asset paths in build/ui/index.html");