diff --git a/internal/vite-config/build.config.ts b/internal/vite-config/build.config.ts deleted file mode 100644 index 97e572c56..000000000 --- a/internal/vite-config/build.config.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { defineBuildConfig } from 'unbuild'; - -export default defineBuildConfig({ - clean: true, - declaration: true, - entries: ['src/index'], -}); diff --git a/internal/vite-config/package.json b/internal/vite-config/package.json index 129113704..f83d85b50 100644 --- a/internal/vite-config/package.json +++ b/internal/vite-config/package.json @@ -12,7 +12,8 @@ "license": "MIT", "type": "module", "scripts": { - "stub": "pnpm unbuild --stub" + "build": "pnpm exec tsdown", + "stub": "pnpm exec tsdown" }, "files": [ "dist" diff --git a/internal/vite-config/tsdown.config.ts b/internal/vite-config/tsdown.config.ts new file mode 100644 index 000000000..88b76bebc --- /dev/null +++ b/internal/vite-config/tsdown.config.ts @@ -0,0 +1,40 @@ +import { cp, mkdir } from 'node:fs/promises'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +import { defineConfig } from 'tsdown'; + +const rootDir = dirname(fileURLToPath(import.meta.url)); +const loadingAssets = ['default-loading-antd.html', 'default-loading.html']; + +export default defineConfig({ + clean: true, + deps: { + skipNodeModulesBundle: true, + }, + dts: { + resolver: 'tsc', + }, + entry: ['src/index.ts'], + format: ['esm'], + hooks: { + 'build:done': async (context) => { + const outDir = context.options.outDir; + if (!outDir) { + return; + } + + await mkdir(outDir, { recursive: true }); + + for (const file of loadingAssets) { + await cp( + join(rootDir, 'src/plugins/inject-app-loading', file), + join(outDir, file), + ); + } + }, + }, + outExtensions: () => ({ + dts: '.d.ts', + }), +}); diff --git a/scripts/vsh/src/check-circular/index.ts b/scripts/vsh/src/check-circular/index.ts index 064250613..999f7f249 100644 --- a/scripts/vsh/src/check-circular/index.ts +++ b/scripts/vsh/src/check-circular/index.ts @@ -1,10 +1,15 @@ import type { CAC } from 'cac'; -import { extname } from 'node:path'; +import { access, mkdtemp, readFile, rm } from 'node:fs/promises'; +import { createRequire } from 'node:module'; +import { tmpdir } from 'node:os'; +import { extname, join } from 'node:path'; -import { getStagedFiles } from '@vben/node-utils'; +import { execa, getStagedFiles } from '@vben/node-utils'; -import { circularDepsDetect } from 'circular-dependency-scanner'; +const require = createRequire(import.meta.url); +const circularScannerCli = + require.resolve('circular-dependency-scanner/dist/cli.js'); // 默认配置 const DEFAULT_CONFIG = { @@ -41,6 +46,44 @@ interface CommandOptions { // 缓存机制 const cache = new Map(); +async function detectCircularDependencies({ + cwd, + ignorePattern, + staged, +}: { + cwd: string; + ignorePattern: string; + staged: boolean; +}): Promise { + const tempDir = await mkdtemp(join(tmpdir(), 'vsh-check-circular-')); + const outputFile = join(tempDir, 'circles.json'); + + try { + const args = [circularScannerCli, cwd, '--output', outputFile]; + + if (staged) { + args.push('--absolute'); + } + + args.push('--ignore', ignorePattern); + + await execa(process.execPath, args, { + cwd, + }); + + await access(outputFile); + const output = await readFile(outputFile, 'utf8'); + return JSON.parse(output) as CircularDependencyResult[]; + } catch (error) { + if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') { + return []; + } + throw error; + } finally { + await rm(tempDir, { force: true, recursive: true }); + } +} + /** * 格式化循环依赖的输出 * @param circles - 循环依赖结果 @@ -85,17 +128,17 @@ async function checkCircular({ const cacheKey = `${staged}-${process.cwd()}-${ignorePattern}`; if (cache.has(cacheKey)) { const cachedResults = cache.get(cacheKey); - if (cachedResults) { - verbose && formatCircles(cachedResults); + if (cachedResults && verbose) { + formatCircles(cachedResults); } return; } // 检测循环依赖 - const results = await circularDepsDetect({ - absolute: staged, + const results = await detectCircularDependencies({ cwd: process.cwd(), - ignore: [ignorePattern], + ignorePattern, + staged, }); if (staged) { @@ -118,11 +161,15 @@ async function checkCircular({ // 更新缓存 cache.set(cacheKey, circularFiles); - verbose && formatCircles(circularFiles); + if (verbose) { + formatCircles(circularFiles); + } } else { // 更新缓存 cache.set(cacheKey, results); - verbose && formatCircles(results); + if (verbose) { + formatCircles(results); + } } // 如果发现循环依赖,只输出警告信息