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.md CHANGED
@@ -1,313 +1,311 @@
1
- <h1 align="center">vite-plugin-dts</h1>
2
-
3
- <p align="center">
4
- A Vite plugin that generates declaration files (<code>*.d.ts</code>) from <code>.ts(x)</code> or <code>.vue</code> source files when using Vite in <a href="https://vitejs.dev/guide/build.html#library-mode">library mode</a>.
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.zh-CN.md)
14
-
15
- ## Install
16
-
17
- ```sh
18
- pnpm add vite-plugin-dts -D
19
- ```
20
-
21
- ## Usage
22
-
23
- In `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
- In your component:
44
-
45
- ```vue
46
- <template>
47
- <div></div>
48
- </template>
49
-
50
- <script lang="ts">
51
- // using defineComponent for inferring types
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
- // Need to access the defineProps returned value to
63
- // infer types although you never use the props directly
64
- const props = defineProps<{
65
- color: 'blue' | 'red'
66
- }>()
67
- </script>
68
-
69
- <template>
70
- <div>{{ color }}</div>
71
- </template>
72
- ```
73
-
74
- ## FAQ
75
-
76
- Here are some FAQ's and solutions.
77
-
78
- ### Missing some declaration files after build (before `1.7.0`)
79
-
80
- By default, the `skipDiagnostics` option is set to `true` which means 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).
81
-
82
- If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on diagnostic and logging features of this plugin. It will help you check the type errors during build and log error information to the terminal.
83
-
84
- ### Type error when using both `script` and `setup-script` in Vue component (before `3.0.0`)
85
-
86
- This is usually caused by using the `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.
87
-
88
- Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/examples/vue/components/BothScripts.vue). You should remove the `defineComponent` which in `script` and export a native object directly.
89
-
90
- ### Type errors that are unable to infer types from packages in `node_modules`
91
-
92
- This is an existing issue when TypeScript infers types from packages located in `node_modules` through soft links (pnpm). Please refer to [this TypeScript issue](https://github.com/microsoft/TypeScript/issues/42873). The current workaround is to add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
93
-
94
- ```json
95
- {
96
- "compilerOptions": {
97
- "baseUrl": ".",
98
- "paths": {
99
- "third-lib": ["node_modules/third-lib"]
100
- }
101
- }
102
- }
103
- ```
104
-
105
- ## Options
106
-
107
- ```ts
108
- import type ts from 'typescript'
109
- import type { LogLevel } from 'vite'
110
-
111
- interface TransformWriteFile {
112
- filePath?: string
113
- content?: string
114
- }
115
-
116
- export interface PluginOptions {
117
- /**
118
- * Specify root directory
119
- *
120
- * By Default it base on 'root' of your Vite config, or `process.cwd()` if using Rollup
121
- */
122
- root?: string
123
-
124
- /**
125
- * Specify declaration files output directory
126
- *
127
- * Can be specified a array to output to multiple directories
128
- *
129
- * By Default it base on 'build.outDir' of your Vite config, or `outDir` of tsconfig.json if using Rollup
130
- */
131
- outDir?: string | string[]
132
-
133
- /**
134
- * Manually set the root path of the entry files, useful in monorepo
135
- *
136
- * The output path of each file will be calculated base on it
137
- *
138
- * By Default it is the smallest public path for all files
139
- */
140
- entryRoot?: string
141
-
142
- /**
143
- * Strictly restrict declaration files output inside `outDir`
144
- *
145
- * Because if `entryRoot` is specified, declaration files maybe outside `outDir`
146
- *
147
- * @default true
148
- */
149
- strictOutput?: boolean
150
-
151
- /**
152
- * Specify a CompilerOptions to override
153
- *
154
- * @default null
155
- */
156
- compilerOptions?: ts.CompilerOptions | null
157
-
158
- /**
159
- * Specify tsconfig.json path
160
- *
161
- * Plugin also resolve include and exclude files from tsconfig.json
162
- *
163
- * By default plugin will find config form root if not specify
164
- */
165
- tsconfigPath?: string
166
-
167
- /**
168
- * Set which paths should exclude when transform aliases
169
- *
170
- * If it's regexp, it will test the original import path directly
171
- *
172
- * @default []
173
- */
174
- aliasesExclude?: (string | RegExp)[]
175
-
176
- /**
177
- * Whether transform file name '.vue.d.ts' to '.d.ts'
178
- *
179
- * @default false
180
- */
181
- cleanVueFileName?: boolean
182
-
183
- /**
184
- * Whether transform dynamic import to static
185
- *
186
- * Force `true` when `rollupTypes` is effective
187
- *
188
- * eg. `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`
189
- *
190
- * @default false
191
- */
192
- staticImport?: boolean
193
-
194
- /**
195
- * Manual set include glob
196
- *
197
- * By Default it base on `include` option of the tsconfig.json
198
- */
199
- include?: string | string[]
200
-
201
- /**
202
- * Manual set exclude glob
203
- *
204
- * By Default it base on `exclude` option of the tsconfig.json, be `'node_module/**'` when empty
205
- */
206
- exclude?: string | string[]
207
-
208
- /**
209
- * Whether remove those `import 'xxx'`
210
- *
211
- * @default true
212
- */
213
- clearPureImport?: boolean
214
-
215
- /**
216
- * Whether generate types entry file
217
- *
218
- * When `true` will from package.json types field if exists or `${outDir}/index.d.ts`
219
- *
220
- * Force `true` when `rollupTypes` is effective
221
- *
222
- * @default false
223
- */
224
- insertTypesEntry?: boolean
225
-
226
- /**
227
- * Set whether rollup declaration files after emit
228
- *
229
- * Power by `@microsoft/api-extractor`, it will start a new program which takes some time
230
- *
231
- * @default false
232
- */
233
- rollupTypes?: boolean
234
-
235
- /**
236
- * Bundled packages for `@microsoft/api-extractor`
237
- *
238
- * @default []
239
- * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
240
- */
241
- bundledPackages?: string[]
242
-
243
- /**
244
- * Whether copy .d.ts source files into `outDir`
245
- *
246
- * @default false
247
- * @remarks Before 2.0 it defaults to true
248
- */
249
- copyDtsFiles?: boolean
250
-
251
- /**
252
- * Specify the log level of plugin
253
- *
254
- * By Default it base on 'logLevel' option of your Vite config
255
- */
256
- logLevel?: LogLevel
257
-
258
- /**
259
- * Hook after diagnostic emitted
260
- *
261
- * According to the length to judge whether there is any type error
262
- *
263
- * @default () => {}
264
- */
265
- afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => void | Promise<void>
266
-
267
- /**
268
- * Hook before each declaration file is written
269
- *
270
- * You can transform declaration file's path and content through it
271
- *
272
- * The file will be skipped when return exact `false`
273
- *
274
- * @default () => {}
275
- */
276
- beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
277
-
278
- /**
279
- * Hook after built
280
- *
281
- * It wil be called after all declaration files are written
282
- *
283
- * @default () => {}
284
- */
285
- afterBuild?: () => void | Promise<void>
286
- }
287
- ```
288
-
289
- ## Contributors
290
-
291
- Thanks for all the contributions!
292
-
293
- <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
294
- <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
295
- </a>
296
-
297
- ## Example
298
-
299
- Clone and run the following script:
300
-
301
- ```sh
302
- pnpm run test:ts
303
- ```
304
-
305
- Then check `examples/ts/types`.
306
-
307
- Also Vue and React cases under `examples`.
308
-
309
- A real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).
310
-
311
- ## License
312
-
313
- MIT License.
1
+ <h1 align="center">vite-plugin-dts</h1>
2
+
3
+ <p align="center">
4
+ A Vite plugin that generates declaration files (<code>*.d.ts</code>) from <code>.ts(x)</code> or <code>.vue</code> source files when using Vite in <a href="https://vitejs.dev/guide/build.html#library-mode">library mode</a>.
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.zh-CN.md)
14
+
15
+ ## Install
16
+
17
+ ```sh
18
+ pnpm i vite-plugin-dts -D
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ In `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
+ In your component:
44
+
45
+ ```vue
46
+ <template>
47
+ <div></div>
48
+ </template>
49
+
50
+ <script lang="ts">
51
+ // using defineComponent for inferring types
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
+ ## FAQ
73
+
74
+ Here are some FAQ's and solutions.
75
+
76
+ ### Missing some declaration files after build (before `1.7.0`)
77
+
78
+ By default, the `skipDiagnostics` option is set to `true` which means 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).
79
+
80
+ If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on diagnostic and logging features of this plugin. It will help you check the type errors during build and log error information to the terminal.
81
+
82
+ ### Type error when using both `script` and `setup-script` in Vue component (before `3.0.0`)
83
+
84
+ This is usually caused by using the `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.
85
+
86
+ Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/examples/vue/components/BothScripts.vue). You should remove the `defineComponent` which in `script` and export a native object directly.
87
+
88
+ ### Type errors that are unable to infer types from packages in `node_modules`
89
+
90
+ This is an existing issue when TypeScript infers types from packages located in `node_modules` through soft links (pnpm). Please refer to [this TypeScript issue](https://github.com/microsoft/TypeScript/issues/42873). The current workaround is to add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
91
+
92
+ ```json
93
+ {
94
+ "compilerOptions": {
95
+ "baseUrl": ".",
96
+ "paths": {
97
+ "third-lib": ["node_modules/third-lib"]
98
+ }
99
+ }
100
+ }
101
+ ```
102
+
103
+ ## Options
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
+ * Specify root directory
117
+ *
118
+ * By Default it base on 'root' of your Vite config, or `process.cwd()` if using Rollup
119
+ */
120
+ root?: string
121
+
122
+ /**
123
+ * Specify declaration files output directory
124
+ *
125
+ * Can be specified a array to output to multiple directories
126
+ *
127
+ * By Default it base on 'build.outDir' of your Vite config, or `outDir` of tsconfig.json if using Rollup
128
+ */
129
+ outDir?: string | string[]
130
+
131
+ /**
132
+ * Manually set the root path of the entry files, useful in monorepo
133
+ *
134
+ * The output path of each file will be calculated base on it
135
+ *
136
+ * By Default it is the smallest public path for all files
137
+ */
138
+ entryRoot?: string
139
+
140
+ /**
141
+ * Strictly restrict declaration files output inside `outDir`
142
+ *
143
+ * Because if `entryRoot` is specified, declaration files maybe outside `outDir`
144
+ *
145
+ * @default true
146
+ */
147
+ strictOutput?: boolean
148
+
149
+ /**
150
+ * Specify a CompilerOptions to override
151
+ *
152
+ * @default null
153
+ */
154
+ compilerOptions?: ts.CompilerOptions | null
155
+
156
+ /**
157
+ * Specify tsconfig.json path
158
+ *
159
+ * Plugin also resolve include and exclude files from tsconfig.json
160
+ *
161
+ * By default plugin will find config form root if not specify
162
+ */
163
+ tsconfigPath?: string
164
+
165
+ /**
166
+ * Set which paths should exclude when transform aliases
167
+ *
168
+ * If it's regexp, it will test the original import path directly
169
+ *
170
+ * @default []
171
+ */
172
+ aliasesExclude?: (string | RegExp)[]
173
+
174
+ /**
175
+ * Whether transform file name '.vue.d.ts' to '.d.ts'
176
+ *
177
+ * @default false
178
+ */
179
+ cleanVueFileName?: boolean
180
+
181
+ /**
182
+ * Whether transform dynamic import to static
183
+ *
184
+ * Force `true` when `rollupTypes` is effective
185
+ *
186
+ * eg. `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`
187
+ *
188
+ * @default false
189
+ */
190
+ staticImport?: boolean
191
+
192
+ /**
193
+ * Manual set include glob
194
+ *
195
+ * By Default it base on `include` option of the tsconfig.json
196
+ */
197
+ include?: string | string[]
198
+
199
+ /**
200
+ * Manual set exclude glob
201
+ *
202
+ * By Default it base on `exclude` option of the tsconfig.json, be `'node_module/**'` when empty
203
+ */
204
+ exclude?: string | string[]
205
+
206
+ /**
207
+ * Whether remove those `import 'xxx'`
208
+ *
209
+ * @default true
210
+ */
211
+ clearPureImport?: boolean
212
+
213
+ /**
214
+ * Whether generate types entry file
215
+ *
216
+ * When `true` will from package.json types field if exists or `${outDir}/index.d.ts`
217
+ *
218
+ * Force `true` when `rollupTypes` is effective
219
+ *
220
+ * @default false
221
+ */
222
+ insertTypesEntry?: boolean
223
+
224
+ /**
225
+ * Set whether rollup declaration files after emit
226
+ *
227
+ * Power by `@microsoft/api-extractor`, it will start a new program which takes some time
228
+ *
229
+ * @default false
230
+ */
231
+ rollupTypes?: boolean
232
+
233
+ /**
234
+ * Bundled packages for `@microsoft/api-extractor`
235
+ *
236
+ * @default []
237
+ * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
238
+ */
239
+ bundledPackages?: string[]
240
+
241
+ /**
242
+ * Whether copy .d.ts source files into `outDir`
243
+ *
244
+ * @default false
245
+ * @remarks Before 2.0 it defaults to true
246
+ */
247
+ copyDtsFiles?: boolean
248
+
249
+ /**
250
+ * Specify the log level of plugin
251
+ *
252
+ * By Default it base on 'logLevel' option of your Vite config
253
+ */
254
+ logLevel?: LogLevel
255
+
256
+ /**
257
+ * Hook after diagnostic emitted
258
+ *
259
+ * According to the length to judge whether there is any type error
260
+ *
261
+ * @default () => {}
262
+ */
263
+ afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => void | Promise<void>
264
+
265
+ /**
266
+ * Hook before each declaration file is written
267
+ *
268
+ * You can transform declaration file's path and content through it
269
+ *
270
+ * The file will be skipped when return exact `false`
271
+ *
272
+ * @default () => {}
273
+ */
274
+ beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
275
+
276
+ /**
277
+ * Hook after built
278
+ *
279
+ * It wil be called after all declaration files are written
280
+ *
281
+ * @default () => {}
282
+ */
283
+ afterBuild?: () => void | Promise<void>
284
+ }
285
+ ```
286
+
287
+ ## Contributors
288
+
289
+ Thanks for all the contributions!
290
+
291
+ <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
292
+ <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
293
+ </a>
294
+
295
+ ## Example
296
+
297
+ Clone and run the following script:
298
+
299
+ ```sh
300
+ pnpm run test:ts
301
+ ```
302
+
303
+ Then check `examples/ts/types`.
304
+
305
+ Also Vue and React cases under `examples`.
306
+
307
+ A real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).
308
+
309
+ ## License
310
+
311
+ MIT License.