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 mcpDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const outputDirectory = path.join(mcpDir, "dist"); 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(outputDirectory, { recursive: true }); await build({ entryPoints: [path.join(mcpDir, "src/server.ts"), path.join(mcpDir, "src/deploy-health.ts")], outdir: outputDirectory, bundle: true, platform: "node", format: "esm", target: "node24", sourcemap, banner: { js: 'import { createRequire as __hivegridCreateRequire } from "node:module"; const require = __hivegridCreateRequire(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 === "pg" || specifier.startsWith("pg/") || specifier === "pino" || specifier.startsWith("pino/") ); }