rasengan
Version:
The modern React Framework
23 lines (22 loc) • 1.49 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { TemplateLayout } from "../../entries/server/index.js";
import { renderToString } from "../node/rendering.js";
import { resolveBuildOptions } from "./index.js";
import { ManifestManager } from "./manifest.js";
import path from "node:path";
import fs from "node:fs/promises";
export const renderIndexHTML = async (template, options) => {
const { rootPath, config, enableIndexFallback } = options;
const buildOptions = resolveBuildOptions({});
const manifest = new ManifestManager(path.posix.join(buildOptions.buildDirectory, (config.ssr || config.prerender) ? buildOptions.clientPathDirectory : '', buildOptions.manifestPathDirectory, 'manifest.json'));
// Get assets tags
const assets = manifest.generateMetaTags(''); // TODO: Add the correct path
// Generate html from template
const html = renderToString(_jsx(TemplateLayout, { Template: template, assets: assets, isSpaMode: true }));
// Render the html into an index.html or spa-fallback.html file
await fs.writeFile(path.posix.join(rootPath, config.prerender ? buildOptions.staticDirectory : buildOptions.buildDirectory, enableIndexFallback ? "spa-fallback.html" : "index.html"), html, 'utf-8');
// Delete the dist/assets/template.js or dist/client/assets/template.js file
if (!config.prerender) {
await fs.rm(path.posix.join(rootPath, buildOptions.buildDirectory, buildOptions.assetPathDirectory, 'template.js'));
}
};