vite-plugin-dts 1.7.0 → 1.7.1

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 CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
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
6
 
7
+ > **Notice**: `skipDiagnostics` option default to `false` since 1.7.0.
8
+
7
9
  ## Install
8
10
 
9
11
  ```sh
@@ -63,6 +65,37 @@ const props = defineProps<{
63
65
  </template>
64
66
  ```
65
67
 
68
+ ## FAQ
69
+
70
+ Here are some FAQ's and solutions.
71
+
72
+ ### Missing some declaration files after build
73
+
74
+ 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).
75
+
76
+ 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.
77
+
78
+ ### Take type error when using both `script` and `setup-script` in vue component
79
+
80
+ 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.
81
+
82
+ 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.
83
+
84
+ ### Take errors that unable to infer types from packages which under `node_modules`
85
+
86
+ 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:
87
+
88
+ ```json
89
+ {
90
+ "compilerOptions": {
91
+ "baseUrl": ".",
92
+ "paths": {
93
+ "third-lib": ["node_modules/third-lib"]
94
+ }
95
+ }
96
+ }
97
+ ```
98
+
66
99
  ## Options
67
100
 
68
101
  ```ts
@@ -74,96 +107,186 @@ interface TransformWriteFile {
74
107
  }
75
108
 
76
109
  export interface PluginOptions {
77
- // Depends on the root directory
78
- // Defaults base on your vite config root options
110
+ /**
111
+ * Depends on the root directory
112
+ *
113
+ * Defaults base on your vite config root options
114
+ */
79
115
  root?: string
80
116
 
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
117
+ /**
118
+ * Declaration files output directory
119
+ *
120
+ * Can be specified a array to output to multiple directories
121
+ *
122
+ * Defaults base on your vite config output options
123
+ */
84
124
  outputDir?: string | string[]
85
125
 
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
126
+ /**
127
+ * Manually set the root path of the entry files
128
+ *
129
+ * The output path of each file will be caculated base on it
130
+ *
131
+ * Defaults is the smallest public path for all files
132
+ */
89
133
  entryRoot?: string
90
134
 
91
- // Project init compilerOptions using by ts-morph
92
- // Default: null
135
+ /**
136
+ * Project init compilerOptions using by ts-morph
137
+ *
138
+ * @default null
139
+ */
93
140
  compilerOptions?: ts.CompilerOptions | null
94
141
 
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'
142
+ /**
143
+ * Project init tsconfig.json file path by ts-morph
144
+ *
145
+ * Plugin also resolve incldue and exclude files from tsconfig.json
146
+ *
147
+ * @default 'tsconfig.json'
148
+ */
98
149
  tsConfigFilePath?: string
99
150
 
100
- // Set which paths should exclude when transform aliases
101
- // If it's regexp, it will test the original import path directly
102
- // Default: []
151
+ /**
152
+ * Set which paths should exclude when transform aliases
153
+ *
154
+ * If it's regexp, it will test the original import path directly
155
+ *
156
+ * @default []
157
+ */
103
158
  aliasesExclude?: (string | RegExp)[]
104
159
 
105
- // Whether transform file name '.vue.d.ts' to '.d.ts'
106
- // Default: false
160
+ /**
161
+ * Whether transform file name '.vue.d.ts' to '.d.ts'
162
+ *
163
+ * @default false
164
+ */
107
165
  cleanVueFileName?: boolean
108
166
 
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
167
+ /**
168
+ * Whether transform dynamic import to static
169
+ *
170
+ * Force true when `rollupTypes` is effective
171
+ *
172
+ * eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
173
+ *
174
+ * @default false
175
+ */
113
176
  staticImport?: boolean
114
177
 
115
- // Manual set include glob
116
- // Defaults base on your tsconfig.json include option
178
+ /**
179
+ * Manual set include glob
180
+ *
181
+ * Defaults base on your tsconfig.json include option
182
+ */
117
183
  include?: string | string[]
118
184
 
119
- // Manual set exclude glob
120
- // Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
185
+ /**
186
+ * Manual set exclude glob
187
+ *
188
+ * Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
189
+ */
121
190
  exclude?: string | string[]
122
191
 
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
192
+ /**
193
+ * Do not emit if content of file only includes 'export {}'
194
+ *
195
+ * @default true
196
+ */
197
+ clearPureImport?: boolean
198
+
199
+ /**
200
+ * Whether generate types entry file
201
+ *
202
+ * When true will from package.json types field if exists or `${outputDir}/index.d.ts`
203
+ *
204
+ * Force true when `rollupTypes` is effective
205
+ *
206
+ * @default false
207
+ */
127
208
  insertTypesEntry?: boolean
128
209
 
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
210
+ /**
211
+ * Set to rollup declaration files after emit
212
+ *
213
+ * Power by `@microsoft/api-extractor`, it will start a new program which takes some time
214
+ *
215
+ * @default false
216
+ */
132
217
  rollupTypes?: boolean
133
218
 
134
- // Whether copy .d.ts source files into outputDir
135
- // Default: true
219
+ /**
220
+ * Whether copy .d.ts source files into outputDir
221
+ *
222
+ * @default true
223
+ */
136
224
  copyDtsFiles?: boolean
137
225
 
138
- // Whether emit nothing when has any diagnostic
139
- // Default: false
226
+ /**
227
+ * Whether emit nothing when has any diagnostic
228
+ *
229
+ * @default false
230
+ */
140
231
  noEmitOnError?: boolean
141
232
 
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
233
+ /**
234
+ * Whether skip typescript diagnostics
235
+ *
236
+ * Skip type diagnostics means that type errors will not interrupt the build process
237
+ *
238
+ * But for the source files with type errors will not be emitted
239
+ *
240
+ * @default false
241
+ */
146
242
  skipDiagnostics?: boolean
147
243
 
148
- // Whether log diagnostic informations
149
- // Not effective when `skipDiagnostics` is true
150
- // Default: false
244
+ /**
245
+ * Whether log diagnostic informations
246
+ *
247
+ * Not effective when `skipDiagnostics` is true
248
+ *
249
+ * @deprecated
250
+ * @default false
251
+ */
151
252
  logDiagnostics?: boolean
152
253
 
153
- // After emit diagnostic hook
154
- // According to the length to judge whether there is any type error
155
- // Default: () => {}
254
+ /**
255
+ * Customize typescript lib folder path
256
+ *
257
+ * Should pass a relative path to root or a absolute path
258
+ *
259
+ * @default undefined
260
+ */
261
+ libFolderPath?: string
262
+
263
+ /**
264
+ * After emit diagnostic hook
265
+ *
266
+ * According to the length to judge whether there is any type error
267
+ *
268
+ * @default () => {}
269
+ */
156
270
  afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
157
271
 
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: () => {}
272
+ /**
273
+ * Before declaration file be writed hook
274
+ *
275
+ * You can transform declaration file-path and content through it
276
+ *
277
+ * The file will be skipped when return exact false
278
+ *
279
+ * @default () => {}
280
+ */
162
281
  beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
163
282
 
164
- // After build hook
165
- // It wil be called after all declaration files are written
166
- // Default: () => {}
283
+ /**
284
+ * After build hook
285
+ *
286
+ * It wil be called after all declaration files are written
287
+ *
288
+ * @default () => {}
289
+ */
167
290
  afterBuild?: () => void | Promise<void>
168
291
  }
169
292
  ```
