Skip to content

Commit

Permalink
feat: add afterRollup option
Browse files Browse the repository at this point in the history
fix #322
  • Loading branch information
qmhc committed Apr 23, 2024
1 parent c4cbd6d commit 791a1b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ export interface PluginOptions {
}
>,

/**
* Hook called after rolling up declaration files
*
* @default () => {}
*/
afterRollup?: (result: ExtractorResult) => MaybePromise<void>,

/**
* Hook called after all declaration files are written
*
Expand Down
11 changes: 9 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export interface PluginOptions {
insertTypesEntry?: boolean,

/**
* 设置是否在发出类型文件后将其打包
* 设置是否将发出的类型文件打包进单个文件
*
* 基于 `@microsoft/api-extractor`,过程将会消耗一些时间
*
Expand Down Expand Up @@ -356,7 +356,14 @@ export interface PluginOptions {
>,

/**
* 在所有类型文件被写入后调用的钩子
* 类型文件被打包进单个文件后的钩子
*
* @default () => {}
*/
afterRollup?: (result: ExtractorResult) => MaybePromise<void>,

/**
* 在所有类型文件被写入后的钩子
*
* 它会接收一个记录了那些最终被写入的文件的映射(path -> content)
*
Expand Down
44 changes: 19 additions & 25 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
strictOutput = true,
afterDiagnostic = noop,
beforeWriteFile = noop,
afterRollup = noop,
afterBuild = noop
} = options

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type ts from 'typescript'
import type {
ExtractorResult,
IExtractorConfigPrepareOptions,
IExtractorInvokeOptions
} from '@microsoft/api-extractor'
Expand Down Expand Up @@ -249,6 +250,13 @@ export interface PluginOptions {
}
>,

/**
* Hook called after rolling up declaration files
*
* @default () => {}
*/
afterRollup?: (result: ExtractorResult) => MaybePromise<void>,

/**
* Hook called after all declaration files are written
*
Expand Down

0 comments on commit 791a1b7

Please sign in to comment.