vite-plugin-dts 1.6.3 → 1.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +214 -214
- package/README.zh-CN.md +213 -213
- package/dist/index.mjs +7 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,214 +1,214 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**English** | [中文](./README.zh-CN.md)
|
|
4
|
-
|
|
5
|
-
A vite plugin that generates declaration files (`*.d.ts`) from `.ts(x)` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
pnpm add vite-plugin-dts -D
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
In `vite.config.ts`:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import { resolve } from 'path'
|
|
19
|
-
import { defineConfig } from 'vite'
|
|
20
|
-
import dts from 'vite-plugin-dts'
|
|
21
|
-
|
|
22
|
-
export default defineConfig({
|
|
23
|
-
build: {
|
|
24
|
-
lib: {
|
|
25
|
-
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
-
name: 'MyLib',
|
|
27
|
-
formats: ['es'],
|
|
28
|
-
fileName: 'my-lib'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
plugins: [dts()]
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
In your component:
|
|
36
|
-
|
|
37
|
-
```vue
|
|
38
|
-
<template>
|
|
39
|
-
<div></div>
|
|
40
|
-
</template>
|
|
41
|
-
|
|
42
|
-
<script lang="ts">
|
|
43
|
-
// using defineComponent for inferring types
|
|
44
|
-
import { defineComponent } from 'vue'
|
|
45
|
-
|
|
46
|
-
export default defineComponent({
|
|
47
|
-
name: 'Component'
|
|
48
|
-
})
|
|
49
|
-
</script>
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
```vue
|
|
53
|
-
<script setup lang="ts">
|
|
54
|
-
// Need to access the defineProps returned value to
|
|
55
|
-
// infer types although you never use the props directly
|
|
56
|
-
const props = defineProps<{
|
|
57
|
-
color: 'blue' | 'red'
|
|
58
|
-
}>()
|
|
59
|
-
</script>
|
|
60
|
-
|
|
61
|
-
<template>
|
|
62
|
-
<div>{{ color }}</div>
|
|
63
|
-
</template>
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Options
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
-
|
|
71
|
-
interface TransformWriteFile {
|
|
72
|
-
filePath?: string
|
|
73
|
-
content?: string
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface PluginOptions {
|
|
77
|
-
// Depends on the root directory
|
|
78
|
-
// Defaults base on your vite config root options
|
|
79
|
-
root?: string
|
|
80
|
-
|
|
81
|
-
// Declaration files output directory
|
|
82
|
-
// Can be specified a array to output to multiple directories
|
|
83
|
-
// Defaults base on your vite config output options
|
|
84
|
-
outputDir?: string | string[]
|
|
85
|
-
|
|
86
|
-
// Manually set the root path of the entry files
|
|
87
|
-
// The output path of each file will be caculated base on it
|
|
88
|
-
// Defaults is the smallest public path for all files
|
|
89
|
-
entryRoot?: string
|
|
90
|
-
|
|
91
|
-
// Project init compilerOptions using by ts-morph
|
|
92
|
-
// Default: null
|
|
93
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
94
|
-
|
|
95
|
-
// Project init tsconfig.json file path by ts-morph
|
|
96
|
-
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
97
|
-
// Default: 'tsconfig.json'
|
|
98
|
-
tsConfigFilePath?: string
|
|
99
|
-
|
|
100
|
-
// Set which paths should exclude when transform aliases
|
|
101
|
-
// If it's regexp, it will test the original import path directly
|
|
102
|
-
// Default: []
|
|
103
|
-
aliasesExclude?: (string | RegExp)[]
|
|
104
|
-
|
|
105
|
-
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
106
|
-
// Default: false
|
|
107
|
-
cleanVueFileName?: boolean
|
|
108
|
-
|
|
109
|
-
// Whether transform dynamic import to static
|
|
110
|
-
// Force true when `rollupTypes` is effective
|
|
111
|
-
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
112
|
-
// Default: false
|
|
113
|
-
staticImport?: boolean
|
|
114
|
-
|
|
115
|
-
// Manual set include glob
|
|
116
|
-
// Defaults base on your tsconfig.json include option
|
|
117
|
-
include?: string | string[]
|
|
118
|
-
|
|
119
|
-
// Manual set exclude glob
|
|
120
|
-
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
121
|
-
exclude?: string | string[]
|
|
122
|
-
|
|
123
|
-
// Whether generate types entry file
|
|
124
|
-
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
125
|
-
// Force true when `rollupTypes` is effective
|
|
126
|
-
// Default: false
|
|
127
|
-
insertTypesEntry?: boolean
|
|
128
|
-
|
|
129
|
-
// Set to rollup declaration files after emit
|
|
130
|
-
// Power by `@microsoft/api-extractor`, it will start a new program which takes some time
|
|
131
|
-
// Default: false
|
|
132
|
-
rollupTypes?: boolean
|
|
133
|
-
|
|
134
|
-
// Whether copy .d.ts source files into outputDir
|
|
135
|
-
// Default: true
|
|
136
|
-
copyDtsFiles?: boolean
|
|
137
|
-
|
|
138
|
-
// Whether emit nothing when has any diagnostic
|
|
139
|
-
// Default: false
|
|
140
|
-
noEmitOnError?: boolean
|
|
141
|
-
|
|
142
|
-
// Whether skip typescript diagnostics
|
|
143
|
-
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
144
|
-
// But for the source files with type errors will not be emitted
|
|
145
|
-
// Default: true
|
|
146
|
-
skipDiagnostics?: boolean
|
|
147
|
-
|
|
148
|
-
// Whether log diagnostic informations
|
|
149
|
-
// Not effective when `skipDiagnostics` is true
|
|
150
|
-
// Default: false
|
|
151
|
-
logDiagnostics?: boolean
|
|
152
|
-
|
|
153
|
-
// After emit diagnostic hook
|
|
154
|
-
// According to the length to judge whether there is any type error
|
|
155
|
-
// Default: () => {}
|
|
156
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
157
|
-
|
|
158
|
-
// Before declaration file be writed hook
|
|
159
|
-
// You can transform declaration file-path and content through it
|
|
160
|
-
// The file will be skipped when return exact false
|
|
161
|
-
// Default: () => {}
|
|
162
|
-
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
163
|
-
|
|
164
|
-
// After build hook
|
|
165
|
-
// It wil be called after all declaration files are written
|
|
166
|
-
// Default: () => {}
|
|
167
|
-
afterBuild?: () => void | Promise<void>
|
|
168
|
-
}
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
## Example
|
|
172
|
-
|
|
173
|
-
Clone and run the following script:
|
|
174
|
-
|
|
175
|
-
```sh
|
|
176
|
-
pnpm run test:e2e
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
Then check `example/types`.
|
|
180
|
-
|
|
181
|
-
## FAQ
|
|
182
|
-
|
|
183
|
-
Here are some FAQ's and solutions.
|
|
184
|
-
|
|
185
|
-
### Missing some declaration files after build
|
|
186
|
-
|
|
187
|
-
By default `skipDiagnostics` option is `true`, which means that type diagnostic will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). If there are some files with type errors which interrupt the build process, these files will not be emitted (declaration files won't be generated).
|
|
188
|
-
|
|
189
|
-
If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on the diagnostic and log features of this plugin. It will help you check the type errors during build and log error information to the terminal.
|
|
190
|
-
|
|
191
|
-
### Take type error when using both `script` and `setup-script` in vue component
|
|
192
|
-
|
|
193
|
-
This is usually caused by using `defineComponent` function in both `script` and `setup-script`. When `vue/compiler-sfc` compiles these files, the default export result from `script` gets merged with the parameter object of `defineComponent` from `setup-script`. This is incompatible with parameters and types returned from `defineComponent`, which results in a type error.
|
|
194
|
-
|
|
195
|
-
Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue), you should remove the `defineComponent` which in `script` and export a native object directly.
|
|
196
|
-
|
|
197
|
-
### Take errors that unable to infer types from packages which under `node_modules`
|
|
198
|
-
|
|
199
|
-
This is a exist issue when TypeScript inferring types from packages which under `node_modules` through soft links (pnpm), you can refer to this [issue](https://github.com/microsoft/TypeScript/issues/42873). Currently has a workaround that add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
|
|
200
|
-
|
|
201
|
-
```json
|
|
202
|
-
{
|
|
203
|
-
"compilerOptions": {
|
|
204
|
-
"baseUrl": ".",
|
|
205
|
-
"paths": {
|
|
206
|
-
"third-lib": ["node_modules/third-lib"]
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
## License
|
|
213
|
-
|
|
214
|
-
MIT License.
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**English** | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A vite plugin that generates declaration files (`*.d.ts`) from `.ts(x)` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add vite-plugin-dts -D
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
In `vite.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { resolve } from 'path'
|
|
19
|
+
import { defineConfig } from 'vite'
|
|
20
|
+
import dts from 'vite-plugin-dts'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
build: {
|
|
24
|
+
lib: {
|
|
25
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
+
name: 'MyLib',
|
|
27
|
+
formats: ['es'],
|
|
28
|
+
fileName: 'my-lib'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
plugins: [dts()]
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In your component:
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div></div>
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
<script lang="ts">
|
|
43
|
+
// using defineComponent for inferring types
|
|
44
|
+
import { defineComponent } from 'vue'
|
|
45
|
+
|
|
46
|
+
export default defineComponent({
|
|
47
|
+
name: 'Component'
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
// Need to access the defineProps returned value to
|
|
55
|
+
// infer types although you never use the props directly
|
|
56
|
+
const props = defineProps<{
|
|
57
|
+
color: 'blue' | 'red'
|
|
58
|
+
}>()
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<template>
|
|
62
|
+
<div>{{ color }}</div>
|
|
63
|
+
</template>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
+
|
|
71
|
+
interface TransformWriteFile {
|
|
72
|
+
filePath?: string
|
|
73
|
+
content?: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface PluginOptions {
|
|
77
|
+
// Depends on the root directory
|
|
78
|
+
// Defaults base on your vite config root options
|
|
79
|
+
root?: string
|
|
80
|
+
|
|
81
|
+
// Declaration files output directory
|
|
82
|
+
// Can be specified a array to output to multiple directories
|
|
83
|
+
// Defaults base on your vite config output options
|
|
84
|
+
outputDir?: string | string[]
|
|
85
|
+
|
|
86
|
+
// Manually set the root path of the entry files
|
|
87
|
+
// The output path of each file will be caculated base on it
|
|
88
|
+
// Defaults is the smallest public path for all files
|
|
89
|
+
entryRoot?: string
|
|
90
|
+
|
|
91
|
+
// Project init compilerOptions using by ts-morph
|
|
92
|
+
// Default: null
|
|
93
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
94
|
+
|
|
95
|
+
// Project init tsconfig.json file path by ts-morph
|
|
96
|
+
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
97
|
+
// Default: 'tsconfig.json'
|
|
98
|
+
tsConfigFilePath?: string
|
|
99
|
+
|
|
100
|
+
// Set which paths should exclude when transform aliases
|
|
101
|
+
// If it's regexp, it will test the original import path directly
|
|
102
|
+
// Default: []
|
|
103
|
+
aliasesExclude?: (string | RegExp)[]
|
|
104
|
+
|
|
105
|
+
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
106
|
+
// Default: false
|
|
107
|
+
cleanVueFileName?: boolean
|
|
108
|
+
|
|
109
|
+
// Whether transform dynamic import to static
|
|
110
|
+
// Force true when `rollupTypes` is effective
|
|
111
|
+
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
112
|
+
// Default: false
|
|
113
|
+
staticImport?: boolean
|
|
114
|
+
|
|
115
|
+
// Manual set include glob
|
|
116
|
+
// Defaults base on your tsconfig.json include option
|
|
117
|
+
include?: string | string[]
|
|
118
|
+
|
|
119
|
+
// Manual set exclude glob
|
|
120
|
+
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
121
|
+
exclude?: string | string[]
|
|
122
|
+
|
|
123
|
+
// Whether generate types entry file
|
|
124
|
+
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
125
|
+
// Force true when `rollupTypes` is effective
|
|
126
|
+
// Default: false
|
|
127
|
+
insertTypesEntry?: boolean
|
|
128
|
+
|
|
129
|
+
// Set to rollup declaration files after emit
|
|
130
|
+
// Power by `@microsoft/api-extractor`, it will start a new program which takes some time
|
|
131
|
+
// Default: false
|
|
132
|
+
rollupTypes?: boolean
|
|
133
|
+
|
|
134
|
+
// Whether copy .d.ts source files into outputDir
|
|
135
|
+
// Default: true
|
|
136
|
+
copyDtsFiles?: boolean
|
|
137
|
+
|
|
138
|
+
// Whether emit nothing when has any diagnostic
|
|
139
|
+
// Default: false
|
|
140
|
+
noEmitOnError?: boolean
|
|
141
|
+
|
|
142
|
+
// Whether skip typescript diagnostics
|
|
143
|
+
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
144
|
+
// But for the source files with type errors will not be emitted
|
|
145
|
+
// Default: true
|
|
146
|
+
skipDiagnostics?: boolean
|
|
147
|
+
|
|
148
|
+
// Whether log diagnostic informations
|
|
149
|
+
// Not effective when `skipDiagnostics` is true
|
|
150
|
+
// Default: false
|
|
151
|
+
logDiagnostics?: boolean
|
|
152
|
+
|
|
153
|
+
// After emit diagnostic hook
|
|
154
|
+
// According to the length to judge whether there is any type error
|
|
155
|
+
// Default: () => {}
|
|
156
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
157
|
+
|
|
158
|
+
// Before declaration file be writed hook
|
|
159
|
+
// You can transform declaration file-path and content through it
|
|
160
|
+
// The file will be skipped when return exact false
|
|
161
|
+
// Default: () => {}
|
|
162
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
163
|
+
|
|
164
|
+
// After build hook
|
|
165
|
+
// It wil be called after all declaration files are written
|
|
166
|
+
// Default: () => {}
|
|
167
|
+
afterBuild?: () => void | Promise<void>
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Example
|
|
172
|
+
|
|
173
|
+
Clone and run the following script:
|
|
174
|
+
|
|
175
|
+
```sh
|
|
176
|
+
pnpm run test:e2e
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Then check `example/types`.
|
|
180
|
+
|
|
181
|
+
## FAQ
|
|
182
|
+
|
|
183
|
+
Here are some FAQ's and solutions.
|
|
184
|
+
|
|
185
|
+
### Missing some declaration files after build
|
|
186
|
+
|
|
187
|
+
By default `skipDiagnostics` option is `true`, which means that type diagnostic will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). If there are some files with type errors which interrupt the build process, these files will not be emitted (declaration files won't be generated).
|
|
188
|
+
|
|
189
|
+
If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on the diagnostic and log features of this plugin. It will help you check the type errors during build and log error information to the terminal.
|
|
190
|
+
|
|
191
|
+
### Take type error when using both `script` and `setup-script` in vue component
|
|
192
|
+
|
|
193
|
+
This is usually caused by using `defineComponent` function in both `script` and `setup-script`. When `vue/compiler-sfc` compiles these files, the default export result from `script` gets merged with the parameter object of `defineComponent` from `setup-script`. This is incompatible with parameters and types returned from `defineComponent`, which results in a type error.
|
|
194
|
+
|
|
195
|
+
Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue), you should remove the `defineComponent` which in `script` and export a native object directly.
|
|
196
|
+
|
|
197
|
+
### Take errors that unable to infer types from packages which under `node_modules`
|
|
198
|
+
|
|
199
|
+
This is a exist issue when TypeScript inferring types from packages which under `node_modules` through soft links (pnpm), you can refer to this [issue](https://github.com/microsoft/TypeScript/issues/42873). Currently has a workaround that add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"compilerOptions": {
|
|
204
|
+
"baseUrl": ".",
|
|
205
|
+
"paths": {
|
|
206
|
+
"third-lib": ["node_modules/third-lib"]
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## License
|
|
213
|
+
|
|
214
|
+
MIT License.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,213 +1,213 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**中文** | [English](./README.md)
|
|
4
|
-
|
|
5
|
-
一款用于在 [库模式](https://cn.vitejs.dev/guide/build.html#library-mode) 中,从 `.ts(x)` 或 `.vue` 源文件生成类型文件(`.d.ts`)的 Vite 插件。
|
|
6
|
-
|
|
7
|
-
## 安装
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
pnpm add vite-plugin-dts -D
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## 使用
|
|
14
|
-
|
|
15
|
-
在 `vite.config.ts`:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import { resolve } from 'path'
|
|
19
|
-
import { defineConfig } from 'vite'
|
|
20
|
-
import dts from 'vite-plugin-dts'
|
|
21
|
-
|
|
22
|
-
export default defineConfig({
|
|
23
|
-
build: {
|
|
24
|
-
lib: {
|
|
25
|
-
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
-
name: 'MyLib',
|
|
27
|
-
formats: ['es'],
|
|
28
|
-
fileName: 'my-lib'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
plugins: [dts()]
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
在你的组件中:
|
|
36
|
-
|
|
37
|
-
```vue
|
|
38
|
-
<template>
|
|
39
|
-
<div></div>
|
|
40
|
-
</template>
|
|
41
|
-
|
|
42
|
-
<script lang="ts">
|
|
43
|
-
// 使用 defineComponent 来进行类型推断
|
|
44
|
-
import { defineComponent } from 'vue'
|
|
45
|
-
|
|
46
|
-
export default defineComponent({
|
|
47
|
-
name: 'Component'
|
|
48
|
-
})
|
|
49
|
-
</script>
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
```vue
|
|
53
|
-
<script setup lang="ts">
|
|
54
|
-
// 尽管没有直接使用 props,你仍需要接收 defineProps 的返回值
|
|
55
|
-
const props = defineProps<{
|
|
56
|
-
color: 'blue' | 'red'
|
|
57
|
-
}>()
|
|
58
|
-
</script>
|
|
59
|
-
|
|
60
|
-
<template>
|
|
61
|
-
<div>{{ color }}</div>
|
|
62
|
-
</template>
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## 选项
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
import type { ts, Diagnostic } from 'ts-morph'
|
|
69
|
-
|
|
70
|
-
interface TransformWriteFile {
|
|
71
|
-
filePath?: string
|
|
72
|
-
content?: string
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface PluginOptions {
|
|
76
|
-
// 执行的根目录
|
|
77
|
-
// 默认基于 vite 配置的 root 选项
|
|
78
|
-
root?: string
|
|
79
|
-
|
|
80
|
-
// 声明文件的输出目录
|
|
81
|
-
// 可以指定一个数组来输出到多个目录中
|
|
82
|
-
// 默认基于 vite 配置的输出目录
|
|
83
|
-
outputDir?: string | string[]
|
|
84
|
-
|
|
85
|
-
// 用于手动设置入口文件的根路径
|
|
86
|
-
// 在计算每个文件的输出路径时将基于该路径
|
|
87
|
-
// 默认为所有文件的最小公共路径
|
|
88
|
-
entryRoot?: string
|
|
89
|
-
|
|
90
|
-
// 提供给 ts-morph Project 初始化的 compilerOptions 选项
|
|
91
|
-
// 默认值: null
|
|
92
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
93
|
-
|
|
94
|
-
// 提供给 ts-morph Project 初始化的 tsconfig.json 路径
|
|
95
|
-
// 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
|
|
96
|
-
// 默认值: 'tsconfig.json'
|
|
97
|
-
tsConfigFilePath?: string
|
|
98
|
-
|
|
99
|
-
// 设置在转换别名时哪些路径需要排除
|
|
100
|
-
// 如果为正则,会直接使用 test 和原始路径进行比较
|
|
101
|
-
// 默认值: []
|
|
102
|
-
aliasesExclude?: (string | RegExp)[]
|
|
103
|
-
|
|
104
|
-
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
105
|
-
// 默认值: false
|
|
106
|
-
cleanVueFileName?: boolean
|
|
107
|
-
|
|
108
|
-
//是否将动态引入转换为静态
|
|
109
|
-
// 当开启打包类型文件时强制为 true
|
|
110
|
-
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
111
|
-
// 默认值: false
|
|
112
|
-
staticImport?: boolean
|
|
113
|
-
|
|
114
|
-
// 手动设置包含路径的 glob
|
|
115
|
-
// 默认基于 tsconfig.json 的 include 选项
|
|
116
|
-
include?: string | string[]
|
|
117
|
-
|
|
118
|
-
// 手动设置排除路径的 glob
|
|
119
|
-
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
120
|
-
exclude?: string | string[]
|
|
121
|
-
|
|
122
|
-
// 是否生成类型声明入口
|
|
123
|
-
// 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
|
|
124
|
-
// 当开启打包类型文件时强制为 true
|
|
125
|
-
// 默认值: false
|
|
126
|
-
insertTypesEntry?: boolean
|
|
127
|
-
|
|
128
|
-
// 设置是否在发出类型文件后将其打包
|
|
129
|
-
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
130
|
-
// 默认值: false
|
|
131
|
-
rollupTypes?: boolean
|
|
132
|
-
|
|
133
|
-
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
134
|
-
// 默认值: true
|
|
135
|
-
copyDtsFiles?: boolean
|
|
136
|
-
|
|
137
|
-
// 出现类型诊断信息时不生成类型文件
|
|
138
|
-
// 默认值: false
|
|
139
|
-
noEmitOnError?: boolean
|
|
140
|
-
|
|
141
|
-
// 是否跳过类型诊断
|
|
142
|
-
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
143
|
-
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
144
|
-
// 默认值: true
|
|
145
|
-
skipDiagnostics?: boolean
|
|
146
|
-
|
|
147
|
-
// 是否打印类型诊断信息
|
|
148
|
-
// 当跳过类型诊断时该属性将不会生效
|
|
149
|
-
// 默认值: false
|
|
150
|
-
logDiagnostics?: boolean
|
|
151
|
-
|
|
152
|
-
// 获取诊断信息后的钩子
|
|
153
|
-
// 可以根据参数 length 来判断有误类型错误
|
|
154
|
-
// 默认值: () => {}
|
|
155
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
156
|
-
|
|
157
|
-
// 类型声明文件被写入前的钩子
|
|
158
|
-
// 可以在钩子里转换文件路径和文件内容
|
|
159
|
-
// 当返回 false 时会跳过该文件
|
|
160
|
-
// 默认值: () => {}
|
|
161
|
-
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
162
|
-
|
|
163
|
-
// 构建后回调钩子
|
|
164
|
-
// 将会在所有类型文件被写入后调用
|
|
165
|
-
// 默认值: () => {}
|
|
166
|
-
afterBuild?: () => void | Promise<void>
|
|
167
|
-
}
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
## 示例
|
|
171
|
-
|
|
172
|
-
克隆项目然后执行下列命令:
|
|
173
|
-
|
|
174
|
-
```sh
|
|
175
|
-
pnpm run test:e2e
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
然后检查 `example/types` 目录。
|
|
179
|
-
|
|
180
|
-
## 常见问题
|
|
181
|
-
|
|
182
|
-
此处将收录一些常见的问题并提供一些解决方案。
|
|
183
|
-
|
|
184
|
-
### 打包后出现类型文件缺失
|
|
185
|
-
|
|
186
|
-
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
187
|
-
|
|
188
|
-
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
189
|
-
|
|
190
|
-
### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
|
|
191
|
-
|
|
192
|
-
这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
|
|
193
|
-
|
|
194
|
-
这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
|
|
195
|
-
|
|
196
|
-
### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
|
|
197
|
-
|
|
198
|
-
这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
|
|
199
|
-
|
|
200
|
-
```json
|
|
201
|
-
{
|
|
202
|
-
"compilerOptions": {
|
|
203
|
-
"baseUrl": ".",
|
|
204
|
-
"paths": {
|
|
205
|
-
"third-lib": ["node_modules/third-lib"]
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
## 授权
|
|
212
|
-
|
|
213
|
-
MIT 授权。
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**中文** | [English](./README.md)
|
|
4
|
+
|
|
5
|
+
一款用于在 [库模式](https://cn.vitejs.dev/guide/build.html#library-mode) 中,从 `.ts(x)` 或 `.vue` 源文件生成类型文件(`.d.ts`)的 Vite 插件。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add vite-plugin-dts -D
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用
|
|
14
|
+
|
|
15
|
+
在 `vite.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { resolve } from 'path'
|
|
19
|
+
import { defineConfig } from 'vite'
|
|
20
|
+
import dts from 'vite-plugin-dts'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
build: {
|
|
24
|
+
lib: {
|
|
25
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
+
name: 'MyLib',
|
|
27
|
+
formats: ['es'],
|
|
28
|
+
fileName: 'my-lib'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
plugins: [dts()]
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
在你的组件中:
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div></div>
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
<script lang="ts">
|
|
43
|
+
// 使用 defineComponent 来进行类型推断
|
|
44
|
+
import { defineComponent } from 'vue'
|
|
45
|
+
|
|
46
|
+
export default defineComponent({
|
|
47
|
+
name: 'Component'
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
// 尽管没有直接使用 props,你仍需要接收 defineProps 的返回值
|
|
55
|
+
const props = defineProps<{
|
|
56
|
+
color: 'blue' | 'red'
|
|
57
|
+
}>()
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<div>{{ color }}</div>
|
|
62
|
+
</template>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 选项
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import type { ts, Diagnostic } from 'ts-morph'
|
|
69
|
+
|
|
70
|
+
interface TransformWriteFile {
|
|
71
|
+
filePath?: string
|
|
72
|
+
content?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PluginOptions {
|
|
76
|
+
// 执行的根目录
|
|
77
|
+
// 默认基于 vite 配置的 root 选项
|
|
78
|
+
root?: string
|
|
79
|
+
|
|
80
|
+
// 声明文件的输出目录
|
|
81
|
+
// 可以指定一个数组来输出到多个目录中
|
|
82
|
+
// 默认基于 vite 配置的输出目录
|
|
83
|
+
outputDir?: string | string[]
|
|
84
|
+
|
|
85
|
+
// 用于手动设置入口文件的根路径
|
|
86
|
+
// 在计算每个文件的输出路径时将基于该路径
|
|
87
|
+
// 默认为所有文件的最小公共路径
|
|
88
|
+
entryRoot?: string
|
|
89
|
+
|
|
90
|
+
// 提供给 ts-morph Project 初始化的 compilerOptions 选项
|
|
91
|
+
// 默认值: null
|
|
92
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
93
|
+
|
|
94
|
+
// 提供给 ts-morph Project 初始化的 tsconfig.json 路径
|
|
95
|
+
// 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
|
|
96
|
+
// 默认值: 'tsconfig.json'
|
|
97
|
+
tsConfigFilePath?: string
|
|
98
|
+
|
|
99
|
+
// 设置在转换别名时哪些路径需要排除
|
|
100
|
+
// 如果为正则,会直接使用 test 和原始路径进行比较
|
|
101
|
+
// 默认值: []
|
|
102
|
+
aliasesExclude?: (string | RegExp)[]
|
|
103
|
+
|
|
104
|
+
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
105
|
+
// 默认值: false
|
|
106
|
+
cleanVueFileName?: boolean
|
|
107
|
+
|
|
108
|
+
//是否将动态引入转换为静态
|
|
109
|
+
// 当开启打包类型文件时强制为 true
|
|
110
|
+
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
111
|
+
// 默认值: false
|
|
112
|
+
staticImport?: boolean
|
|
113
|
+
|
|
114
|
+
// 手动设置包含路径的 glob
|
|
115
|
+
// 默认基于 tsconfig.json 的 include 选项
|
|
116
|
+
include?: string | string[]
|
|
117
|
+
|
|
118
|
+
// 手动设置排除路径的 glob
|
|
119
|
+
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
120
|
+
exclude?: string | string[]
|
|
121
|
+
|
|
122
|
+
// 是否生成类型声明入口
|
|
123
|
+
// 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
|
|
124
|
+
// 当开启打包类型文件时强制为 true
|
|
125
|
+
// 默认值: false
|
|
126
|
+
insertTypesEntry?: boolean
|
|
127
|
+
|
|
128
|
+
// 设置是否在发出类型文件后将其打包
|
|
129
|
+
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
130
|
+
// 默认值: false
|
|
131
|
+
rollupTypes?: boolean
|
|
132
|
+
|
|
133
|
+
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
134
|
+
// 默认值: true
|
|
135
|
+
copyDtsFiles?: boolean
|
|
136
|
+
|
|
137
|
+
// 出现类型诊断信息时不生成类型文件
|
|
138
|
+
// 默认值: false
|
|
139
|
+
noEmitOnError?: boolean
|
|
140
|
+
|
|
141
|
+
// 是否跳过类型诊断
|
|
142
|
+
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
143
|
+
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
144
|
+
// 默认值: true
|
|
145
|
+
skipDiagnostics?: boolean
|
|
146
|
+
|
|
147
|
+
// 是否打印类型诊断信息
|
|
148
|
+
// 当跳过类型诊断时该属性将不会生效
|
|
149
|
+
// 默认值: false
|
|
150
|
+
logDiagnostics?: boolean
|
|
151
|
+
|
|
152
|
+
// 获取诊断信息后的钩子
|
|
153
|
+
// 可以根据参数 length 来判断有误类型错误
|
|
154
|
+
// 默认值: () => {}
|
|
155
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
156
|
+
|
|
157
|
+
// 类型声明文件被写入前的钩子
|
|
158
|
+
// 可以在钩子里转换文件路径和文件内容
|
|
159
|
+
// 当返回 false 时会跳过该文件
|
|
160
|
+
// 默认值: () => {}
|
|
161
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
162
|
+
|
|
163
|
+
// 构建后回调钩子
|
|
164
|
+
// 将会在所有类型文件被写入后调用
|
|
165
|
+
// 默认值: () => {}
|
|
166
|
+
afterBuild?: () => void | Promise<void>
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## 示例
|
|
171
|
+
|
|
172
|
+
克隆项目然后执行下列命令:
|
|
173
|
+
|
|
174
|
+
```sh
|
|
175
|
+
pnpm run test:e2e
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
然后检查 `example/types` 目录。
|
|
179
|
+
|
|
180
|
+
## 常见问题
|
|
181
|
+
|
|
182
|
+
此处将收录一些常见的问题并提供一些解决方案。
|
|
183
|
+
|
|
184
|
+
### 打包后出现类型文件缺失
|
|
185
|
+
|
|
186
|
+
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
187
|
+
|
|
188
|
+
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
189
|
+
|
|
190
|
+
### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
|
|
191
|
+
|
|
192
|
+
这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
|
|
193
|
+
|
|
194
|
+
这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
|
|
195
|
+
|
|
196
|
+
### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
|
|
197
|
+
|
|
198
|
+
这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"compilerOptions": {
|
|
203
|
+
"baseUrl": ".",
|
|
204
|
+
"paths": {
|
|
205
|
+
"third-lib": ["node_modules/third-lib"]
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 授权
|
|
212
|
+
|
|
213
|
+
MIT 授权。
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
import __cjs_url__ from 'url';
|
|
3
|
+
import __cjs_path__ from 'path';
|
|
4
|
+
import __cjs_mod__ from 'module';
|
|
5
|
+
const __filename = __cjs_url__.fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __cjs_path__.dirname(__filename);
|
|
7
|
+
const require = __cjs_mod__.createRequire(import.meta.url);
|
|
1
8
|
import { isAbsolute, resolve, dirname, normalize, sep, relative, basename } from 'node:path';
|
|
2
9
|
import fs from 'fs-extra';
|
|
3
10
|
import os from 'os';
|
package/package.json
CHANGED
|
@@ -94,6 +94,7 @@
|
|
|
94
94
|
"_postinstall": "is-ci || husky install",
|
|
95
95
|
"postpublish": "pinst --enable",
|
|
96
96
|
"precommit": "lint-staged -c ./.husky/.lintstagedrc -q",
|
|
97
|
+
"preinstall": "npx only-allow pnpm",
|
|
97
98
|
"prepublishOnly": "pinst --disable",
|
|
98
99
|
"prettier": "pretty-quick --staged",
|
|
99
100
|
"release": "tsx scripts/release.ts",
|
|
@@ -102,5 +103,5 @@
|
|
|
102
103
|
},
|
|
103
104
|
"type": "module",
|
|
104
105
|
"types": "dist/index.d.ts",
|
|
105
|
-
"version": "1.6.
|
|
106
|
+
"version": "1.6.4"
|
|
106
107
|
}
|