126 lines
3.1 KiB
JavaScript
126 lines
3.1 KiB
JavaScript
const esbuild = require('esbuild');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const { execSync } = require('child_process');
|
||
|
||
const isWatch = process.argv.includes('--watch');
|
||
|
||
const commonOptions = {
|
||
entryPoints: [path.resolve(__dirname, '../src/index.ts')],
|
||
bundle: true,
|
||
minify: true,
|
||
sourcemap: false,
|
||
target: ['es2018'],
|
||
logLevel: 'info',
|
||
};
|
||
|
||
// 生成 TypeScript 声明文件
|
||
function generateDeclarations() {
|
||
console.log('Generating TypeScript declarations...');
|
||
|
||
const tmpDir = path.resolve(__dirname, '../dist-tmp');
|
||
|
||
try {
|
||
// 清理临时目录
|
||
if (fs.existsSync(tmpDir)) {
|
||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||
}
|
||
fs.mkdirSync(tmpDir, { recursive: true });
|
||
|
||
// 生成所有 .d.ts 到临时目录(宽松模式,忽略错误)
|
||
execSync(
|
||
`tsc --skipLibCheck --declaration --declarationDir ${tmpDir} --outDir ${tmpDir} --strict false --noImplicitAny false --strictNullChecks false --noEmitOnError false --emitDeclarationOnly`,
|
||
{
|
||
cwd: path.resolve(__dirname, '..'),
|
||
stdio: 'pipe',
|
||
}
|
||
);
|
||
} catch (e) {
|
||
// 忽略 tsc 错误
|
||
}
|
||
|
||
// 只复制 .d.ts 文件到 dist,删除多余的 .js
|
||
copyDtsFiles(tmpDir, path.resolve(__dirname, '../dist'));
|
||
|
||
// 清理临时目录
|
||
if (fs.existsSync(tmpDir)) {
|
||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||
}
|
||
|
||
console.log('Declarations generated.');
|
||
}
|
||
|
||
// 递归复制 .d.ts 文件
|
||
function copyDtsFiles(srcDir, destDir) {
|
||
if (!fs.existsSync(destDir)) {
|
||
fs.mkdirSync(destDir, { recursive: true });
|
||
}
|
||
|
||
const files = fs.readdirSync(srcDir);
|
||
for (const file of files) {
|
||
const srcPath = path.join(srcDir, file);
|
||
const stat = fs.statSync(srcPath);
|
||
|
||
if (stat.isDirectory()) {
|
||
copyDtsFiles(srcPath, path.join(destDir, file));
|
||
} else if (file.endsWith('.d.ts')) {
|
||
fs.copyFileSync(srcPath, path.join(destDir, file));
|
||
}
|
||
}
|
||
}
|
||
|
||
async function build() {
|
||
const distDir = path.resolve(__dirname, '../dist');
|
||
|
||
// 清理 dist 目录
|
||
if (fs.existsSync(distDir)) {
|
||
fs.rmSync(distDir, { recursive: true, force: true });
|
||
}
|
||
fs.mkdirSync(distDir, { recursive: true });
|
||
|
||
const outputs = [
|
||
{
|
||
format: 'iife',
|
||
outfile: path.resolve(__dirname, '../dist/index.iife.js'),
|
||
globalName: 'LightSDK',
|
||
},
|
||
{
|
||
format: 'esm',
|
||
outfile: path.resolve(__dirname, '../dist/index.esm.js'),
|
||
},
|
||
{
|
||
format: 'cjs',
|
||
outfile: path.resolve(__dirname, '../dist/index.cjs.js'),
|
||
},
|
||
];
|
||
|
||
for (const output of outputs) {
|
||
await esbuild.build({
|
||
...commonOptions,
|
||
format: output.format,
|
||
outfile: output.outfile,
|
||
globalName: output.globalName,
|
||
});
|
||
}
|
||
|
||
// 生成声明文件
|
||
generateDeclarations();
|
||
|
||
console.log('Build complete!');
|
||
}
|
||
|
||
if (isWatch) {
|
||
console.log('Watching for changes...');
|
||
const ctx = esbuild.context({
|
||
...commonOptions,
|
||
format: 'esm',
|
||
outfile: path.resolve(__dirname, '../dist/index.esm.js'),
|
||
});
|
||
ctx.watch();
|
||
} else {
|
||
build().catch((e) => {
|
||
console.error(e);
|
||
process.exit(1);
|
||
});
|
||
}
|