From 8edea27a362e30fe0c0f90d0ed7deeeaca37bf8a Mon Sep 17 00:00:00 2001 From: dopamine Date: Tue, 6 May 2025 09:42:40 +0800 Subject: [PATCH] build: rollup dts (#20586) * build: rollup dts * update * update --- internal/build/package.json | 1 + internal/build/src/tasks/types-definitions.ts | 72 +++++- package.json | 3 + .../src/collapse-transition.vue | 2 +- .../components/collection/src/collection.ts | 3 +- packages/components/radio/src/radio-group.ts | 2 +- .../virtual-list/__tests__/scrollbar.test.ts | 2 +- packages/element-plus/make-installer.ts | 2 +- pnpm-lock.yaml | 234 +++++++++++++++++- typings/env.d.ts | 2 +- 10 files changed, 298 insertions(+), 25 deletions(-) diff --git a/internal/build/package.json b/internal/build/package.json index cbbd6375e5..174c443ee7 100644 --- a/internal/build/package.json +++ b/internal/build/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "@element-plus/build-constants": "workspace:*", + "@microsoft/api-extractor": "^7.52.5", "@pnpm/find-workspace-packages": "^4.0.16", "@pnpm/logger": "^4.0.0", "@rollup/plugin-commonjs": "^22.0.1", diff --git a/internal/build/src/tasks/types-definitions.ts b/internal/build/src/tasks/types-definitions.ts index 4930d51399..d354bdb11a 100644 --- a/internal/build/src/tasks/types-definitions.ts +++ b/internal/build/src/tasks/types-definitions.ts @@ -1,15 +1,77 @@ import path from 'path' import { readFile, writeFile } from 'fs/promises' +import ts from 'typescript' import glob from 'fast-glob' import { copy, remove } from 'fs-extra' -import { buildOutput } from '@element-plus/build-utils' +import { Extractor, ExtractorConfig } from '@microsoft/api-extractor' +import { buildOutput, epRoot, projRoot } from '@element-plus/build-utils' import { pathRewriter, run } from '../utils' export const generateTypesDefinitions = async () => { + const typesDir = path.join(buildOutput, 'types', 'packages') + const entryDir = path.join(typesDir, 'element-plus') + const entryFilePath = path.join(entryDir, 'index.d.ts') + const tsDir = path.join(projRoot, 'node_modules', 'typescript') + const tsConfigPath = path.join(projRoot, 'tsconfig.web.json') + const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile) + + // Generate .d.ts files await run( 'npx vue-tsc -p tsconfig.web.json --declaration --emitDeclarationOnly --declarationDir dist/types' ) - const typesDir = path.join(buildOutput, 'types', 'packages') + + // Rollup all .d.ts files into index.d.ts + const extractorConfig = ExtractorConfig.prepare({ + configObject: { + projectFolder: typesDir, + mainEntryPointFilePath: entryFilePath, + apiReport: { + enabled: false, + }, + docModel: { + enabled: false, + }, + tsdocMetadata: { + enabled: false, + }, + dtsRollup: { + enabled: true, + untrimmedFilePath: entryFilePath, + }, + compiler: { + overrideTsconfig: { + compilerOptions: { + lib: tsConfig.config.compilerOptions.lib, + paths: { + 'element-plus': [entryFilePath], + '@element-plus/*': [`${typesDir}/*`], + }, + skipLibCheck: true, + }, + include: [typesDir], + }, + }, + }, + configObjectFullPath: undefined, + packageJsonFullPath: path.join(epRoot, 'package.json'), + }) + + Extractor.invoke(extractorConfig, { typescriptCompilerFolder: tsDir }) + + // Format the bundle file + const fileContent = await readFile(entryFilePath, 'utf8') + const sourceFile = ts.createSourceFile( + entryFilePath, + fileContent, + tsConfig.config.compilerOptions.target + ) + + const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }) + const formattedText = printer.printFile(sourceFile) + + await writeFile(entryFilePath, formattedText, 'utf8') + + // "@element-plus" should be replaced with "element-plus" const filePaths = await glob(`**/*.d.ts`, { cwd: typesDir, absolute: true, @@ -19,7 +81,7 @@ export const generateTypesDefinitions = async () => { await writeFile(filePath, pathRewriter('esm')(content), 'utf8') }) await Promise.all(rewriteTasks) - const sourceDir = path.join(typesDir, 'element-plus') - await copy(sourceDir, typesDir) - await remove(sourceDir) + + await copy(entryDir, typesDir) + await remove(entryDir) } diff --git a/package.json b/package.json index f70b282b71..7c94003a22 100644 --- a/package.json +++ b/package.json @@ -134,6 +134,9 @@ }, "patchedDependencies": { "async-validator@4.2.5": "patches/async-validator@4.2.5.patch" + }, + "overrides": { + "typescript": "$typescript" } }, "lint-staged": { diff --git a/packages/components/collapse-transition/src/collapse-transition.vue b/packages/components/collapse-transition/src/collapse-transition.vue index 37dc1ee49e..dc9daa295f 100644 --- a/packages/components/collapse-transition/src/collapse-transition.vue +++ b/packages/components/collapse-transition/src/collapse-transition.vue @@ -6,7 +6,7 @@