* 加载 TypeScript/JavaScript 配置文件
(configPath: string)
| 35 | * 加载 TypeScript/JavaScript 配置文件 |
| 36 | */ |
| 37 | async function loadConfigFile(configPath: string): Promise<EasypackConfig> { |
| 38 | const ext = path.extname(configPath) |
| 39 | |
| 40 | // 对于 .js 和 .mjs 文件,直接导入 |
| 41 | if (ext === '.js' || ext === '.mjs') { |
| 42 | const module = await import(pathToFileURL(configPath).href) |
| 43 | return module.default |
| 44 | } |
| 45 | |
| 46 | // 对于 .ts 文件,使用 esbuild 编译 |
| 47 | const result = await build({ |
| 48 | entryPoints: [configPath], |
| 49 | bundle: true, |
| 50 | format: 'esm', |
| 51 | platform: 'node', |
| 52 | write: false, |
| 53 | external: ['@easy-editor/easypack'], |
| 54 | }) |
| 55 | |
| 56 | const code = result.outputFiles[0].text |
| 57 | const tempFile = path.join(path.dirname(configPath), '.easypack.config.tmp.mjs') |
| 58 | |
| 59 | fs.writeFileSync(tempFile, code) |
| 60 | try { |
| 61 | const module = await import(pathToFileURL(tempFile).href) |
| 62 | return module.default |
| 63 | } finally { |
| 64 | // 清理临时文件 |
| 65 | try { |
| 66 | fs.unlinkSync(tempFile) |
| 67 | } catch { |
| 68 | // 忽略清理错误 |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * 查找配置文件 |