Skip to content

Commit

Permalink
fix: inserted entry not through beforeWriteFile
Browse files Browse the repository at this point in the history
fix #48
  • Loading branch information
qmhc committed Dec 29, 2021
1 parent ab451ae commit e16b9b1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/plugin.ts
Expand Up @@ -350,10 +350,11 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {
if (insertTypesEntry) {
const pkgPath = resolve(root, 'package.json')
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, 'utf-8')) : {}
const typesPath = pkg.types ? resolve(root, pkg.types) : resolve(outputDir, 'index.d.ts')

let typesPath = pkg.types ? resolve(root, pkg.types) : resolve(outputDir, 'index.d.ts')

if (!fs.existsSync(typesPath)) {
const content =
let content =
entries
.map(entry => {
let filePath = normalizePath(
Expand All @@ -367,6 +368,15 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {
})
.join('\n') + '\n'

if (typeof beforeWriteFile === 'function') {
const result = beforeWriteFile(typesPath, content)

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

await fs.writeFile(typesPath, content, 'utf-8')
}

Expand Down

0 comments on commit e16b9b1

Please sign in to comment.