Skip to content

Commit 49b16bb

Browse files
LiZhequLiZhequ
and
LiZhequ
authored
fix: component's path should be transformed in dts (#197)
Co-authored-by: LiZhequ <lizhequ@ysbang.cn>
1 parent 3ecdead commit 49b16bb

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/core/declaration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { resolve, dirname, relative, isAbsolute } from 'path'
22
import { promises as fs, existsSync } from 'fs'
33
import { notNullish, slash } from '@antfu/utils'
44
import { Context } from './context'
5+
import { getTransformedPath } from './utils'
56

67
export function parseDeclaration(code: string): Record<string, string> {
78
if (!code)
@@ -18,7 +19,7 @@ export async function generateDeclaration(ctx: Context, root: string, filepath:
1819
.map(({ path, name, importName }) => {
1920
if (!name)
2021
return undefined
21-
22+
path = getTransformedPath(path, ctx)
2223
const related = isAbsolute(path)
2324
? `./${relative(dirname(filepath), path)}`
2425
: path

src/core/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ export function matchGlobs(filepath: string, globs: string[]) {
6060
return false
6161
}
6262

63+
export function getTransformedPath(path: string, ctx: Context): string {
64+
if (ctx.options.importPathTransform) {
65+
const result = ctx.options.importPathTransform(path)
66+
if (result != null)
67+
path = result
68+
}
69+
70+
return path
71+
}
72+
6373
export function stringifyImport(info: ImportInfo | string) {
6474
if (typeof info === 'string')
6575
return `import '${info}'`
@@ -72,11 +82,7 @@ export function stringifyImport(info: ImportInfo | string) {
7282
}
7383

7484
export function stringifyComponentImport({ name, path, importName, sideEffects }: ComponentInfo, ctx: Context) {
75-
if (ctx.options.importPathTransform) {
76-
const result = ctx.options.importPathTransform(path)
77-
if (result != null)
78-
path = result
79-
}
85+
path = getTransformedPath(path, ctx)
8086

8187
const imports = [
8288
stringifyImport({ name, path, importName }),

0 commit comments

Comments
 (0)