Skip to content

Commit 2d375d4

Browse files
authored
feat(dts): introduce the dtsMode to control the behavior of generating dts file (#549)
1 parent 125a27e commit 2d375d4

File tree

7 files changed

+117
-86
lines changed

7 files changed

+117
-86
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ AutoImport({
320320
// Set `false` to disable.
321321
dts: './auto-imports.d.ts',
322322

323+
// The mode for generating the .d.ts file.
324+
// 'overwrite': overwrite the whole existing .d.ts file with the new type definitions.
325+
// 'append': only append the new type definitions to the existing .d.ts file, means the existing type definitions will be kept.
326+
// Default to 'append'
327+
dtsMode: 'append',
328+
323329
// Array of strings of regexes that contains imports meant to be ignored during
324330
// the declaration file generation. You may find this useful when you need to provide
325331
// a custom signature for a function.

src/core/ctx.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function createContext(options: Options = {}, root = process.cwd()) {
2121

2222
const {
2323
dts: preferDTS = isPackageExists('typescript'),
24+
dtsMode = 'append',
2425
dirsScanOptions,
2526
dirs,
2627
vueDirectives,
@@ -146,19 +147,21 @@ ${dts}`.trim()}\n`
146147
return i.from
147148
},
148149
})
149-
const currentDTS = parseDTS(currentContent)!
150150
if (options.vueTemplate) {
151151
currentContent = currentContent.replace(
152152
componentCustomPropertiesReg,
153153
$1 => `interface GlobalComponents {}\n ${$1}`,
154154
)
155155
}
156-
if (originalDTS) {
157-
Object.keys(currentDTS).forEach((key) => {
158-
originalDTS[key] = currentDTS[key]
159-
})
160-
const dtsList = Object.keys(originalDTS).sort().map(k => ` ${k}: ${originalDTS[k]}`)
161-
return currentContent.replace(dtsReg, () => `declare global {\n${dtsList.join('\n')}\n}`)
156+
157+
if (dtsMode === 'append') {
158+
const currentDTS = parseDTS(currentContent)!
159+
if (originalDTS) {
160+
Object.assign(originalDTS, currentDTS)
161+
162+
const dtsList = Object.keys(originalDTS).sort().map(k => ` ${k}: ${originalDTS[k]}`)
163+
return currentContent.replace(dtsReg, () => `declare global {\n${dtsList.join('\n')}\n}`)
164+
}
162165
}
163166

164167
return currentContent

src/types.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,20 @@ export interface Options {
154154
parser?: UnimportOptions['parser']
155155

156156
/**
157-
* Filepath to generate corresponding .d.ts file.
158-
* Default enabled when `typescript` is installed locally.
159-
* Set `false` to disable.
160-
*
161-
* @default './auto-imports.d.ts'
157+
* Specifies the file path for generating the corresponding .d.ts file.
158+
* This option is enabled by default when `typescript` is installed locally.
159+
* Set to `false` to disable this feature.
162160
*/
163161
dts?: string | boolean
164162

163+
/**
164+
* Mode for generating the .d.ts file.
165+
* - `overwrite`: overwrite the whole existing .d.ts file with the new type definitions.
166+
* - `append`: only append the new type definitions to the existing .d.ts file, means the existing type definitions will be kept.
167+
* @default 'append'
168+
*/
169+
dtsMode?: 'overwrite' | 'append'
170+
165171
/**
166172
* Auto import inside Vue templates
167173
*

test/__snapshots__/dts.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`dts 1`] = `
3+
exports[`dts > normal 1`] = `
44
"/* eslint-disable */
55
/* prettier-ignore */
66
// @ts-nocheck

test/dts.ignore.test.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

test/dts.increase.test.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/dts.test.ts

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,97 @@ import process from 'node:process'
33
import { expect, it } from 'vitest'
44
import { createContext } from '../src/core/ctx'
55

6-
it('dts', async () => {
7-
const cwd = process.cwd()
8-
const ctx = createContext({
9-
packagePresets: ['magic-string'],
10-
imports: [
11-
'vue-demi',
12-
'react',
13-
'svelte',
14-
'svelte/animate',
15-
'svelte/easing',
16-
'svelte/motion',
17-
'svelte/store',
18-
'svelte/transition',
19-
{
6+
describe('dts', () => {
7+
it('normal', async () => {
8+
const cwd = process.cwd()
9+
const ctx = createContext({
10+
packagePresets: ['magic-string'],
11+
imports: [
12+
'vue-demi',
13+
'react',
14+
'svelte',
15+
'svelte/animate',
16+
'svelte/easing',
17+
'svelte/motion',
18+
'svelte/store',
19+
'svelte/transition',
20+
{
21+
custom: [
22+
'customNamed',
23+
['default', 'customDefault'],
24+
],
25+
custom2: [
26+
['*', 'custom2'],
27+
],
28+
[join(cwd, 'foo.ts')]: ['foo'],
29+
},
30+
'vue/macros',
31+
],
32+
})
33+
34+
expect(await ctx.generateDTS(join(cwd, 'index.d.ts'))).toMatchSnapshot()
35+
})
36+
37+
it('dts ignore', async () => {
38+
const cwd = process.cwd()
39+
const ctx = createContext({
40+
imports: [{
2041
custom: [
21-
'customNamed',
22-
['default', 'customDefault'],
42+
'shouldBePresent',
43+
'shouldAlsoBePresent',
44+
'shouldBeIgnored',
45+
'ignoreme_shoudAlsoBeIgnored',
2346
],
24-
custom2: [
25-
['*', 'custom2'],
26-
],
27-
[join(cwd, 'foo.ts')]: ['foo'],
28-
},
29-
'vue/macros',
30-
],
47+
}],
48+
ignoreDts: [
49+
'shouldBeIgnored',
50+
/^ignoreme_/,
51+
],
52+
})
53+
54+
const dtsContent = await ctx.generateDTS(join(cwd, './test/tmp/dts.ignore.d.ts'))
55+
56+
expect(dtsContent).toContain('shouldBePresent')
57+
expect(dtsContent).toContain('shouldAlsoBePresent')
58+
expect(dtsContent).not.toContain('shouldBeIgnored')
59+
expect(dtsContent).not.toContain('ignoreme_shoudAlsoBeIgnored')
3160
})
3261

33-
expect(await ctx.generateDTS(join(cwd, 'index.d.ts'))).toMatchSnapshot()
62+
it('dts in append mode', async () => {
63+
const cwd = process.cwd()
64+
const dts = join(cwd, './test/tmp/dts.increase.d.ts')
65+
const ctx = createContext({
66+
imports: ['vue'],
67+
dts,
68+
})
69+
70+
const dtsContent = await ctx.generateDTS(dts)
71+
expect(dtsContent).toContain('AAA')
72+
expect(dtsContent).toContain('BBB')
73+
expect(dtsContent).toContain('$$')
74+
expect(dtsContent).toContain('customFile')
75+
expect(dtsContent).toContain('customFile1')
76+
})
77+
78+
it('dts in overwrite mode', async () => {
79+
const cwd = process.cwd()
80+
const dts = join(cwd, './test/tmp/dts.increase.d.ts')
81+
const ctx = createContext({
82+
imports: ['vue'],
83+
dts,
84+
dtsMode: 'overwrite',
85+
})
86+
87+
const dtsContent = await ctx.generateDTS(dts)
88+
expect(dtsContent).not.toContain('AAA')
89+
expect(dtsContent).not.toContain('BBB')
90+
expect(dtsContent).not.toContain('$$')
91+
expect(dtsContent).not.toContain('customFile')
92+
expect(dtsContent).not.toContain('customFile1')
93+
94+
expect(dtsContent).toContain('ref')
95+
expect(dtsContent).toContain('reactive')
96+
expect(dtsContent).toContain('watch')
97+
expect(dtsContent).toContain('computed')
98+
})
3499
})

0 commit comments

Comments
 (0)