@@ -178,37 +301,6 @@ pnpm run test:e2e
178
301
 
179
302
  Then check `example/types`.
180
303
 
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
304
  ## License
213
305
 
214
306
  MIT License.
package/README.zh-CN.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  一款用于在 [库模式](https://cn.vitejs.dev/guide/build.html#library-mode) 中,从 `.ts(x)` 或 `.vue` 源文件生成类型文件(`.d.ts`)的 Vite 插件。
6
6
 
7
+ > **注意**: 从 1.7.0 开始 `skipDiagnostics` 选项默认为 `false`。
8
+
7
9
  ## 安装
8
10
 
9
11
  ```sh
@@ -62,6 +64,37 @@ const props = defineProps<{
62
64
  </template>
63
65
  ```
64
66
 
67
+ ## 常见问题
68
+
69
+ 此处将收录一些常见的问题并提供一些解决方案。
70
+
71
+ ### 打包后出现类型文件缺失
72
+
73
+ 默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
74
+
75
+ 如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
76
+
77
+ ### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
78
+
79
+ 这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
80
+
81
+ 这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
82
+
83
+ ### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
84
+
85
+ 这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
86
+
87
+ ```json
88
+ {
89
+ "compilerOptions": {
90
+ "baseUrl": ".",
91
+ "paths": {
92
+ "third-lib": ["node_modules/third-lib"]
93
+ }
94
+ }
95
+ }
96
+ ```
97
+
65
98
  ## 选项
66
99
 
67
100
  ```ts
@@ -73,96 +106,186 @@ interface TransformWriteFile {
73
106
  }
74
107
 
75
108
  export interface PluginOptions {
76
- // 执行的根目录
77
- // 默认基于 vite 配置的 root 选项
109
+ /**
110
+ * 执行的根目录
111
+ *
112
+ * 默认基于 vite 配置的 root 选项
113
+ */
78
114
  root?: string
79
115
 
80
- // 声明文件的输出目录
81
- // 可以指定一个数组来输出到多个目录中
82
- // 默认基于 vite 配置的输出目录
116
+ /**
117
+ * 声明文件的输出目录
118
+ *
119
+ * 可以指定一个数组来输出到多个目录中
120
+ *
121
+ * 默认基于 vite 配置的输出目录
122
+ */
83
123
  outputDir?: string | string[]
84
124
 
85
- // 用于手动设置入口文件的根路径
86
- // 在计算每个文件的输出路径时将基于该路径
87
- // 默认为所有文件的最小公共路径
125
+ /**
126
+ * 用于手动设置入口文件的根路径
127
+ *
128
+ * 在计算每个文件的输出路径时将基于该路径
129
+ *
130
+ * 默认为所有文件的最小公共路径
131
+ */
88
132
  entryRoot?: string
89
133
 
90
- // 提供给 ts-morph Project 初始化的 compilerOptions 选项
91
- // 默认值: null
134
+ /**
135
+ * 提供给 ts-morph Project 初始化的 compilerOptions 选项
136
+ *
137
+ * @default null
138
+ */
92
139
  compilerOptions?: ts.CompilerOptions | null
93
140
 
94
- // 提供给 ts-morph Project 初始化的 tsconfig.json 路径
95
- // 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
96
- // 默认值: 'tsconfig.json'
141
+ /**
142
+ * 提供给 ts-morph Project 初始化的 tsconfig.json 路径
143
+ *
144
+ * 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
145
+ *
146
+ * @default 'tsconfig.json'
147
+ */
97
148
  tsConfigFilePath?: string
98
149
 
99
- // 设置在转换别名时哪些路径需要排除
100
- // 如果为正则,会直接使用 test 和原始路径进行比较
101
- // 默认值: []
150
+ /**
151
+ * 设置在转换别名时哪些路径需要排除
152
+ *
153
+ * 如果为正则,会直接使用 test 和原始路径进行比较
154
+ *
155
+ * @default []
156
+ */
102
157
  aliasesExclude?: (string | RegExp)[]
103
158
 
104
- // 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
105
- // 默认值: false
159
+ /**
160
+ * 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
161
+ *
162
+ * @default false
163
+ */
106
164
  cleanVueFileName?: boolean
107
165
 
108
- //是否将动态引入转换为静态
109
- // 当开启打包类型文件时强制为 true
110
- // eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
111
- // 默认值: false
166
+ /**
167
+ * 是否将动态引入转换为静态
168
+ *
169
+ * 当开启打包类型文件时强制为 true
170
+ *
171
+ * 例如将 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
172
+ *
173
+ * @default false
174
+ */
112
175
  staticImport?: boolean
113
176
 
114
- // 手动设置包含路径的 glob
115
- // 默认基于 tsconfig.json 的 include 选项
177
+ /**
178
+ * 手动设置包含路径的 glob
179
+ *
180
+ * 默认基于 tsconfig.json 的 include 选项
181
+ */
116
182
  include?: string | string[]
117
183
 
118
- // 手动设置排除路径的 glob
119
- // 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
184
+ /**
185
+ * 手动设置排除路径的 glob
186
+ *
187
+ * 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
188
+ */
120
189
  exclude?: string | string[]
121
190
 
122
- // 是否生成类型声明入口
123
- // 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
124
- // 当开启打包类型文件时强制为 true
125
- // 默认值: false
191
+ /**
192
+ * 如果文件内容仅包含 'export {}' 则跳过生成
193
+ *
194
+ * @default true
195
+ */
196
+ clearPureImport?: boolean
197
+
198
+ /**
199
+ * 是否生成类型声明入口
200
+ *
201
+ * 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
202
+ *
203
+ * 当开启打包类型文件时强制为 true
204
+ *
205
+ * @default false
206
+ */
126
207
  insertTypesEntry?: boolean
127
208
 
128
- // 设置是否在发出类型文件后将其打包
129
- // 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
130
- // 默认值: false
209
+ /**
210
+ * 设置是否在发出类型文件后将其打包
211
+ *
212
+ * 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
213
+ *
214
+ * @default false
215
+ */
131
216
  rollupTypes?: boolean
132
217
 
133
- // 是否将源码里的 .d.ts 文件复制到 outputDir
134
- // 默认值: true
218
+ /**
219
+ * 是否将源码里的 .d.ts 文件复制到 outputDir
220
+ *
221
+ * @default true
222
+ */
135
223
  copyDtsFiles?: boolean
136
224
 
137
- // 出现类型诊断信息时不生成类型文件
138
- // 默认值: false
225
+ /**
226
+ * 出现类型诊断信息时不生成类型文件
227
+ *
228
+ * @default false
229
+ */
139
230
  noEmitOnError?: boolean
140
231
 
141
- // 是否跳过类型诊断
142
- // 跳过类型诊断意味着出现错误时不会中断打包进程的执行
143
- // 但对于出现错误的源文件,将无法生成相应的类型文件
144
- // 默认值: true
232
+ /**
233
+ * 是否跳过类型诊断
234
+ *
235
+ * 跳过类型诊断意味着出现错误时不会中断打包进程的执行
236
+ *
237
+ * 但对于出现错误的源文件,将无法生成相应的类型文件
238
+ *
239
+ * @default false
240
+ */
145
241
  skipDiagnostics?: boolean
146
242
 
147
- // 是否打印类型诊断信息
148
- // 当跳过类型诊断时该属性将不会生效
149
- // 默认值: false
243
+ /**
244
+ * 是否打印类型诊断信息
245
+ *
246
+ * 当跳过类型诊断时该属性将不会生效
247
+ *
248
+ * @deprecated
249
+ * @default false
250
+ */
150
251
  logDiagnostics?: boolean
151
252
 
152
- // 获取诊断信息后的钩子
153
- // 可以根据参数 length 来判断有误类型错误
154
- // 默认值: () => {}
253
+ /**
254
+ * 定制 typescript 的 lib 文件夹路径
255
+ *
256
+ * 应传入一个 root 的相对路径或一个绝对路径
257
+ *
258
+ * @default undefined
259
+ */
260
+ libFolderPath?: string
261
+
262
+ /**
263
+ * 获取诊断信息后的钩子
264
+ *
265
+ * 可以根据参数 length 来判断有误类型错误
266
+ *
267
+ * @default () => {}
268
+ */
155
269
  afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
156
270
 
157
- // 类型声明文件被写入前的钩子
158
- // 可以在钩子里转换文件路径和文件内容
159
- // 当返回 false 时会跳过该文件
160
- // 默认值: () => {}
271
+ /**
272
+ * 类型声明文件被写入前的钩子
273
+ *
274
+ * 可以在钩子里转换文件路径和文件内容
275
+ *
276
+ * 当返回 false 时会跳过该文件
277
+ *
278
+ * @default () => {}
279
+ */
161
280
  beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
162
281
 
163
- // 构建后回调钩子
164
- // 将会在所有类型文件被写入后调用
165
- // 默认值: () => {}
282
+ /**
283
+ * 构建后回调钩子
284
+ *
285
+ * 将会在所有类型文件被写入后调用
286
+ *
287
+ * @default () => {}
288
+ */
166
289
  afterBuild?: () => void | Promise<void>
167
290
  }
168
291
  ```
@@ -177,37 +300,6 @@ pnpm run test:e2e
177
300
 
178
301
  然后检查 `example/types` 目录。
179
302
 
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
303
  ## 授权
212
304
 
213
305
  MIT 授权。
package/dist/index.cjs CHANGED
@@ -12,6 +12,7 @@ const require$$1$1 = require('fs');
12
12
  const require$$1 = require('path');
13
13
  const require$$5 = require('buffer');
14
14
  const require$$7 = require('inspector');
15
+ const pluginutils = require('@rollup/pluginutils');
15
16
  const node_fs = require('node:fs');
16
17
  const node_module = require('node:module');
17
18
  const apiExtractor = require('@microsoft/api-extractor');
@@ -176864,13 +176865,12 @@ function rollupDeclarationFiles({
176864
176865
  }
176865
176866
 
176866
176867
  const noneExport = "export {};\n";
176867
- const virtualPrefix = "\0";
176868
176868
  const vueRE = /\.vue$/;
176869
- const tsRE = /\.tsx?$/;
176870
- const jsRE = /\.jsx?$/;
176869
+ const tsRE = /\.(m|c)?tsx?$/;
176870
+ const jsRE = /\.(m|c)?jsx?$/;
176871
176871
  const dtsRE = /\.d\.tsx?$/;
176872
- const tjsRE = /\.(t|j)sx?$/;
176873
- const watchExtensionRE = /\.(vue|(t|j)sx?)$/;
176872
+ const tjsRE = /\.(m|c)?(t|j)sx?$/;
176873
+ const watchExtensionRE = /\.(vue|(m|c)?(t|j)sx?)$/;
176874
176874
  const fullRelativeRE = /^\.\.?\//;
176875
176875
  const defaultIndex = "index.d.ts";
176876
176876
  const noop = () => {
@@ -176907,6 +176907,9 @@ function dtsPlugin(options = {}) {
176907
176907
  let tsConfigPath;
176908
176908
  let outputDirs;
176909
176909
  let isBundle = false;
176910
+ let include;
176911
+ let exclude;
176912
+ let filter;
176910
176913
  const sourceDtsFiles = /* @__PURE__ */ new Set();
176911
176914
  let hasJsVue = false;
176912
176915
  let allowJs = false;
@@ -177001,6 +177004,16 @@ ${kolorist.cyan(
177001
177004
  libFolderPath: libFolderPath ? ensureAbsolute(libFolderPath, root) : void 0
177002
177005
  });
177003
177006
  allowJs = project.getCompilerOptions().allowJs ?? false;
177007
+ const tsConfig = typescript.readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
177008
+ const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
177009
+ const parentTsConfig = parentTsConfigPath ? typescript.readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
177010
+ include = ensureArray(
177011
+ options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*"
177012
+ ).map(normalizeGlob);
177013
+ exclude = ensureArray(
177014
+ options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**"
177015
+ ).map(normalizeGlob);
177016
+ filter = pluginutils.createFilter(include, exclude, { resolve: root });
177004
177017
  },
177005
177018
  buildStart(inputOptions) {
177006
177019
  if (Array.isArray(inputOptions.input)) {
@@ -177013,7 +177026,7 @@ ${kolorist.cyan(
177013
177026
  }
177014
177027
  },
177015
177028
  transform(code, id) {
177016
- if (id.startsWith(virtualPrefix)) {
177029
+ if (!filter(id)) {
177017
177030
  return null;
177018
177031
  }
177019
177032
  if (vueRE.test(id)) {
@@ -177024,7 +177037,7 @@ ${kolorist.cyan(
177024
177037
  project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
177025
177038
  }
177026
177039
  } else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
177027
- project.addSourceFileAtPath(id);
177040
+ project.createSourceFile(id, code, { overwrite: true });
177028
177041
  }
177029
177042
  return null;
177030
177043
  },
@@ -177046,18 +177059,12 @@ ${logPrefix} Start generate declaration files...`));
177046
177059
  isBundle = true;
177047
177060
  sourceDtsFiles.clear();
177048
177061
  const startTime = Date.now();
177049
- const tsConfig = typescript.readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
177050
- const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
177051
- const parentTsConfig = parentTsConfigPath ? typescript.readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
177052
- const include = options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*";
177053
- const exclude = options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**";
177054
- bundleDebug("read config");
177055
177062
  const includedFileSet = /* @__PURE__ */ new Set();
177056
177063
  if (include && include.length) {
177057
- const files = await glob(ensureArray(include).map(normalizeGlob), {
177064
+ const files = await glob(include, {
177058
177065
  cwd: root,
177059
177066
  absolute: true,
177060
- ignore: ensureArray(exclude).map(normalizeGlob)
177067
+ ignore: exclude
177061
177068
  });
177062
177069
  files.forEach((file) => {
177063
177070
  if (dtsRE.test(file)) {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Plugin } from 'vite';
1
+ import * as vite from 'vite';
2
2
  import { ts, Diagnostic } from 'ts-morph';
3
3
 
4
4
  interface TransformWriteFile {
@@ -6,28 +6,169 @@ interface TransformWriteFile {
6
6
  content?: string;
7
7
  }
8
8
  interface PluginOptions {
9
- include?: string | string[];
10
- exclude?: string | string[];
9
+ /**
10
+ * Depends on the root directory
11
+ *
12
+ * Defaults base on your vite config root options
13
+ */
11
14
  root?: string;
15
+ /**
16
+ * Declaration files output directory
17
+ *
18
+ * Can be specified a array to output to multiple directories
19
+ *
20
+ * Defaults base on your vite config output options
21
+ */
12
22
  outputDir?: string | string[];
23
+ /**
24
+ * Manually set the root path of the entry files
25
+ *
26
+ * The output path of each file will be caculated base on it
27
+ *
28
+ * Defaults is the smallest public path for all files
29
+ */
13
30
  entryRoot?: string;
31
+ /**
32
+ * Project init compilerOptions using by ts-morph
33
+ *
34
+ * @default null
35
+ */
14
36
  compilerOptions?: ts.CompilerOptions | null;
37
+ /**
38
+ * Project init tsconfig.json file path by ts-morph
39
+ *
40
+ * Plugin also resolve incldue and exclude files from tsconfig.json
41
+ *
42
+ * @default 'tsconfig.json'
43
+ */
15
44
  tsConfigFilePath?: string;
45
+ /**
46
+ * Set which paths should exclude when transform aliases
47
+ *
48
+ * If it's regexp, it will test the original import path directly
49
+ *
50
+ * @default []
51
+ */
16
52
  aliasesExclude?: (string | RegExp)[];
53
+ /**
54
+ * Whether transform file name '.vue.d.ts' to '.d.ts'
55
+ *
56
+ * @default false
57
+ */
17
58
  cleanVueFileName?: boolean;
59
+ /**
60
+ * Whether transform dynamic import to static
61
+ *
62
+ * Force true when `rollupTypes` is effective
63
+ *
64
+ * eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
65
+ *
66
+ * @default false
67
+ */
18
68
  staticImport?: boolean;
69
+ /**
70
+ * Manual set include glob
71
+ *
72
+ * Defaults base on your tsconfig.json include option
73
+ */
74
+ include?: string | string[];
75
+ /**
76
+ * Manual set exclude glob
77
+ *
78
+ * Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
79
+ */
80
+ exclude?: string | string[];
81
+ /**
82
+ * Do not emit if content of file only includes 'export {}'
83
+ *
84
+ * @default true
85
+ */
19
86
  clearPureImport?: boolean;
87
+ /**
88
+ * Whether generate types entry file
89
+ *
90
+ * When true will from package.json types field if exists or `${outputDir}/index.d.ts`
91
+ *
92
+ * Force true when `rollupTypes` is effective
93
+ *
94
+ * @default false
95
+ */
20
96
  insertTypesEntry?: boolean;
97
+ /**
98
+ * Set to rollup declaration files after emit
99
+ *
100
+ * Power by `@microsoft/api-extractor`, it will start a new program which takes some time
101
+ *
102
+ * @default false
103
+ */
21
104
  rollupTypes?: boolean;
105
+ /**
106
+ * Whether copy .d.ts source files into outputDir
107
+ *
108
+ * @default true
109
+ */
22
110
  copyDtsFiles?: boolean;
111
+ /**
112
+ * Whether emit nothing when has any diagnostic
113
+ *
114
+ * @default false
115
+ */
23
116
  noEmitOnError?: boolean;
117
+ /**
118
+ * Whether skip typescript diagnostics
119
+ *
120
+ * Skip type diagnostics means that type errors will not interrupt the build process
121
+ *
122
+ * But for the source files with type errors will not be emitted
123
+ *
124
+ * @default false
125
+ */
24
126
  skipDiagnostics?: boolean;
127
+ /**
128
+ * Whether log diagnostic informations
129
+ *
130
+ * Not effective when `skipDiagnostics` is true
131
+ *
132
+ * @deprecated
133
+ * @default false
134
+ */
25
135
  logDiagnostics?: boolean;
136
+ /**
137
+ * Customize typescript lib folder path
138
+ *
139
+ * Should pass a relative path to root or a absolute path
140
+ *
141
+ * @default undefined
142
+ */
26
143
  libFolderPath?: string;
144
+ /**
145
+ * After emit diagnostic hook
146
+ *
147
+ * According to the length to judge whether there is any type error
148
+ *
149
+ * @default () => {}
150
+ */
27
151
  afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>;
152
+ /**
153
+ * Before declaration file be writed hook
154
+ *
155
+ * You can transform declaration file-path and content through it
156
+ *
157
+ * The file will be skipped when return exact false
158
+ *
159
+ * @default () => {}
160
+ */
28
161
  beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile;
162
+ /**
163
+ * After build hook
164
+ *
165
+ * It wil be called after all declaration files are written
166
+ *
167
+ * @default () => {}
168
+ */
29
169
  afterBuild?: () => void | Promise<void>;
30
- }
31
- declare function dtsPlugin(options?: PluginOptions): Plugin;
170
+ }
171
+
172
+ declare function dtsPlugin(options?: PluginOptions): vite.Plugin;
32
173
 
33
- export { dtsPlugin as default };
174
+ export { PluginOptions, dtsPlugin as default };
package/dist/index.mjs CHANGED
@@ -17,6 +17,7 @@ import require$$1$1 from 'fs';
17
17
  import require$$1 from 'path';
18
18
  import require$$5 from 'buffer';
19
19
  import require$$7 from 'inspector';
20
+ import { createFilter } from '@rollup/pluginutils';
20
21
  import { existsSync, readdirSync, lstatSync, rmdirSync } from 'node:fs';
21
22
  import { createRequire } from 'node:module';
22
23
  import { ExtractorConfig, CompilerState } from '@microsoft/api-extractor';
@@ -176869,13 +176870,12 @@ function rollupDeclarationFiles({
176869
176870
  }
176870
176871
 
176871
176872
  const noneExport = "export {};\n";
176872
- const virtualPrefix = "\0";
176873
176873
  const vueRE = /\.vue$/;
176874
- const tsRE = /\.tsx?$/;
176875
- const jsRE = /\.jsx?$/;
176874
+ const tsRE = /\.(m|c)?tsx?$/;
176875
+ const jsRE = /\.(m|c)?jsx?$/;
176876
176876
  const dtsRE = /\.d\.tsx?$/;
176877
- const tjsRE = /\.(t|j)sx?$/;
176878
- const watchExtensionRE = /\.(vue|(t|j)sx?)$/;
176877
+ const tjsRE = /\.(m|c)?(t|j)sx?$/;
176878
+ const watchExtensionRE = /\.(vue|(m|c)?(t|j)sx?)$/;
176879
176879
  const fullRelativeRE = /^\.\.?\//;
176880
176880
  const defaultIndex = "index.d.ts";
176881
176881
  const noop = () => {
@@ -176912,6 +176912,9 @@ function dtsPlugin(options = {}) {
176912
176912
  let tsConfigPath;
176913
176913
  let outputDirs;
176914
176914
  let isBundle = false;
176915
+ let include;
176916
+ let exclude;
176917
+ let filter;
176915
176918
  const sourceDtsFiles = /* @__PURE__ */ new Set();
176916
176919
  let hasJsVue = false;
176917
176920
  let allowJs = false;
@@ -177006,6 +177009,16 @@ ${cyan(
177006
177009
  libFolderPath: libFolderPath ? ensureAbsolute(libFolderPath, root) : void 0
177007
177010
  });
177008
177011
  allowJs = project.getCompilerOptions().allowJs ?? false;
177012
+ const tsConfig = typescript.readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
177013
+ const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
177014
+ const parentTsConfig = parentTsConfigPath ? typescript.readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
177015
+ include = ensureArray(
177016
+ options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*"
177017
+ ).map(normalizeGlob);
177018
+ exclude = ensureArray(
177019
+ options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**"
177020
+ ).map(normalizeGlob);
177021
+ filter = createFilter(include, exclude, { resolve: root });
177009
177022
  },
177010
177023
  buildStart(inputOptions) {
177011
177024
  if (Array.isArray(inputOptions.input)) {
@@ -177018,7 +177031,7 @@ ${cyan(
177018
177031
  }
177019
177032
  },
177020
177033
  transform(code, id) {
177021
- if (id.startsWith(virtualPrefix)) {
177034
+ if (!filter(id)) {
177022
177035
  return null;
177023
177036
  }
177024
177037
  if (vueRE.test(id)) {
@@ -177029,7 +177042,7 @@ ${cyan(
177029
177042
  project.createSourceFile(`${id}.${ext || "js"}`, content, { overwrite: true });
177030
177043
  }
177031
177044
  } else if (!id.includes(".vue?vue") && (tsRE.test(id) || allowJs && jsRE.test(id))) {
177032
- project.addSourceFileAtPath(id);
177045
+ project.createSourceFile(id, code, { overwrite: true });
177033
177046
  }
177034
177047
  return null;
177035
177048
  },
@@ -177051,18 +177064,12 @@ ${logPrefix} Start generate declaration files...`));
177051
177064
  isBundle = true;
177052
177065
  sourceDtsFiles.clear();
177053
177066
  const startTime = Date.now();
177054
- const tsConfig = typescript.readConfigFile(tsConfigPath, project.getFileSystem().readFileSync).config ?? {};
177055
- const parentTsConfigPath = tsConfig.extends && ensureAbsolute(tsConfig.extends, root);
177056
- const parentTsConfig = parentTsConfigPath ? typescript.readConfigFile(parentTsConfigPath, project.getFileSystem().readFileSync).config : {};
177057
- const include = options.include ?? tsConfig.include ?? parentTsConfig.include ?? "**/*";
177058
- const exclude = options.exclude ?? tsConfig.exclude ?? parentTsConfig.exclude ?? "node_modules/**";
177059
- bundleDebug("read config");
177060
177067
  const includedFileSet = /* @__PURE__ */ new Set();
177061
177068
  if (include && include.length) {
177062
- const files = await glob(ensureArray(include).map(normalizeGlob), {
177069
+ const files = await glob(include, {
177063
177070
  cwd: root,
177064
177071
  absolute: true,
177065
- ignore: ensureArray(exclude).map(normalizeGlob)
177072
+ ignore: exclude
177066
177073
  });
177067
177074
  files.forEach((file) => {
177068
177075
  if (dtsRE.test(file)) {
package/package.json CHANGED
@@ -7,6 +7,7 @@
7
7
  },
8
8
  "dependencies": {
9
9
  "@microsoft/api-extractor": "^7.33.5",
10
+ "@rollup/pluginutils": "^5.0.2",
10
11
  "@rushstack/node-core-library": "^3.53.2",
11
12
  "debug": "^4.3.4",
12
13
  "fast-glob": "^3.2.12",
@@ -84,9 +85,6 @@
84
85
  "type": "git",
85
86
  "url": "git+https://github.com/qmhc/vite-plugin-dts.git"
86
87
  },
87
- "type": "module",
88
- "types": "dist/index.d.ts",
89
- "version": "1.7.0",
90
88
  "scripts": {
91
89
  "build": "tsx scripts/build.ts",
92
90
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path .",
@@ -95,10 +93,15 @@
95
93
  "lint:src": "eslint --fix --ext .js,.jsx,.ts,.tsx,.vue src/**",
96
94
  "lint:example": "eslint --fix --ext .js,.jsx,.ts,.tsx,.vue example/{src,components}/**",
97
95
  "_postinstall": "is-ci || husky install",
96
+ "postpublish": "pinst --enable",
98
97
  "precommit": "lint-staged -c ./.husky/.lintstagedrc -q",
98
+ "prepublishOnly": "pinst --disable",
99
99
  "prettier": "pretty-quick --staged",
100
100
  "release": "tsx scripts/release.ts",
101
101
  "test": "vitest run",
102
102
  "test:e2e": "pnpm -C example build"
103
- }
104
- }
103
+ },
104
+ "type": "module",
105
+ "types": "dist/index.d.ts",
106
+ "version": "1.7.1"
107
+ }