74 lines
2.3 KiB
JavaScript
74 lines
2.3 KiB
JavaScript
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 rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
const entryPoint = path.join(rootDir, "server/server.ts");
|
|
const outputFile = path.join(rootDir, "build/server-runtime/server.js");
|
|
const nodeBuiltins = new Set([...builtinModules, ...builtinModules.map((moduleName) => `node:${moduleName}`)]);
|
|
const buildSourcemapsEnabled =
|
|
process.env.TOOLCTRL_BUILD_SOURCEMAPS === "1" ||
|
|
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: buildSourcemapsEnabled,
|
|
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 === "react-router" ||
|
|
specifier.startsWith("react-router/") ||
|
|
specifier === "@react-router/node" ||
|
|
specifier.startsWith("@react-router/node/") ||
|
|
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/")
|
|
);
|
|
}
|