Skip to content

fix: component's path should be transformed in dts #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve, dirname, relative, isAbsolute } from 'path'
import { promises as fs, existsSync } from 'fs'
import { notNullish, slash } from '@antfu/utils'
import { Context } from './context'
import { getTransformedPath } from './utils'

export function parseDeclaration(code: string): Record<string, string> {
if (!code)
Expand All @@ -18,7 +19,7 @@ export async function generateDeclaration(ctx: Context, root: string, filepath:
.map(({ path, name, importName }) => {
if (!name)
return undefined

path = getTransformedPath(path, ctx)
const related = isAbsolute(path)
? `./${relative(dirname(filepath), path)}`
: path
Expand Down
16 changes: 11 additions & 5 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export function matchGlobs(filepath: string, globs: string[]) {
return false
}

export function getTransformedPath(path: string, ctx: Context): string {
if (ctx.options.importPathTransform) {
const result = ctx.options.importPathTransform(path)
if (result != null)
path = result
}

return path
}

export function stringifyImport(info: ImportInfo | string) {
if (typeof info === 'string')
return `import '${info}'`
Expand All @@ -72,11 +82,7 @@ export function stringifyImport(info: ImportInfo | string) {
}

export function stringifyComponentImport({ name, path, importName, sideEffects }: ComponentInfo, ctx: Context) {
if (ctx.options.importPathTransform) {
const result = ctx.options.importPathTransform(path)
if (result != null)
path = result
}
path = getTransformedPath(path, ctx)

const imports = [
stringifyImport({ name, path, importName }),
Expand Down