83 lines
3.0 KiB
JavaScript
83 lines
3.0 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";
|
|
import { precompressWebAssets, pruneUnreferencedDuplicateCss } from "./precompress-web-assets.mjs";
|
|
|
|
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.HIVEGRID_BUILD_SOURCEMAPS === "1" || process.env.SOURCEMAP === "true" || process.env.BUILD_SOURCEMAPS === "true";
|
|
const browserCspBuildEnvKeys = ["VITE_SENTRY_DSN_WEB", "VITE_OTEL_EXPORTER_OTLP_ENDPOINT", "VITE_HOCUSPOCUS_URL"];
|
|
const browserCspBuildEnvDefines = Object.fromEntries(browserCspBuildEnvKeys.map((name) => [`process.env.${name}`, JSON.stringify(process.env[name] ?? "")]));
|
|
|
|
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: "node24",
|
|
sourcemap: buildSourcemapsEnabled,
|
|
define: browserCspBuildEnvDefines,
|
|
banner: {
|
|
js: 'import { createRequire as __hivegridCreateRequire } from "node:module"; const require = __hivegridCreateRequire(import.meta.url);',
|
|
},
|
|
plugins: [externalizeRuntimeDependencies],
|
|
logLevel: "warning",
|
|
});
|
|
|
|
for (const buildDirectory of ["build", "build-portal"]) {
|
|
const clientBuildDirectory = path.join(rootDir, "apps", "web", buildDirectory, "client");
|
|
await pruneUnreferencedDuplicateCss(path.join(clientBuildDirectory, "assets"));
|
|
await precompressWebAssets(clientBuildDirectory);
|
|
}
|
|
|
|
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 === "pg" ||
|
|
specifier.startsWith("pg/") ||
|
|
specifier === "jsonwebtoken" ||
|
|
specifier.startsWith("jsonwebtoken/") ||
|
|
specifier === "pino" ||
|
|
specifier.startsWith("pino/")
|
|
);
|
|
}
|