diff --git a/README.md b/README.md index 886371c..3790dc1 100644 --- a/README.md +++ b/README.md @@ -326,6 +326,11 @@ AutoImport({ // Default to 'append' dtsMode: 'append', + // Preserve the original file extensions in the generated .d.ts file. + // Set to `true` to keep the extensions for .ts and .tsx files. + // Default to false + preserveExtsInDts: false, + // Array of strings of regexes that contains imports meant to be ignored during // the declaration file generation. You may find this useful when you need to provide // a custom signature for a function. diff --git a/src/core/ctx.ts b/src/core/ctx.ts index a2f54b7..ce6af23 100644 --- a/src/core/ctx.ts +++ b/src/core/ctx.ts @@ -22,6 +22,7 @@ export function createContext(options: Options = {}, root = process.cwd()) { const { dts: preferDTS = isPackageExists('typescript'), dtsMode = 'append', + preserveExtsInDts = false, dirsScanOptions, dirs, vueDirectives, @@ -139,7 +140,11 @@ ${dts}`.trim()}\n` let currentContent = await unimport.generateTypeDeclarations({ resolvePath: (i) => { if (i.from.startsWith('.') || isAbsolute(i.from)) { - const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, '')) + const related = slash( + preserveExtsInDts + ? relative(dir, i.from) + : relative(dir, i.from).replace(/\.ts(x)?$/, ''), + ) return !related.startsWith('.') ? `./${related}` : related diff --git a/src/types.ts b/src/types.ts index d2aaeb9..55c07e5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -168,6 +168,13 @@ export interface Options { */ dtsMode?: 'overwrite' | 'append' + /** + * Preserve the original file extensions in the generated .d.ts file. + * Set to `true` to keep the extensions for .ts and .tsx files. + * @default false + */ + preserveExtsInDts?: boolean + /** * Auto import inside Vue templates *