65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
/*
|
|
* build.mjs — TypeScript 编译为 ES module JavaScript
|
|
*
|
|
* 每个 .ts 编译为同名 .js,保留 ES module import 路径(含 .js 后缀)
|
|
* 排除:types/(类型声明)、vendor/(第三方)、node_modules、.git、logs、assets
|
|
*/
|
|
|
|
import { build } from 'esbuild';
|
|
import { readdir } from 'fs/promises';
|
|
import path from 'path';
|
|
|
|
const SKIP_DIRS = new Set(['node_modules', 'vendor', '.git', 'logs', 'assets', 'types']);
|
|
|
|
async function walk(dir, files = []) {
|
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
for (const e of entries) {
|
|
if (e.isDirectory()) {
|
|
if (SKIP_DIRS.has(e.name)) continue;
|
|
await walk(path.join(dir, e.name), files);
|
|
} else if (e.isFile() && e.name.endsWith('.ts') && !e.name.endsWith('.d.ts')) {
|
|
files.push(path.join(dir, e.name));
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
const files = await walk('.');
|
|
if (files.length === 0) {
|
|
console.log('No .ts files to build.');
|
|
process.exit(0);
|
|
}
|
|
|
|
const isWatch = process.argv.includes('--watch');
|
|
|
|
console.log(`Building ${files.length} .ts files → .js (esbuild, ESM, ES2022)...`);
|
|
|
|
if (isWatch) {
|
|
const ctx = await (await import('esbuild')).context({
|
|
entryPoints: files,
|
|
outbase: '.',
|
|
outdir: '.',
|
|
outExtension: { '.js': '.js' },
|
|
format: 'esm',
|
|
target: 'es2022',
|
|
sourcemap: false,
|
|
logLevel: 'info',
|
|
allowOverwrite: true,
|
|
});
|
|
await ctx.watch();
|
|
console.log('Watching for changes... (Ctrl+C to exit)');
|
|
} else {
|
|
await build({
|
|
entryPoints: files,
|
|
outbase: '.',
|
|
outdir: '.',
|
|
outExtension: { '.js': '.js' },
|
|
format: 'esm',
|
|
target: 'es2022',
|
|
sourcemap: false,
|
|
logLevel: 'info',
|
|
allowOverwrite: true,
|
|
});
|
|
console.log(`Done. Built ${files.length} files.`);
|
|
}
|