UNPKG

@aniyajs/rotor

Version:

基于webpack5开发的一款专注于打包、运行的工具

63 lines (54 loc) 1.63 kB
const fs = require('fs-extra'); const path = require('path'); const execa = require("execa"); const paths = require('./paths'); // 通用编译参数 const baseCompilerOptions = { module: 'commonjs', allowJs: true, resolveJsonModule: true, esModuleInterop: true, skipLibCheck: true, noEmitOnError: false, strict: false, declaration: false, sourceMap: false, target: "esnext", }; const TEMP_CONFIG_PATH = path.join(paths.appTempCachePath, '__temp_tsconfig_for_tsc__.json'); /** * 编译单个/多个 JS/TS 文件,不受项目 tsconfig 影响 * @param {string|string[]} entryFiles 入口文件路径数组 * @param {string} outDir 输出目录 */ function compileWithTempConfig(entryFiles, outDir) { const files = Array.isArray(entryFiles) ? entryFiles : [entryFiles]; const tempConfig = { compilerOptions: { ...baseCompilerOptions, outDir: outDir, }, include: files, }; try { fs.ensureFileSync(TEMP_CONFIG_PATH); fs.writeFileSync(TEMP_CONFIG_PATH, JSON.stringify(tempConfig, null, 2), 'utf-8'); execa.sync('tsc', ['-p', TEMP_CONFIG_PATH], { cwd: process.cwd(), }); } catch (err) { console.error('❌ 编译临时配置失败:', err.message || err); process.exit(1); } } /** * 清理临时 tsconfig 文件 * 在所有编译完成后调用一次即可 */ function cleanupTempConfig() { if (fs.existsSync(TEMP_CONFIG_PATH)) { fs.removeSync(TEMP_CONFIG_PATH); } } module.exports = compileWithTempConfig; module.exports.cleanupTempConfig = cleanupTempConfig;