-
Notifications
You must be signed in to change notification settings - Fork 1
/
postbuild.js
76 lines (61 loc) · 2.04 KB
/
postbuild.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const start = new Date();
const fs = require("fs");
const path = require("path");
const csso = require("csso");
const subsetFont = require("subset-font");
const { JSDOM } = require("jsdom");
const getAllFiles = function (dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath);
arrayOfFiles ||= [];
files.forEach((file) => {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
} else {
const filepath = path.join(dirPath, "/", file);
path.parse(filepath).ext === ".html" && arrayOfFiles.push(filepath);
}
});
return arrayOfFiles;
};
const allFiles = getAllFiles("./dist");
console.log(allFiles.length + " HTML files found");
let chars = "!";
allFiles.forEach((file) => {
const content = fs.readFileSync(file, "utf-8");
const { document, location } = new JSDOM(content).window;
const styleElement = document.querySelector("style");
document.querySelectorAll("em, .pacific").forEach((node) => {
chars += node.textContent;
});
const { css } = csso.minify(styleElement.textContent, {
restructure: true,
forceMediaMerge: true,
comments: "exclamation",
});
styleElement.textContent = css;
if (
["/update-ubuntu-terminal", "/install-usb-modem-ubuntu"].includes(
location.pathname
)
) {
Array.from({ length: 29 }).forEach(() => {
document.head.appendChild(document.body.firstElementChild);
});
}
if (["/node-fetch"].includes(location.pathname)) {
Array.from({ length: 28 }).forEach(() => {
document.head.appendChild(document.body.firstElementChild);
});
}
if (["/terms", "/privacy-policy"].includes(location.pathname)) {
document.querySelector("#root").appendChild(document.body.lastElementChild);
Array.from({ length: 21 }).forEach(() => {
document.head.appendChild(document.body.firstElementChild);
});
}
fs.writeFileSync(
file,
"<!DOCTYPE html>" + document.documentElement.outerHTML
);
});
console.log("Finished PostBuild in", new Date() - start, "ms");