import { resolve } from "path"; import { writeFileSync, unlinkSync, rmSync } from "fs"; import { execSync } from "child_process"; import dts from "rollup-plugin-dts"; import tsResolve from "@rollup/plugin-node-resolve"; import * as Rollup from "rollup"; const generateTs = (app: string) => { const enter = app ? `src/app/${app}/` : `src/board/`; const enterPath = resolve(__dirname, enter, `index.ts`); const tsconfigPath = resolve(__dirname, "./tsconfig.prod.json"); const outPath = resolve(__dirname, "dist/tempts"); const tsconfig = { compilerOptions: { target: "es2015", useDefineForClassFields: true, module: "ESNext", lib: ["es2015", "DOM", "DOM.Iterable"], skipLibCheck: true, /* Bundler mode */ moduleResolution: "Node", resolveJsonModule: true, isolatedModules: true, jsx: "preserve", /* Linting */ noUnusedLocals: true, noUnusedParameters: true, noFallthroughCasesInSwitch: true, allowImportingTsExtensions: true, emitDeclarationOnly: true, // 只输出声明文件(ts 产物) declaration: true, // 自动生成声明文件 declarationDir: outPath, // 声明文件生成的目录 incremental: false, // 启用增量编译 }, include: [enterPath, "src/vite-env.d.ts"], references: [ { path: "./tsconfig.node.json", }, ], }; writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 2)); try { execSync(`cd ${__dirname} && npm run tsc`, { stdio: "inherit" }); unlinkSync(tsconfigPath); } catch (error) { console.error("Error executing command:", error); throw error; } return outPath; }; const mergeTs = async (app: string, input: string) => { const name = app ? app : "board"; const enter = resolve(input, `${app ? `app/${app}/` : ``}index.d.ts`); const out = resolve(__dirname, `./dist/${name}.d.ts`); const bundle = await Rollup.rollup({ input: enter, plugins: [dts(), tsResolve()], }); await bundle.write({ file: out, format: "es" }); rmSync(input, { recursive: true, force: true }); }; export const generateDTS = async (app: string) => { await mergeTs(app, generateTs(app)); };