hospital-adapter-service/esbuild.config.js

92 lines
3.1 KiB
JavaScript
Raw Normal View History

2026-06-18 16:10:40 +08:00
const { build } = require("esbuild");
const fs = require("fs");
const path = require("path");
const buildOptions = parseBuildOptions(process.argv.slice(2));
2026-06-18 16:10:40 +08:00
function loadEnvFile(filePath) {
if (!fs.existsSync(filePath)) return {};
return fs.readFileSync(filePath, "utf8").split(/\r?\n/).reduce((acc, line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith("#")) return acc;
const index = trimmed.indexOf("=");
if (index <= 0) return acc;
const key = trimmed.slice(0, index).trim();
const value = trimmed.slice(index + 1).trim();
if (key) acc[key] = value;
return acc;
}, {});
}
function buildEmbeddedEnvBanner(envFilePath) {
const env = loadEnvFile(envFilePath);
2026-06-18 16:10:40 +08:00
const lines = Object.entries(env).map(([key, value]) => `process.env[${JSON.stringify(key)}]=${JSON.stringify(value)};`);
if (!lines.length) return "";
return `/* embedded ${path.basename(envFilePath)} */\n${lines.join("\n")}`;
2026-06-18 16:10:40 +08:00
}
function parseBuildOptions(args) {
const raw = {};
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === "--env" || arg === "--env-file" || arg === "--out" || arg === "--outfile") {
raw[arg.slice(2)] = args[i + 1];
i += 1;
continue;
}
if (arg.startsWith("--env=")) raw.env = arg.slice("--env=".length);
if (arg.startsWith("--env-file=")) raw["env-file"] = arg.slice("--env-file=".length);
if (arg.startsWith("--out=")) raw.out = arg.slice("--out=".length);
if (arg.startsWith("--outfile=")) raw.outfile = arg.slice("--outfile=".length);
}
const envName = normalizeName(raw.env || process.env.CONFIG_BUILD_ENV || "");
const envFileInput = raw["env-file"] || process.env.CONFIG_BUILD_ENV_FILE || (envName ? `.env.${envName}` : ".env.production");
const envFilePath = path.resolve(__dirname, envFileInput);
const outfile = raw.outfile || raw.out || process.env.CONFIG_BUILD_OUTFILE || path.resolve(__dirname, "dist", envName ? `bundle.${envName}.js` : "bundle.js");
if ((raw.env || raw["env-file"] || process.env.CONFIG_BUILD_ENV || process.env.CONFIG_BUILD_ENV_FILE) && !fs.existsSync(envFilePath)) {
console.error(`[build] env file not found: ${envFilePath}`);
process.exit(1);
}
return {
envName,
envFilePath,
outfile: path.resolve(__dirname, outfile),
};
}
function normalizeName(input) {
const text = String(input || "").trim();
if (!text) return "";
return text.replace(/^\.env\./, "").replace(/[^a-zA-Z0-9_-]/g, "_");
}
console.log(`[build] env: ${buildOptions.envFilePath}`);
console.log(`[build] outfile: ${buildOptions.outfile}`);
2026-06-18 16:10:40 +08:00
build({
entryPoints: ["./index.js"],
outfile: buildOptions.outfile,
2026-06-18 16:10:40 +08:00
platform: "node",
bundle: true,
minify: true,
sourcemap: false,
target: ["node16", "node18", "node20"],
banner: {
js: buildEmbeddedEnvBanner(buildOptions.envFilePath),
2026-06-18 16:10:40 +08:00
},
external: ["mysql2", "pg"],
plugins: [
{
name: "bundle-oracledb-js",
setup(build) {
build.onResolve({ filter: /^oracledb$/ }, () => ({
path: path.resolve(__dirname, "node_modules", "oracledb", "index.js"),
}));
},
},
],
}).catch(() => process.exit(1));