vite-plugin-dts 1.6.6 → 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
@@ -1,214 +1,306 @@
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
+ > **Notice**: `skipDiagnostics` option default to `false` since 1.7.0.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ pnpm add vite-plugin-dts -D
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ In `vite.config.ts`:
18
+
19
+ ```ts
20
+ import { resolve } from 'path'
21
+ import { defineConfig } from 'vite'
22
+ import dts from 'vite-plugin-dts'
23
+
24
+ export default defineConfig({
25
+ build: {
26
+ lib: {
27
+ entry: resolve(__dirname, 'src/index.ts'),
28
+ name: 'MyLib',
29
+ formats: ['es'],
30
+ fileName: 'my-lib'
31
+ }
32
+ },
33
+ plugins: [dts()]
34
+ })
35
+ ```
36
+
37
+ In your component:
38
+
39
+ ```vue
40
+ <template>
41
+ <div></div>
42
+ </template>
43
+
44
+ <script lang="ts">
45
+ // using defineComponent for inferring types
46
+ import { defineComponent } from 'vue'
47
+
48
+ export default defineComponent({
49
+ name: 'Component'
50
+ })
51
+ </script>
52
+ ```
53
+
54
+ ```vue
55
+ <script setup lang="ts">
56
+ // Need to access the defineProps returned value to
57
+ // infer types although you never use the props directly
58
+ const props = defineProps<{
59
+ color: 'blue' | 'red'
60
+ }>()
61
+ </script>
62
+
63
+ <template>
64
+ <div>{{ color }}</div>
65
+ </template>
66
+ ```
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
+
99
+ ## Options
100
+
101
+ ```ts
102
+ import type { ts, Diagnostic } from 'ts-morph'
103
+
104
+ interface TransformWriteFile {
105
+ filePath?: string
106
+ content?: string
107
+ }
108
+
109
+ export interface PluginOptions {
110
+ /**
111
+ * Depends on the root directory
112
+ *
113
+ * Defaults base on your vite config root options
114
+ */
115
+ root?: string
116
+
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
+ */
124
+ outputDir?: string | string[]
125
+
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
+ */
133
+ entryRoot?: string
134
+
135
+ /**
136
+ * Project init compilerOptions using by ts-morph
137
+ *
138
+ * @default null
139
+ */
140
+ compilerOptions?: ts.CompilerOptions | null
141
+
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
+ */
149
+ tsConfigFilePath?: string
150
+
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
+ */
158
+ aliasesExclude?: (string | RegExp)[]
159
+
160
+ /**
161
+ * Whether transform file name '.vue.d.ts' to '.d.ts'
162
+ *
163
+ * @default false
164
+ */
165
+ cleanVueFileName?: boolean
166
+
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
+ */
176
+ staticImport?: boolean
177
+
178
+ /**
179
+ * Manual set include glob
180
+ *
181
+ * Defaults base on your tsconfig.json include option
182
+ */
183
+ include?: string | string[]
184
+
185
+ /**
186
+ * Manual set exclude glob
187
+ *
188
+ * Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
189
+ */
190
+ exclude?: string | string[]
191
+
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
+ */
208
+ insertTypesEntry?: boolean
209
+
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
+ */
217
+ rollupTypes?: boolean
218
+
219
+ /**
220
+ * Whether copy .d.ts source files into outputDir
221
+ *
222
+ * @default true
223
+ */
224
+ copyDtsFiles?: boolean
225
+
226
+ /**
227
+ * Whether emit nothing when has any diagnostic
228
+ *
229
+ * @default false
230
+ */
231
+ noEmitOnError?: boolean
232
+
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
+ */
242
+ skipDiagnostics?: boolean
243
+
244
+ /**
245
+ * Whether log diagnostic informations
246
+ *
247
+ * Not effective when `skipDiagnostics` is true
248
+ *
249
+ * @deprecated
250
+ * @default false
251
+ */
252
+ logDiagnostics?: boolean
253
+
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
+ */
270
+ afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
271
+
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
+ */
281
+ beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
282
+
283
+ /**
284
+ * After build hook
285
+ *
286
+ * It wil be called after all declaration files are written
287
+ *
288
+ * @default () => {}
289
+ */
290
+ afterBuild?: () => void | Promise<void>
291
+ }
292
+ ```
293
+
294
+ ## Example
295
+
296
+ Clone and run the following script:
297
+
298
+ ```sh
299
+ pnpm run test:e2e
300
+ ```
301
+
302
+ Then check `example/types`.
303
+
304
+ ## License
305
+
306
+ MIT License.