Skip to content

Commit

Permalink
fix: do not stripe file extension when a custom resolvePath is prov…
Browse files Browse the repository at this point in the history
…ided, close #306
  • Loading branch information
antfu committed Dec 28, 2023
1 parent 63f8ad0 commit 1d31ff2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type MagicString from 'magic-string'
import { version } from '../package.json'
import type { Addon, Import, ImportInjectionResult, InjectImportsOptions, Thenable, TypeDeclarationOptions, Unimport, UnimportContext, UnimportMeta, UnimportOptions } from './types'
import { addImportToCode, dedupeImports, getMagicString, normalizeImports, toExports, toTypeDeclarationFile, toTypeReExports } from './utils'
import { addImportToCode, dedupeImports, getMagicString, normalizeImports, stripFileExtension, toExports, toTypeDeclarationFile, toTypeReExports } from './utils'
import { resolveBuiltinPresets } from './preset'
import { vueTemplateAddon } from './addons'
import { dedupeDtsExports, scanExports, scanFilesFromDir } from './node/scan-dirs'
Expand All @@ -12,7 +12,7 @@ export function createUnimport(opts: Partial<UnimportOptions>): Unimport {

async function generateTypeDeclarations(options?: TypeDeclarationOptions) {
const opts: TypeDeclarationOptions = {
resolvePath: i => i.typeFrom || i.from,
resolvePath: i => stripFileExtension(i.typeFrom || i.from),
...options,
}
const {
Expand Down
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,14 @@ export function toExports(imports: Import[], fileDir?: string, includeType = fal
.join('\n')
}

export function stripFileExtension(path: string) {
return path.replace(/\.[a-zA-Z]+$/, '')
}

export function toTypeDeclarationItems(imports: Import[], options?: TypeDeclarationOptions) {
return imports
.map((i) => {
const from = (options?.resolvePath?.(i) || i.typeFrom || i.from).replace(/\.[a-zA-Z]+$/, '')
const from = options?.resolvePath?.(i) || stripFileExtension(i.typeFrom || i.from)
return `const ${i.as}: typeof import('${from}')${i.name !== '*' ? `['${i.name}']` : ''}`
})
.sort()
Expand All @@ -145,7 +149,7 @@ export function toTypeDeclarationFile(imports: Import[], options?: TypeDeclarati
export function toTypeReExports(imports: Import[], options?: TypeDeclarationOptions) {
const importsMap = new Map<string, Import[]>()
imports.forEach((i) => {
const from = options?.resolvePath?.(i) || i.from
const from = options?.resolvePath?.(i) || stripFileExtension(i.typeFrom || i.from)
const list = importsMap.get(from) || []
list.push(i)
importsMap.set(from, list)
Expand Down
12 changes: 6 additions & 6 deletions test/dts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ it('dts', async () => {
export type { JQuery } from 'jquery'
import('jquery')
// @ts-ignore
export type { CustomType1, CustomInterface1 } from '<root>/playground/composables/index.ts'
import('<root>/playground/composables/index.ts')
export type { CustomType1, CustomInterface1 } from '<root>/playground/composables/index'
import('<root>/playground/composables/index')
// @ts-ignore
export type { CustomType2 } from '<root>/playground/composables/nested/bar/sub/index.ts'
import('<root>/playground/composables/nested/bar/sub/index.ts')
export type { CustomType2 } from '<root>/playground/composables/nested/bar/sub/index'
import('<root>/playground/composables/nested/bar/sub/index')
// @ts-ignore
export type { vanillaTypeOnlyFunction, VanillaInterface, VanillaInterfaceAlias } from '<root>/playground/composables/vanilla.d.ts'
import('<root>/playground/composables/vanilla.d.ts')
export type { vanillaTypeOnlyFunction, VanillaInterface, VanillaInterfaceAlias } from '<root>/playground/composables/vanilla.d'
import('<root>/playground/composables/vanilla.d')
}"
`)
})
1 change: 1 addition & 0 deletions test/public-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ it('public-api', async () => {
"separatorRE",
"stringifyImports",
"stripCommentsAndStrings",
"stripFileExtension",
"toExports",
"toImports",
"toTypeDeclarationFile",
Expand Down

0 comments on commit 1d31ff2

Please sign in to comment.