corp-transfer/esbuild.config.js

57 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { build } = require("esbuild");
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
// mongodb 的可选原生模块:打包时标记为 external运行时不需要
const mongodbOptionals = [
"kerberos",
"mongodb-client-encryption",
"@mongodb-js/zstd",
"@aws-sdk/credential-providers",
"snappy",
"gcp-metadata",
"aws4",
"socks",
];
function cleanDist() {
const distDir = path.resolve(__dirname, "dist");
fs.rmSync(distDir, { recursive: true, force: true });
fs.mkdirSync(distDir, { recursive: true });
}
function getEmbeddedEnvBanner() {
const envPath = path.resolve(__dirname, ".env.gk");
const envRaw = fs.existsSync(envPath) ? fs.readFileSync(envPath, "utf8") : "";
const parsed = envRaw ? dotenv.parse(envRaw) : {};
const payload = JSON.stringify(parsed);
return [
"(() => {",
` const embeddedEnv = ${payload};`,
" if (!process.env.NODE_ENV) process.env.NODE_ENV = 'gk';",
" for (const [key, value] of Object.entries(embeddedEnv)) {",
" if (process.env[key] == null || process.env[key] === '') process.env[key] = value;",
" }",
"})();",
].join("\n");
}
cleanDist();
build({
entryPoints: ["./transfer.js", "./corpid.js", "./replace-qrcode-url.js"],
outdir: "./dist",
entryNames: "[name]",
platform: "node",
bundle: true,
minify: false,
sourcemap: false,
target: ["node18"],
external: mongodbOptionals,
banner: {
js: getEmbeddedEnvBanner(),
},
}).catch(() => process.exit(1));