From abc943143b97dbec0935b62171b16faeebd6fbf4 Mon Sep 17 00:00:00 2001 From: qmhc <40221744+qmhc@users.noreply.github.com> Date: Mon, 18 Jul 2022 11:15:21 +0800 Subject: [PATCH] fix: dynamiclly adjust the exports of inserted index file fix #81, fix #86 --- src/plugin.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index dbc283c..911565c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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[] @@ -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)) { @@ -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` + } + } + if (typeof beforeWriteFile === 'function') { const result = beforeWriteFile(typesPath, content)