Skip to content

Commit

Permalink
fix: dynamiclly adjust the exports of inserted index file
Browse files Browse the repository at this point in the history
fix #81, fix #86
  • Loading branch information
qmhc committed Jul 18, 2022
1 parent 39ff8ab commit abc9431
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {

let root: string
let entryRoot = options.entryRoot ?? ''
let libName: string
let indexName: string
let aliases: Alias[]
let entries: string[]
Expand Down Expand Up @@ -160,10 +161,12 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {
)
)

libName = '_default'
indexName = defaultIndex
} else {
const filename = config.build.lib.fileName ?? defaultIndex

libName = config.build.lib.name || '_default'
indexName = typeof filename === 'string' ? filename : filename('es')

if (!dtsRE.test(indexName)) {
Expand Down Expand Up @@ -418,16 +421,23 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {

if (!fs.existsSync(typesPath)) {
const entry = entries[0]
const outputIndex = resolve(outputDir, relative(entryRoot, entry.replace(tsRE, '.d.ts')))

let filePath = normalizePath(
relative(dirname(typesPath), resolve(outputDir, relative(entryRoot, entry)))
)
let filePath = normalizePath(relative(dirname(typesPath), outputIndex))

filePath = filePath.replace(tsRE, '')
filePath = filePath.replace(dtsRE, '')
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`

let content = `export * from '${filePath}'\n`

if (fs.existsSync(outputIndex)) {
const entryCodes = await fs.readFile(outputIndex, 'utf-8')

if (entryCodes.includes('export default')) {
content += `import ${libName} from '${filePath}'\nexport default ${libName}\n`

This comment has been minimized.

Copy link
@mkermani144

mkermani144 Jul 18, 2022

You can also use

content += 'export { default } from ${filePath}'
}
}

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

Expand Down

0 comments on commit abc9431

Please sign in to comment.