Skip to content

Commit

Permalink
fix: ensure dts source files through beforeWriteFile hook
Browse files Browse the repository at this point in the history
fix #43
  • Loading branch information
qmhc committed Nov 21, 2021
1 parent 73df8ad commit 3265412
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/plugin.ts
Expand Up @@ -337,11 +337,32 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {
bundleDebug('output')

await Promise.all(
Array.from(sourceDtsFiles).map(async filePath => {
const targetPath = resolve(outputDir, relative(root, filePath))
Array.from(sourceDtsFiles).map(async originPath => {
let filePath = resolve(outputDir, relative(root, originPath))
let content = ''
let isRewrite = false

// dts source file will not emit any output file
// so need to call the `beforeWriteFile` hook here
if (typeof beforeWriteFile === 'function') {
content = await fs.readFile(originPath, 'utf-8')

const result = beforeWriteFile(filePath, content)

if (result && isNativeObj(result)) {
filePath = result.filePath ?? filePath
isRewrite = result.content !== content
content = result.content ?? content
}
}

await fs.mkdir(dirname(filePath), { recursive: true })

await fs.mkdir(dirname(targetPath), { recursive: true })
await fs.copyFile(filePath, targetPath)
if (isRewrite) {
await fs.writeFile(filePath, content, 'utf-8')
} else {
await fs.copyFile(originPath, filePath)
}
})
)

Expand Down

0 comments on commit 3265412

Please sign in to comment.