-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-asset-paths.js
More file actions
57 lines (44 loc) · 1.59 KB
/
Copy pathfix-asset-paths.js
File metadata and controls
57 lines (44 loc) · 1.59 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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Get current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Function to recursively find HTML files
function findHtmlFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findHtmlFiles(filePath, fileList);
} else if (path.extname(file) === '.html') {
fileList.push(filePath);
}
});
return fileList;
}
// Function to fix asset paths in HTML files
function fixAssetPaths(htmlFile) {
let content = fs.readFileSync(htmlFile, 'utf8');
// Fix script src paths
content = content.replace(/src="\/_next\//g, 'src="/_next/');
// Fix link href paths
content = content.replace(/href="\/_next\//g, 'href="/_next/');
fs.writeFileSync(htmlFile, content, 'utf8');
console.log(`Fixed asset paths in ${htmlFile}`);
}
// Copy _redirects file to the out directory
if (fs.existsSync('public/_redirects')) {
fs.copyFileSync('public/_redirects', 'out/_redirects');
console.log('Copied _redirects file to out directory');
}
// Copy 404.html to the out directory
if (fs.existsSync('public/404.html')) {
fs.copyFileSync('public/404.html', 'out/404.html');
console.log('Copied 404.html file to out directory');
}
// Find and fix all HTML files
const htmlFiles = findHtmlFiles('out');
htmlFiles.forEach(fixAssetPaths);
console.log(`Fixed asset paths in ${htmlFiles.length} HTML files`);