Skip to content

Commit

Permalink
feat: add emittedFiles parameter for afterBuild hook
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Dec 25, 2023
1 parent 5fcf989 commit b06d2c4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -358,6 +358,8 @@ export interface PluginOptions {
/**
* Hook called after all declaration files are written
*
* It will be received a map (path -> content) that records those emitted files
*
* @default () => {}
*/
afterBuild?: () => MaybePromise<void>
Expand Down
4 changes: 3 additions & 1 deletion README.zh-CN.md
Expand Up @@ -358,9 +358,11 @@ export interface PluginOptions {
/**
* 在所有类型文件被写入后调用的钩子
*
* 它会接收一个记录了那些最终被写入的文件的映射(path -> content)
*
* @default () => {}
*/
afterBuild?: () => MaybePromise<void>
afterBuild?: (emittedFiles: Map<string, string>) => MaybePromise<void>
}
```

Expand Down
6 changes: 3 additions & 3 deletions examples/vue/vite.config.ts
Expand Up @@ -37,9 +37,9 @@ export default defineConfig({
],
// include: ['src/index.ts'],
exclude: ['src/ignore'],
// staticImport: true,
rollupTypes: true,
// insertTypesEntry: true,
staticImport: true,
// rollupTypes: true,
insertTypesEntry: true,
compilerOptions: {
declarationMap: true
},
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
@@ -1,5 +1,6 @@
import { dtsPlugin } from './plugin'

export default dtsPlugin
export { editSourceMapDir } from './utils'

export type { PluginOptions } from './types'
19 changes: 5 additions & 14 deletions src/plugin.ts
Expand Up @@ -20,6 +20,7 @@ import {
transformDynamicImport
} from './transform'
import {
editSourceMapDir,
ensureAbsolute,
ensureArray,
findTypesPath,
Expand Down Expand Up @@ -698,19 +699,9 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
const path = resolve(targetOutDir, relativePath)

if (wroteFile.endsWith('.map')) {
const relativeOutDir = relative(outDir, targetOutDir)

if (relativeOutDir) {
try {
const sourceMap: { sources: string[] } = JSON.parse(content)

sourceMap.sources = sourceMap.sources.map(source => {
return normalizePath(relative(relativeOutDir, source))
})
content = JSON.stringify(sourceMap)
} catch (e) {
logger.warn(`${logPrefix} ${yellow('Processing source map fail:')} ${path}`)
}
// edit `sources` section with correct relative path of source map file
if (!editSourceMapDir(content, outDir, targetOutDir)) {
logger.warn(`${logPrefix} ${yellow('Processing source map fail:')} ${path}`)
}
}

Expand All @@ -721,7 +712,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
}

if (typeof afterBuild === 'function') {
await wrapPromise(afterBuild())
await wrapPromise(afterBuild(emittedFiles))
}

bundleDebug('finish')
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Expand Up @@ -252,7 +252,9 @@ export interface PluginOptions {
/**
* Hook called after all declaration files are written
*
* It will be received a map (path -> content) that records those emitted files
*
* @default () => {}
*/
afterBuild?: () => MaybePromise<void>
afterBuild?: (emittedFiles: Map<string, string>) => MaybePromise<void>
}
30 changes: 29 additions & 1 deletion src/utils.ts
@@ -1,4 +1,12 @@
import { resolve as _resolve, dirname, isAbsolute, normalize, posix, sep } from 'node:path'
import {
resolve as _resolve,
dirname,
isAbsolute,
normalize,
posix,
relative,
sep
} from 'node:path'
import { existsSync, lstatSync, readdirSync, rmdirSync } from 'node:fs'

import ts from 'typescript'
Expand Down Expand Up @@ -366,3 +374,23 @@ export function setModuleResolution(options: CompilerOptions) {

options.moduleResolution = moduleResolution
}

export function editSourceMapDir(content: string, fromDir: string, toDir: string) {
const relativeOutDir = relative(fromDir, toDir)

if (relativeOutDir) {
try {
const sourceMap: { sources: string[] } = JSON.parse(content)

sourceMap.sources = sourceMap.sources.map(source => {
return normalizePath(relative(relativeOutDir, source))
})

return JSON.stringify(sourceMap)
} catch (e) {
return false
}
}

return true
}

0 comments on commit b06d2c4

Please sign in to comment.