From 791a1b7455e8715e068aef84e4c9877157d8cf22 Mon Sep 17 00:00:00 2001 From: qmhc <544022268@qq.com> Date: Tue, 23 Apr 2024 13:08:52 +0800 Subject: [PATCH] feat: add afterRollup option fix #322 --- README.md | 7 +++++++ README.zh-CN.md | 11 +++++++++-- src/plugin.ts | 44 +++++++++++++++++++------------------------- src/rollup.ts | 4 +--- src/types.ts | 8 ++++++++ 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3b1644e..2f6ce6c 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,13 @@ export interface PluginOptions { } >, + /** + * Hook called after rolling up declaration files + * + * @default () => {} + */ + afterRollup?: (result: ExtractorResult) => MaybePromise, + /** * Hook called after all declaration files are written * diff --git a/README.zh-CN.md b/README.zh-CN.md index 21579c3..22398ff 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -269,7 +269,7 @@ export interface PluginOptions { insertTypesEntry?: boolean, /** - * 设置是否在发出类型文件后将其打包 + * 设置是否将发出的类型文件打包进单个文件 * * 基于 `@microsoft/api-extractor`,过程将会消耗一些时间 * @@ -356,7 +356,14 @@ export interface PluginOptions { >, /** - * 在所有类型文件被写入后调用的钩子 + * 类型文件被打包进单个文件后的钩子 + * + * @default () => {} + */ + afterRollup?: (result: ExtractorResult) => MaybePromise, + + /** + * 在所有类型文件被写入后的钩子 * * 它会接收一个记录了那些最终被写入的文件的映射(path -> content) * diff --git a/src/plugin.ts b/src/plugin.ts index 290d74a..172f0be 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -85,6 +85,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { strictOutput = true, afterDiagnostic = noop, beforeWriteFile = noop, + afterRollup = noop, afterBuild = noop } = options @@ -663,40 +664,33 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { ? getTsConfig(configPath, host.readFile).compilerOptions : rawCompilerOptions - if (multiple) { - for (const name of entryNames) { - const path = cleanPath(resolve(outDir, tsToDts(name))) - - rollupDeclarationFiles({ - root, - configPath, - compilerOptions, - outDir, - entryPath: path, - fileName: basename(path), - libFolder, - rollupConfig, - rollupOptions - }) - - emittedFiles.delete(path) - rollupFiles.add(path) - } - } else { - rollupDeclarationFiles({ + const rollup = async (path: string) => { + const result = rollupDeclarationFiles({ root, configPath, compilerOptions, outDir, - entryPath: typesPath, - fileName: basename(typesPath), + entryPath: path, + fileName: basename(path), libFolder, rollupConfig, rollupOptions }) - emittedFiles.delete(typesPath) - rollupFiles.add(typesPath) + emittedFiles.delete(path) + rollupFiles.add(path) + + if (typeof afterRollup === 'function') { + await unwrapPromise(afterRollup(result)) + } + } + + if (multiple) { + await runParallel(cpus().length, entryNames, async name => { + await rollup(cleanPath(resolve(outDir, tsToDts(name)))) + }) + } else { + await rollup(typesPath) } await runParallel(cpus().length, Array.from(emittedFiles.keys()), f => unlink(f)) diff --git a/src/rollup.ts b/src/rollup.ts index a7cbdc2..f221ed3 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -85,13 +85,11 @@ export function rollupDeclarationFiles({ packageJsonFullPath: tryGetPkgPath(configObjectFullPath) }) - const result = Extractor.invoke(extractorConfig, { + return Extractor.invoke(extractorConfig, { localBuild: false, showVerboseMessages: false, showDiagnostics: false, typescriptCompilerFolder: libFolder ? resolve(libFolder) : undefined, ...rollupOptions }) - - return result.succeeded } diff --git a/src/types.ts b/src/types.ts index e30849b..d47603b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,6 @@ import type ts from 'typescript' import type { + ExtractorResult, IExtractorConfigPrepareOptions, IExtractorInvokeOptions } from '@microsoft/api-extractor' @@ -249,6 +250,13 @@ export interface PluginOptions { } >, + /** + * Hook called after rolling up declaration files + * + * @default () => {} + */ + afterRollup?: (result: ExtractorResult) => MaybePromise, + /** * Hook called after all declaration files are written *