From f1ac4d67351c80224d6165b4791b452f4618ee2e Mon Sep 17 00:00:00 2001 From: qmhc <40221744+qmhc@users.noreply.github.com> Date: Thu, 17 Aug 2023 10:01:46 +0800 Subject: [PATCH] fix: improve fine types path logic --- src/plugin.ts | 8 ++------ src/utils.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index cffbd0f..20d2511 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -21,6 +21,7 @@ import { import { ensureAbsolute, ensureArray, + fileTypesPath, isNativeObj, isRegExp, normalizePath, @@ -538,12 +539,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { } catch (e) {} const entryNames = Object.keys(entries) - const types = - pkg.types || - pkg.typings || - pkg.publishConfig?.types || - pkg.publishConfig?.typings || - (pkg.exports?.['.'] || pkg.exports?.['./'])?.types + const types = fileTypesPath(pkg.publishConfig, pkg) const multiple = entryNames.length > 1 let typesPath = types ? resolve(root, types) : resolve(outDir, indexName) diff --git a/src/utils.ts b/src/utils.ts index 1ff8b77..896de4f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -322,3 +322,15 @@ export function toCapitalCase(value: T) { '' ) as CapitalCase } + +export function fileTypesPath(...pkgs: Record[]) { + let path: string + + for (const pkg of pkgs) { + if (typeof pkg !== 'object') continue + + path = pkg.types || pkg.typings || pkg.exports?.['.']?.types || pkg.exports?.['./']?.types + + if (path) return path + } +}