21 lines
599 B
TypeScript
Raw Normal View History

import { getFileExt } from "~/lib/utils";
2024-02-22 12:14:58 +00:00
import { FileSchema } from "~/server/db/schema/file";
import { ProjectSettingsSchema } from "~/server/db/schema/project";
2024-02-22 12:14:58 +00:00
import { transformJs } from "~/server/lib/transform-js";
export const serveJs = async (
file: FileSchema,
cfg?: ProjectSettingsSchema["js"]
) => {
2024-02-22 12:14:58 +00:00
let content = file.content || "";
// transpile to es5
if (cfg?.transpiler === "swc") {
const ext = getFileExt(file.filename);
const typescript = ["ts", "tsx"].includes(ext);
content = await transformJs(content, typescript ? "ts" : "js");
}
2024-02-22 12:14:58 +00:00
return content;
};