one
Version:
One is a new React Framework that makes Vite serve both native and web.
106 lines (101 loc) • 3.93 kB
JavaScript
import FSExtra from "fs-extra";
import { join } from "node:path";
import * as constants from "../constants";
import { getLoaderPath, getPreloadPath } from "../utils/cleanUrl";
import { toAbsolute } from "../utils/toAbsolute";
import { replaceLoader } from "../vite/replaceLoader";
const { readFile, outputFile } = FSExtra;
async function buildPage(serverEntry, path, relativeId, params, foundRoute, clientManifestEntry, staticDir, clientDir, builtMiddlewares, serverJsPath, preloads, allCSS) {
const render = await getRender(serverEntry), htmlPath = `${path.endsWith("/") ? `${removeTrailingSlash(path)}/index` : path}.html`, clientJsPath = join("dist/client", clientManifestEntry.file), htmlOutPath = toAbsolute(join(staticDir, htmlPath)), preloadPath = getPreloadPath(path);
let loaderPath = "", loaderData = {};
try {
await FSExtra.writeFile(
join(clientDir, preloadPath),
preloads.map((preload) => `import "${preload}"`).join(`
`)
);
const exported = await import(toAbsolute(serverJsPath));
if (exported.loader) {
loaderData = await exported.loader?.({ path, params }) ?? null;
const code = await readFile(clientJsPath, "utf-8"), withLoader = (
// super dirty to quickly make ssr loaders work until we have better
`
if (typeof document === 'undefined') globalThis.document = {}
` + replaceLoader({
code,
loaderData
})
), loaderPartialPath = join(clientDir, getLoaderPath(path));
await outputFile(loaderPartialPath, withLoader), loaderPath = getLoaderPath(path);
}
if (foundRoute.type !== "ssr") {
const loaderProps = { path, params };
if (globalThis.__vxrnresetState?.(), foundRoute.type === "ssg") {
const html = await render({
path,
preloads,
loaderProps,
loaderData,
css: allCSS,
mode: "ssg"
});
await outputFile(htmlOutPath, html);
} else foundRoute.type === "spa" && await outputFile(
htmlOutPath,
`<html><head>
${constants.getSpaHeaderElements({ serverContext: { loaderProps, loaderData } })}
${preloads.map((preload) => ` <script type="module" src="${preload}"></script>`).join(`
`)}
${allCSS.map((file) => ` <link rel="stylesheet" href=${file} />`).join(`
`)}
</head></html>`
);
}
} catch (err) {
const errMsg = err instanceof Error ? `${err.message}
${err.stack}` : `${err}`;
console.error(
`Error building static page at ${path} with id ${relativeId}:
${errMsg}
loaderData:
${JSON.stringify(loaderData || null, null, 2)}
params:
${JSON.stringify(params || null, null, 2)}`
), console.error(err), process.exit(1);
}
const middlewares = (foundRoute.middlewares || []).map((x) => builtMiddlewares[x.contextKey]), cleanPath = path === "/" ? path : removeTrailingSlash(path);
return {
type: foundRoute.type,
css: allCSS,
routeFile: foundRoute.file,
middlewares,
cleanPath,
preloadPath,
loaderPath,
clientJsPath,
serverJsPath,
htmlPath,
loaderData,
params,
path,
preloads
};
}
async function getRender(serverEntry) {
let render = null;
try {
const serverImport = await import(serverEntry);
render = serverImport.default.render || // for an unknown reason this is necessary
serverImport.default.default?.render, typeof render != "function" && (console.error("\u274C Error: didn't find render function in entry", serverImport), process.exit(1));
} catch (err) {
console.error("\u274C Error importing the root entry:"), console.error(` This error happened in the built file: ${serverEntry}`), console.error(err.stack), process.exit(1);
}
return render;
}
function removeTrailingSlash(path) {
return path.endsWith("/") ? path.slice(0, path.length - 1) : path;
}
export {
buildPage
};
//# sourceMappingURL=buildPage.js.map