| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- // scripts/esbuild_build.mjs (注意扩展名改成 .mjs 或启用 type: "module")
- import fs from "fs";
- import path from "path";
- import dotenv from 'dotenv';
- import esbuild from 'esbuild';
- import esbuildPluginPino from 'esbuild-plugin-pino';
- // 加载 .env 文件
- const define = {
- 'process.env.NODE_ENV': '"production"'
- }
- const env = dotenv.config({ path: path.resolve('.env') }).parsed;
- for (const key in env) {
- define[`process.env.${key}`] = JSON.stringify(env[key]);
- }
- const env_production = dotenv.config({ path: path.resolve('.env.production') }).parsed;
- for (const key in env_production) {
- define[`process.env.${key}`] = JSON.stringify(env_production[key]);
- }
- console.log({define})
- const SupportEsmPlugin = {
- name: 'SupportESM',
- setup(build) {
- build.onEnd(() => {
- const outdir = build.initialOptions.outdir;
- if (!outdir) return;
- const builtins = [
- '_http_agent',
- '_http_client',
- '_http_common',
- '_http_incoming',
- '_http_outgoing',
- '_http_server',
- '_stream_duplex',
- '_stream_passthrough',
- '_stream_readable',
- '_stream_transform',
- '_stream_wrap',
- '_stream_writable',
- '_tls_common',
- '_tls_wrap',
- 'assert',
- 'async_hooks',
- 'buffer',
- 'child_process',
- 'cluster',
- 'console',
- 'constants',
- 'crypto',
- 'dgram',
- 'diagnostics_channel',
- 'dns',
- 'domain',
- 'events',
- 'fs',
- 'http',
- 'http2',
- 'https',
- 'inspector',
- 'module',
- 'net',
- 'os',
- 'path',
- 'perf_hooks',
- 'process',
- 'punycode',
- 'querystring',
- 'readline',
- 'repl',
- 'stream',
- 'string_decoder',
- 'sys',
- 'timers',
- 'tls',
- 'trace_events',
- 'tty',
- 'url',
- 'util',
- 'v8',
- 'vm',
- 'wasi',
- 'worker_threads',
- 'zlib'
- ]
- const pattern = `\\b__require\\("(${builtins.join('|')})"\\)`;
- const rx = new RegExp(pattern, 'gm');
- const modules = new Map();
- const walk = (dir) => {
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
- const fullPath = path.join(dir, entry.name);
- console.log({fullPath})
- if (entry.isDirectory()) {
- walk(fullPath);
- } else if (/\.(js|ts|mjs|cjs)$/.test(entry.name)) {
- const data = fs.readFileSync(fullPath, 'utf8');
- const out = data.replace(rx, (_, mod) => {
- const id = `__import_${mod.toUpperCase()}`;
- modules.set(mod, id);
- return id;
- });
- fs.writeFileSync(fullPath, '');
- modules.forEach(function(val, key) {
- fs.appendFileSync(fullPath, `import ${val} from ${JSON.stringify(key)};\n`, 'utf8');
- })
- fs.appendFileSync(fullPath, out, 'utf8');
- }
- }
- };
- walk(outdir);
- });
- }
- }
- esbuild.build({
- entryPoints: ['src/index.ts'],
- bundle: true,
- platform: 'node',
- target: 'node22',
- format: 'esm', // ✅ 使用 ESM 输出格式
- outdir: "dist",
- outExtension: {
- '.js': '.mjs'
- },
- // outfile: 'dist/index.mjs',
- define,
- sourcemap: false,
- minify: false,
- external: ['node:*', 'tty', 'fs', 'path', 'net', 'crypto'], // 忽略 Node 内置模块
- plugins: [
- esbuildPluginPino({ transports: ["pino", "pino-pretty"] }),
- SupportEsmPlugin
- ],
- }).then(() => {
- console.log('✅ Build finished!');
- }).catch((err) => {
- console.error('❌ Build failed:', err);
- process.exit(1);
- });
|