-
Notifications
You must be signed in to change notification settings - Fork 24
/
build.js
40 lines (33 loc) · 1.2 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
const fs = require('fs');
const { Snapshot } = require('snapshot-url');
const OUT_DIR = './dist'; // output dir, relative to this file
const HTML_OUTFILE = './dist/index.html'; // output html file, pretty-printed + cleaned
if (!fs.existsSync(OUT_DIR)) {
fs.mkdirSync(OUT_DIR);
}
fs.copyFileSync(`./public/favicon.ico`, `${OUT_DIR}/favicon.ico`);
(async () => {
// hack: this depends on the dev server running
const url = 'http://localhost:8080';
const snapshot = new Snapshot(url);
const dom = await snapshot.renderDOM();
// remove some html tags that we don't need
const DELETE_NODES = 'script, link[rel=preload]';
dom.window.document.
querySelectorAll(DELETE_NODES).
forEach((scriptEl) => scriptEl.parentNode.removeChild(scriptEl));
const finalHtml = snapshot.getHtml({ prettyPrint: true });
// write the final html to a file
fs.open(HTML_OUTFILE, 'w', (err) => {
if (err) {
throw `Couldn't open file: ${HTML_OUTFILE}`;
}
fs.writeFile(HTML_OUTFILE, finalHtml, async (err) => {
if (err) {
console.error(err);
} else {
console.log(`Saved static html to dist/index.html (size: ${finalHtml.length})`);
}
});
});
})();