-
Notifications
You must be signed in to change notification settings - Fork 26
/
build.js
61 lines (58 loc) · 1.83 KB
/
build.js
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
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const fs = require("fs");
const axios = require("axios");
const fsp = require('fs').promises;
const path = require('path');
async function walk(dir) {
let files = await fsp.readdir(dir);
files = await Promise.all(files.map(async file => {
const filePath = path.join(dir, file);
const stats = await fsp.stat(filePath);
if (stats.isDirectory()) return walk(filePath);
else if(stats.isFile()) return filePath;
}));
return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
async function build(){
let bunker = process.argv[2];
let bundled = process.argv[3];
let files = await walk(__dirname+"/"+bunker);
for(let i in files){
files[i] = files[i].split(__dirname+"/"+bunker)[1];
}
console.log(files);
for(let i in files){
if(files[i].split(".")[files[i].split(".").length-1] != "html"){
continue;
}
let ht = fs.readFileSync(__dirname+"/"+bunker+files[i]);
const dom = new JSDOM(ht);
let document = dom.window.document;
let window = dom.window;
//script inlining
let scrs = document.getElementsByTagName("script");
for(let i=0;i<scrs.length;i++){
let content;
if(scrs[i].src.indexOf("http") != 0){
let src = __dirname+"/"+bunker+"/"+scrs[i].src;
content = fs.readFileSync(src, "utf-8");
} else {
content = (await axios.get(scrs[i].src)).data;
}
scrs[i].removeAttribute("src");
scrs[i].innerHTML = content;
}
delete scrs;
//navigation virtualization
let as = document.getElementsByTagName("a");
for(let i=0;i<as.length;i++){
if(as[i].href.indexOf("http") != 0){
as[i].setAttribute("onclick", `document.write(window.navigation['${as[i].href}']);`);
as[i].removeAttribute("href");
}
}
fs.writeFileSync(__dirname+"/"+bundled+files[i], dom.serialize());
}
}
build();