2022-09-05 21:12:35 +09:00
|
|
|
import type { FastifyInstance } from "fastify";
|
|
|
|
import httpProxy from "@fastify/http-proxy";
|
|
|
|
import esbuild from "esbuild";
|
|
|
|
|
|
|
|
async function views(fastify: FastifyInstance) {
|
|
|
|
const viewsDir = fastify.config.viewsDir;
|
|
|
|
const entryPoint = `${viewsDir}/index.ts`;
|
|
|
|
const { host, port } = await esbuild.serve(
|
|
|
|
{
|
|
|
|
host: "127.0.0.1",
|
|
|
|
servedir: viewsDir,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
bundle: true,
|
2022-09-07 17:03:54 +09:00
|
|
|
minify: process.env.NODE_ENV !== "development",
|
2022-09-05 21:12:35 +09:00
|
|
|
entryPoints: [entryPoint],
|
|
|
|
define: {
|
|
|
|
"import.meta.env.QUOT_API_ENDPOINT": JSON.stringify(
|
|
|
|
fastify.config.apiEndpoint
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
await fastify.register(httpProxy, {
|
|
|
|
upstream: `http://${host}:${port}`,
|
|
|
|
async preHandler(req) {
|
|
|
|
if (!/\.(?:html|css|js)$/.test(req.url)) req.raw.url = "/";
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default views;
|