将源码解析,生成路由对应的 html 页面
const path = require("path");
const fs = require("fs");
const template = fs.readFileSync("dist/client/index.html", "utf-8");
const { render } = require("./dist/server/server-entry");
const routesToRender = fs.readdirSync("src/pages").map((file) => {
return file.replace(".jsx", "").toLowerCase();
});
for (const route of routesToRender) {
const context = {};
// 渲染路由对应的 html
const html = render(route, context);
// 替换字符串为 html 模版
const responseHtml = template.replace("<!-- APP_HTML -->", html);
const filePath = `dist/static/${route}.html`;
fs.writeFileSync(filePath, responseHtml);
console.log(`prerender ${filePath}`);
}