-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprerender.js
88 lines (70 loc) · 2.54 KB
/
prerender.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
76
77
78
79
80
81
82
83
84
85
86
87
88
import fse from "fs-extra";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const dir = dirname(fileURLToPath(import.meta.url));
const toAbsolute = (p) => resolve(dir, p);
const transformedTemplate = fse.readFileSync(
resolve(dir, "dist/static/index.html"),
"utf-8"
);
const list = ["math-in-mdx.mdx"];
const doWork = async (url) => {
const { Renderer } = await import("./dist/server/entry-server.js");
// for (const url of test) {
// let shouldBeStatic = list.includes(url);
// if (shouldBeStatic) {
// let staticHtmlPath = "src/nojs/index.html";
// transformedTemplate = fse.readFileSync(
// resolve(dir, staticHtmlPath),
// "utf-8"
// );
// }
const renderer = new Renderer(transformedTemplate);
// const localFilePath = url
// .replace(/^\.\/pages/, "/")
// .replace(/(\.jsx|\.mdx)$/, "");
const localFilePath = url
.replace(/src\/pages/, "")
.replace(/pages\//, "")
.replace(/(\.jsx|\.mdx)$/, "");
// const urlPath = localFilePath.includes("index")
// ? localFilePath.replace(/index$/, "")
// : `/${localFilePath}/`;
const urlPath = localFilePath.includes("index") ? "/" : `/${localFilePath}/`;
const { body } = renderer.render(urlPath);
const fullFilePath = `dist/static${localFilePath}.html`;
fse.outputFileSync(resolve(dir, fullFilePath), body);
console.log("🖨 Prerendered", fullFilePath);
};
async function prerender() {
try {
//let allFiles = fse.readdirSync(toAbsolute("src/pages")).map((file) => file);
const pages = ["src/pages/index.mdx", "src/pages/404.jsx"];
pages.forEach(async (page) => {
await doWork(page);
});
console.log("🦖 Your static site is ready to deploy from dist/static");
} catch (e) {
console.error(e);
}
}
// determine routes to pre-render from src/pages
// const routesToPrerender = fs
// .readdirSync(toAbsolute('src/pages'))
// .map((file) => {
// const name = file.replace(/\.vue$/, '').toLowerCase()
// return name === 'home' ? `/` : `/${name}`
// })
// (async () => {
// // pre-render each route...
// for (const url of routesToPrerender) {
// const [appHtml, preloadLinks] = await render(url, manifest)
// const html = template
// .replace(`<!--preload-links-->`, preloadLinks)
// .replace(`<!--app-html-->`, appHtml)
// const filePath = `dist/static${url === '/' ? '/index' : url}.html`
// fs.writeFileSync(toAbsolute(filePath), html)
// console.log('pre-rendered:', filePath)
// }
// })()
prerender();