Skip to content

Commit e0e3a3c

Browse files
bsdayoantfu
andauthored
feat: add an option for preserving file extensions of .ts and .tsx files (#602)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent a9ead70 commit e0e3a3c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ AutoImport({
326326
// Default to 'append'
327327
dtsMode: 'append',
328328

329+
// Preserve the original file extensions in the generated .d.ts file.
330+
// Set to `true` to keep the extensions for .ts and .tsx files.
331+
// Default to false
332+
dtsPreserveExts: false,
333+
329334
// Array of strings of regexes that contains imports meant to be ignored during
330335
// the declaration file generation. You may find this useful when you need to provide
331336
// a custom signature for a function.

src/core/ctx.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
2222
const {
2323
dts: preferDTS = isPackageExists('typescript'),
2424
dtsMode = 'append',
25+
dtsPreserveExts = false,
2526
dirsScanOptions,
2627
dirs,
2728
vueDirectives,
@@ -139,7 +140,11 @@ ${dts}`.trim()}\n`
139140
let currentContent = await unimport.generateTypeDeclarations({
140141
resolvePath: (i) => {
141142
if (i.from.startsWith('.') || isAbsolute(i.from)) {
142-
const related = slash(relative(dir, i.from).replace(/\.ts(x)?$/, ''))
143+
const related = slash(
144+
dtsPreserveExts
145+
? relative(dir, i.from)
146+
: relative(dir, i.from).replace(/\.ts(x)?$/, ''),
147+
)
143148
return !related.startsWith('.')
144149
? `./${related}`
145150
: related

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ export interface Options {
168168
*/
169169
dtsMode?: 'overwrite' | 'append'
170170

171+
/**
172+
* Preserve the original file extensions in the generated .d.ts file.
173+
* Set to `true` to keep the extensions for .ts and .tsx files.
174+
* @default false
175+
*/
176+
dtsPreserveExts?: boolean
177+
171178
/**
172179
* Auto import inside Vue templates
173180
*

0 commit comments

Comments
 (0)