import { mkdir } from "node:fs/promises"; import { builtinModules } from "node:module"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { build } from "esbuild"; const apiDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const entryPoint = path.join(apiDir, "src/server.ts"); const outputFile = path.join(apiDir, "dist/server.js"); const nodeBuiltins = new Set([...builtinModules, ...builtinModules.map((moduleName) => `node:${moduleName}`)]); const sourcemap = process.env.SOURCEMAP === "true" || process.env.BUILD_SOURCEMAPS === "true"; const externalizeRuntimeDependencies = { name: "externalize-runtime-dependencies", setup(buildContext) { buildContext.onResolve({ filter: /.*/ }, (args) => { if (nodeBuiltins.has(args.path)) { return { path: args.path, external: true }; } if (isExternalRuntimePackage(args.path)) { return { path: args.path, external: true }; } return undefined; }); }, }; await mkdir(path.dirname(outputFile), { recursive: true }); await build({ entryPoints: [entryPoint], outfile: outputFile, bundle: true, platform: "node", format: "esm", target: "node20", sourcemap, banner: { js: 'import { createRequire as __toolctrlCreateRequire } from "node:module"; const require = __toolctrlCreateRequire(import.meta.url);', }, plugins: [externalizeRuntimeDependencies], logLevel: "warning", }); function isBarePackageSpecifier(specifier) { return !specifier.startsWith(".") && !specifier.startsWith("/") && !specifier.startsWith("#"); } function isExternalRuntimePackage(specifier) { if (!isBarePackageSpecifier(specifier)) { return false; } return ( specifier === "@prisma/client" || specifier.startsWith("@prisma/client/") || specifier === "@prisma/adapter-pg" || specifier.startsWith("@prisma/adapter-pg/") || specifier === "jsonwebtoken" || specifier.startsWith("jsonwebtoken/") || specifier === "pino" || specifier.startsWith("pino/") ); }