49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
const { build } = require("esbuild");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
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() {
|
|
const env = loadEnvFile(path.resolve(__dirname, ".env.production"));
|
|
const lines = Object.entries(env).map(([key, value]) => `process.env[${JSON.stringify(key)}]=${JSON.stringify(value)};`);
|
|
if (!lines.length) return "";
|
|
return `/* embedded .env.production */\n${lines.join("\n")}`;
|
|
}
|
|
|
|
build({
|
|
entryPoints: ["./index.js"],
|
|
outfile: "./dist/bundle.js",
|
|
platform: "node",
|
|
bundle: true,
|
|
minify: true,
|
|
sourcemap: false,
|
|
target: ["node16", "node18", "node20"],
|
|
banner: {
|
|
js: buildEmbeddedEnvBanner(),
|
|
},
|
|
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));
|