vite-plugin-dts 3.0.1 → 3.0.3

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.zh-CN.md CHANGED
@@ -1,310 +1,309 @@
1
- <h1 align="center">vite-plugin-dts</h1>
2
-
3
- <p align="center">
4
- 一款用于在 <a href="https://cn.vitejs.dev/guide/build.html#library-mode">库模式</a> 中从 <code>.ts(x)</code> 或 <code>.vue</code> 源文件生成类型文件(<code>*.d.ts</code>)的 Vite 插件。
5
- </p>
6
-
7
- <p align="center">
8
- <a href="https://www.npmjs.com/package/vite-plugin-dts">
9
- <img src="https://img.shields.io/npm/v/vite-plugin-dts?color=orange&label=" alt="version" />
10
- </a>
11
- </p>
12
-
13
- **中文** | [English](./README.md)
14
-
15
- ## 安装
16
-
17
- ```sh
18
- pnpm add vite-plugin-dts -D
19
- ```
20
-
21
- ## 使用
22
-
23
- 在 `vite.config.ts`:
24
-
25
- ```ts
26
- import { resolve } from 'path'
27
- import { defineConfig } from 'vite'
28
- import dts from 'vite-plugin-dts'
29
-
30
- export default defineConfig({
31
- build: {
32
- lib: {
33
- entry: resolve(__dirname, 'src/index.ts'),
34
- name: 'MyLib',
35
- formats: ['es'],
36
- fileName: 'my-lib'
37
- }
38
- },
39
- plugins: [dts()]
40
- })
41
- ```
42
-
43
- 在你的组件中:
44
-
45
- ```vue
46
- <template>
47
- <div></div>
48
- </template>
49
-
50
- <script lang="ts">
51
- // 使用 defineComponent 来进行类型推断
52
- import { defineComponent } from 'vue'
53
-
54
- export default defineComponent({
55
- name: 'Component'
56
- })
57
- </script>
58
- ```
59
-
60
- ```vue
61
- <script setup lang="ts">
62
- // 尽管没有直接使用 props,你仍需要接收 defineProps 的返回值
63
- const props = defineProps<{
64
- color: 'blue' | 'red'
65
- }>()
66
- </script>
67
-
68
- <template>
69
- <div>{{ color }}</div>
70
- </template>
71
- ```
72
-
73
- ## 常见问题
74
-
75
- 此处将收录一些常见的问题并提供一些解决方案。
76
-
77
- ### 打包后出现类型文件缺失 (`1.7.0` 之前)
78
-
79
- 默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
80
-
81
- 如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
82
-
83
- ### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误(`3.0.0` 之前)
84
-
85
- 这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
86
-
87
- 这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
88
-
89
- ### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
90
-
91
- 这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
92
-
93
- ```json
94
- {
95
- "compilerOptions": {
96
- "baseUrl": ".",
97
- "paths": {
98
- "third-lib": ["node_modules/third-lib"]
99
- }
100
- }
101
- }
102
- ```
103
-
104
- ## 选项
105
-
106
- ```ts
107
- import type ts from 'typescript'
108
- import type { LogLevel } from 'vite'
109
-
110
- interface TransformWriteFile {
111
- filePath?: string
112
- content?: string
113
- }
114
-
115
- export interface PluginOptions {
116
- /**
117
- * 指定根目录
118
- *
119
- * 默认基于 Vite 配置的 'root',使用 Rollup 时基于 `process.cwd()`
120
- */
121
- root?: string
122
-
123
- /**
124
- * 指定输出目录
125
- *
126
- * 可以指定一个数组来输出到多个目录中
127
- *
128
- * 默认基于 Vite 配置的 'build.outDir',使用 Rollup 时基于 tsconfig.json 的 `outDir`
129
- */
130
- outDir?: string | string[]
131
-
132
- /**
133
- * 用于手动设置入口文件的根路径,通常用在 monorepo
134
- *
135
- * 在计算每个文件的输出路径时将基于该路径
136
- *
137
- * 默认为所有文件的最小公共路径
138
- */
139
- entryRoot?: string
140
-
141
- /**
142
- * 严格限制类型文件生产在 `outDir` 内
143
- *
144
- * 由于当指定了 `entryRoot` 时,类型文件有可能位于 `outDir` 之外
145
- *
146
- * @default true
147
- */
148
- strictOutput?: boolean
149
-
150
- /**
151
- * 指定一个用于覆写的 CompilerOptions
152
- *
153
- * @default null
154
- */
155
- compilerOptions?: ts.CompilerOptions | null
156
-
157
- /**
158
- * 指定 tsconfig.json 的路径
159
- *
160
- * 插件也会解析 tsconfig.json 的 include 和 exclude 选项
161
- *
162
- * 未指定时插件默认从根目录寻找配置
163
- */
164
- tsconfigPath?: string
165
-
166
- /**
167
- * 设置在转换别名时哪些路径需要排除
168
- *
169
- * @default []
170
- */
171
- aliasesExclude?: (string | RegExp)[]
172
-
173
- /**
174
- * 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
175
- *
176
- * @default false
177
- */
178
- cleanVueFileName?: boolean
179
-
180
- /**
181
- * 是否将动态引入转换为静态
182
- *
183
- * 开启 `rollupTypes` 时强制为 `true`
184
- *
185
- * 例如将 `import('vue').DefineComponent` 转换为 `import { DefineComponent } from 'vue'`
186
- *
187
- * @default false
188
- */
189
- staticImport?: boolean
190
-
191
- /**
192
- * 手动设置包含路径的 glob
193
- *
194
- * 默认基于 tsconfig.json 的 `include` 选项
195
- */
196
- include?: string | string[]
197
-
198
- /**
199
- * 手动设置排除路径的 glob
200
- *
201
- * 默认基于 tsconfig.json 的 `exclude` 选线,未设置时为 `'node_module/**'`
202
- */
203
- exclude?: string | string[]
204
-
205
- /**
206
- * 是否移除那些 `import 'xxx'`
207
- *
208
- * @default true
209
- */
210
- clearPureImport?: boolean
211
-
212
- /**
213
- * 是否生成类型声明入口
214
- *
215
- * 当为 `true` 时会基于 package.json 的 types 字段生成,或者 `${outDir}/index.d.ts`
216
- *
217
- * 当开启打包类型文件时强制为 `true`
218
- *
219
- * @default false
220
- */
221
- insertTypesEntry?: boolean
222
-
223
- /**
224
- * 设置是否在发出类型文件后将其打包
225
- *
226
- * 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
227
- *
228
- * @default false
229
- */
230
- rollupTypes?: boolean
231
-
232
- /**
233
- * 设置 `@microsoft/api-extractor` 的 `bundledPackages` 选项
234
- *
235
- * @default []
236
- * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
237
- */
238
- bundledPackages?: string[]
239
-
240
- /**
241
- * 是否将源码里的 .d.ts 文件复制到 `outDir`
242
- *
243
- * @default false
244
- * @remarks 在 2.0 之前它默认为 true
245
- */
246
- copyDtsFiles?: boolean
247
-
248
- /**
249
- * 指定插件的输出等级
250
- *
251
- * 默认基于 Vite 配置的 'logLevel' 选项
252
- */
253
- logLevel?: LogLevel
254
-
255
- /**
256
- * 获取诊断信息后的钩子
257
- *
258
- * 可以根据参数 length 来判断有误类型错误
259
- *
260
- * @default () => {}
261
- */
262
- afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
263
-
264
- /**
265
- * 类型声明文件被写入前的钩子
266
- *
267
- * 可以在钩子里转换文件路径和文件内容
268
- *
269
- * 当返回 `false` 时会跳过该文件
270
- *
271
- * @default () => {}
272
- */
273
- beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
274
-
275
- /**
276
- * 构建后回调钩子
277
- *
278
- * 将会在所有类型文件被写入后调用
279
- *
280
- * @default () => {}
281
- */
282
- afterBuild?: () => void | Promise<void>
283
- }
284
- ```
285
-
286
- ## 贡献者
287
-
288
- 感谢他们的所做的一切贡献!
289
-
290
- <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
291
- <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
292
- </a>
293
-
294
- ## 示例
295
-
296
- 克隆项目然后执行下列命令:
297
-
298
- ```sh
299
- pnpm run test:ts
300
- ```
301
-
302
- 然后检查 `examples/ts/types` 目录。
303
-
304
- `examples` 目录下同样有 Vue 和 React 的案例。
305
-
306
- 一个使用该插件的真实项目:[Vexip UI](https://github.com/vexip-ui/vexip-ui)。
307
-
308
- ## 授权
309
-
310
- MIT 授权。
1
+ <h1 align="center">vite-plugin-dts</h1>
2
+
3
+ <p align="center">
4
+ 一款用于在 <a href="https://cn.vitejs.dev/guide/build.html#library-mode">库模式</a> 中从 <code>.ts(x)</code> 或 <code>.vue</code> 源文件生成类型文件(<code>*.d.ts</code>)的 Vite 插件。
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://www.npmjs.com/package/vite-plugin-dts">
9
+ <img src="https://img.shields.io/npm/v/vite-plugin-dts?color=orange&label=" alt="version" />
10
+ </a>
11
+ </p>
12
+
13
+ **中文** | [English](./README.md)
14
+
15
+ ## 安装
16
+
17
+ ```sh
18
+ pnpm i vite-plugin-dts -D
19
+ ```
20
+
21
+ ## 使用
22
+
23
+ 在 `vite.config.ts`:
24
+
25
+ ```ts
26
+ import { resolve } from 'path'
27
+ import { defineConfig } from 'vite'
28
+ import dts from 'vite-plugin-dts'
29
+
30
+ export default defineConfig({
31
+ build: {
32
+ lib: {
33
+ entry: resolve(__dirname, 'src/index.ts'),
34
+ name: 'MyLib',
35
+ formats: ['es'],
36
+ fileName: 'my-lib'
37
+ }
38
+ },
39
+ plugins: [dts()]
40
+ })
41
+ ```
42
+
43
+ 在你的组件中:
44
+
45
+ ```vue
46
+ <template>
47
+ <div></div>
48
+ </template>
49
+
50
+ <script lang="ts">
51
+ // 使用 defineComponent 来进行类型推断
52
+ import { defineComponent } from 'vue'
53
+
54
+ export default defineComponent({
55
+ name: 'Component'
56
+ })
57
+ </script>
58
+ ```
59
+
60
+ ```vue
61
+ <script setup lang="ts">
62
+ defineProps<{
63
+ color: 'blue' | 'red'
64
+ }>()
65
+ </script>
66
+
67
+ <template>
68
+ <div>{{ color }}</div>
69
+ </template>
70
+ ```
71
+
72
+ ## 常见问题
73
+
74
+ 此处将收录一些常见的问题并提供一些解决方案。
75
+
76
+ ### 打包后出现类型文件缺失 (`1.7.0` 之前)
77
+
78
+ 默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
79
+
80
+ 如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
81
+
82
+ ### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误(`3.0.0` 之前)
83
+
84
+ 这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
85
+
86
+ 这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
87
+
88
+ ### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
89
+
90
+ 这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
91
+
92
+ ```json
93
+ {
94
+ "compilerOptions": {
95
+ "baseUrl": ".",
96
+ "paths": {
97
+ "third-lib": ["node_modules/third-lib"]
98
+ }
99
+ }
100
+ }
101
+ ```
102
+
103
+ ## 选项
104
+
105
+ ```ts
106
+ import type ts from 'typescript'
107
+ import type { LogLevel } from 'vite'
108
+
109
+ interface TransformWriteFile {
110
+ filePath?: string
111
+ content?: string
112
+ }
113
+
114
+ export interface PluginOptions {
115
+ /**
116
+ * 指定根目录
117
+ *
118
+ * 默认基于 Vite 配置的 'root',使用 Rollup 时基于 `process.cwd()`
119
+ */
120
+ root?: string
121
+
122
+ /**
123
+ * 指定输出目录
124
+ *
125
+ * 可以指定一个数组来输出到多个目录中
126
+ *
127
+ * 默认基于 Vite 配置的 'build.outDir',使用 Rollup 时基于 tsconfig.json 的 `outDir`
128
+ */
129
+ outDir?: string | string[]
130
+
131
+ /**
132
+ * 用于手动设置入口文件的根路径,通常用在 monorepo
133
+ *
134
+ * 在计算每个文件的输出路径时将基于该路径
135
+ *
136
+ * 默认为所有文件的最小公共路径
137
+ */
138
+ entryRoot?: string
139
+
140
+ /**
141
+ * 严格限制类型文件生产在 `outDir` 内
142
+ *
143
+ * 由于当指定了 `entryRoot` 时,类型文件有可能位于 `outDir` 之外
144
+ *
145
+ * @default true
146
+ */
147
+ strictOutput?: boolean
148
+
149
+ /**
150
+ * 指定一个用于覆写的 CompilerOptions
151
+ *
152
+ * @default null
153
+ */
154
+ compilerOptions?: ts.CompilerOptions | null
155
+
156
+ /**
157
+ * 指定 tsconfig.json 的路径
158
+ *
159
+ * 插件也会解析 tsconfig.json 的 include 和 exclude 选项
160
+ *
161
+ * 未指定时插件默认从根目录寻找配置
162
+ */
163
+ tsconfigPath?: string
164
+
165
+ /**
166
+ * 设置在转换别名时哪些路径需要排除
167
+ *
168
+ * @default []
169
+ */
170
+ aliasesExclude?: (string | RegExp)[]
171
+
172
+ /**
173
+ * 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
174
+ *
175
+ * @default false
176
+ */
177
+ cleanVueFileName?: boolean
178
+
179
+ /**
180
+ * 是否将动态引入转换为静态
181
+ *
182
+ * 开启 `rollupTypes` 时强制为 `true`
183
+ *
184
+ * 例如将 `import('vue').DefineComponent` 转换为 `import { DefineComponent } from 'vue'`
185
+ *
186
+ * @default false
187
+ */
188
+ staticImport?: boolean
189
+
190
+ /**
191
+ * 手动设置包含路径的 glob
192
+ *
193
+ * 默认基于 tsconfig.json 的 `include` 选项
194
+ */
195
+ include?: string | string[]
196
+
197
+ /**
198
+ * 手动设置排除路径的 glob
199
+ *
200
+ * 默认基于 tsconfig.json 的 `exclude` 选线,未设置时为 `'node_module/**'`
201
+ */
202
+ exclude?: string | string[]
203
+
204
+ /**
205
+ * 是否移除那些 `import 'xxx'`
206
+ *
207
+ * @default true
208
+ */
209
+ clearPureImport?: boolean
210
+
211
+ /**
212
+ * 是否生成类型声明入口
213
+ *
214
+ * 当为 `true` 时会基于 package.json 的 types 字段生成,或者 `${outDir}/index.d.ts`
215
+ *
216
+ * 当开启打包类型文件时强制为 `true`
217
+ *
218
+ * @default false
219
+ */
220
+ insertTypesEntry?: boolean
221
+
222
+ /**
223
+ * 设置是否在发出类型文件后将其打包
224
+ *
225
+ * 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
226
+ *
227
+ * @default false
228
+ */
229
+ rollupTypes?: boolean
230
+
231
+ /**
232
+ * 设置 `@microsoft/api-extractor` 的 `bundledPackages` 选项
233
+ *
234
+ * @default []
235
+ * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
236
+ */
237
+ bundledPackages?: string[]
238
+
239
+ /**
240
+ * 是否将源码里的 .d.ts 文件复制到 `outDir`
241
+ *
242
+ * @default false
243
+ * @remarks 在 2.0 之前它默认为 true
244
+ */
245
+ copyDtsFiles?: boolean
246
+
247
+ /**
248
+ * 指定插件的输出等级
249
+ *
250
+ * 默认基于 Vite 配置的 'logLevel' 选项
251
+ */
252
+ logLevel?: LogLevel
253
+
254
+ /**
255
+ * 获取诊断信息后的钩子
256
+ *
257
+ * 可以根据参数 length 来判断有误类型错误
258
+ *
259
+ * @default () => {}
260
+ */
261
+ afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
262
+
263
+ /**
264
+ * 类型声明文件被写入前的钩子
265
+ *
266
+ * 可以在钩子里转换文件路径和文件内容
267
+ *
268
+ * 当返回 `false` 时会跳过该文件
269
+ *
270
+ * @default () => {}
271
+ */
272
+ beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
273
+
274
+ /**
275
+ * 构建后回调钩子
276
+ *
277
+ * 将会在所有类型文件被写入后调用
278
+ *
279
+ * @default () => {}
280
+ */
281
+ afterBuild?: () => void | Promise<void>
282
+ }
283
+ ```
284
+
285
+ ## 贡献者
286
+
287
+ 感谢他们的所做的一切贡献!
288
+
289
+ <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
290
+ <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
291
+ </a>
292
+
293
+ ## 示例
294
+
295
+ 克隆项目然后执行下列命令:
296
+
297
+ ```sh
298
+ pnpm run test:ts
299
+ ```
300
+
301
+ 然后检查 `examples/ts/types` 目录。
302
+
303
+ `examples` 目录下同样有 Vue 和 React 的案例。
304
+
305
+ 一个使用该插件的真实项目:[Vexip UI](https://github.com/vexip-ui/vexip-ui)。
306
+
307
+ ## 授权
308
+
309
+ MIT 授权。