-
Notifications
You must be signed in to change notification settings - Fork 4
/
replaceHtml.mjs
50 lines (44 loc) · 1.88 KB
/
replaceHtml.mjs
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
import { writeFileSync, readFileSync } from "fs";
import glob from "glob";
const replaceInHtmlFiles = () => {
try {
const files = glob.sync("dist/**/*.html");
console.log(files);
for (const file of files) {
// htmlファイルの読み込み
const data = readFileSync(file, "utf8");
// htmlの置かれているパスから相対(., ..)を算出
let relativePath = file.replace(/[^/]/g, "").replace(/\//g, ".");
if (relativePath.length === 1) {
relativePath = file.replace(/[^/]/g, "").replace(/\//g, ".");
} else {
relativePath = file.replace(/[^/]/g, "").replace(/\//g, "../");
relativePath = relativePath.slice(0, -1);
// 3 ... | ../../ → ../..
// 4 .... | ../../../ → ../../..
// 5 ..... | ../../../../ → ../../../..
}
// href, srcに指定されている絶対パスを置換
const result = data
.replace(/href="\//g, `href="${relativePath}/`)
.replace(/href='\//g, `href='${relativePath}/`)
.replace(/src="\//g, `src="${relativePath}/`)
.replace(/src='\//g, `src='${relativePath}/`)
.replace(/srcset="\//g, `srcset="${relativePath}/`)
.replace(/srcset='\//g, `srcset='${relativePath}/`)
.replace(/action="\//g, `action="${relativePath}/`)
.replace(/action='\//g, `action='${relativePath}/`)
.replace(/content="\//g, `content="${relativePath}/`)
.replace(/content='\//g, `content='${relativePath}/`);
writeFileSync(file, result, "utf8");
// console.log(file);
}
console.log(`\n// --------------------------\n\n👌 ~ replaceInHtmlFiles\n\n// --------------------------\n`);
} catch (error) {
console.log(
`\n// -------------------------- \n\n🙅♀️ ~ replaceInHtmlFiles\n\n// --------------------------\n${error}\n`,
);
}
};
replaceInHtmlFiles();
// node replaceHtml.mjs