vite-plugin-dts 3.3.1 → 3.5.0

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,332 +1,368 @@
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
- <a href="https://github.com/qmhc/vite-plugin-dts/blob/main/LICENSE">
12
- <img src="https://img.shields.io/npm/l/vite-plugin-dts" alt="license" />
13
- </a>
14
- </p>
15
-
16
- **English** | [中文](./README.zh-CN.md)
17
-
18
- ## Install
19
-
20
- ```sh
21
- pnpm i vite-plugin-dts -D
22
- ```
23
-
24
- ## Usage
25
-
26
- In `vite.config.ts`:
27
-
28
- ```ts
29
- import { resolve } from 'path'
30
- import { defineConfig } from 'vite'
31
- import dts from 'vite-plugin-dts'
32
-
33
- export default defineConfig({
34
- build: {
35
- lib: {
36
- entry: resolve(__dirname, 'src/index.ts'),
37
- name: 'MyLib',
38
- formats: ['es'],
39
- fileName: 'my-lib'
40
- }
41
- },
42
- plugins: [dts()]
43
- })
44
- ```
45
-
46
- Starting with `3.0.0`, you can use this plugin with Rollup.
47
-
48
- ## FAQ
49
-
50
- Here are some FAQ's and solutions.
51
-
52
- ### Missing some declaration files after build (before `1.7.0`)
53
-
54
- By default, the `skipDiagnostics` option is set to `true` which means type diagnostics will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). Files with type errors which interrupt the build process will not be emitted (declaration files won't be generated).
55
-
56
- 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. Type errors during build will be logged to the terminal.
57
-
58
- ### Type error when using both `script` and `setup-script` in Vue component (before `3.0.0`)
59
-
60
- 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`. This results in a type error.
61
-
62
- Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/examples/vue/components/BothScripts.vue). You should remove the `defineComponent` in `script` and export a native object directly.
63
-
64
- ### Type errors that are unable to infer types from packages in `node_modules`
65
-
66
- This is an existing [TypeScript issue](https://github.com/microsoft/TypeScript/issues/42873) where TypeScript infers types from packages located in `node_modules` through soft links (pnpm). A workaround is to add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
67
-
68
- ```json
69
- {
70
- "compilerOptions": {
71
- "baseUrl": ".",
72
- "paths": {
73
- "third-lib": ["node_modules/third-lib"]
74
- }
75
- }
76
- }
77
- ```
78
-
79
- ## Options
80
-
81
- ```ts
82
- import type ts from 'typescript'
83
- import type { LogLevel } from 'vite'
84
-
85
- type MaybePromise<T> = T | Promise<T>
86
-
87
- export interface Resolver {
88
- /**
89
- * The name of the resolver
90
- *
91
- * The later resolver with the same name will overwrite the earlier
92
- */
93
- name: string,
94
- /**
95
- * Determine whether the resolver supports the file
96
- */
97
- supports: (id: string) => void | boolean,
98
- /**
99
- * Transform source to declaration files
100
- *
101
- * Note that the path of the returns should base on `outDir`, or relative path to `root`
102
- */
103
- transform: (payload: {
104
- id: string,
105
- code: string,
106
- root: string,
107
- outDir: string,
108
- host: ts.CompilerHost,
109
- program: ts.Program,
110
- service: ts.LanguageService
111
- }) => MaybePromise<{ path: string, content: string }[]>
112
- }
113
-
114
- export interface PluginOptions {
115
- /**
116
- * Specify root directory
117
- *
118
- * Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup
119
- */
120
- root?: string,
121
-
122
- /**
123
- * Output directory for declaration files
124
- *
125
- * Can be an array to output to multiple directories
126
- *
127
- * Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup
128
- */
129
- outDir?: string | string[],
130
-
131
- /**
132
- * Override root path of entry files (useful in monorepos)
133
- *
134
- * The output path of each file will be calculated based on the value provided
135
- *
136
- * The default is the smallest public path for all source files
137
- */
138
- entryRoot?: string,
139
-
140
- /**
141
- * Restrict declaration files output to `outDir`
142
- *
143
- * If true, generated declaration files outside `outDir` will be ignored
144
- *
145
- * @default true
146
- */
147
- strictOutput?: boolean,
148
-
149
- /**
150
- * Override compilerOptions
151
- *
152
- * @default null
153
- */
154
- compilerOptions?: ts.CompilerOptions | null,
155
-
156
- /**
157
- * Specify tsconfig.json path
158
- *
159
- * Plugin resolves `include` and `exclude` globs from tsconfig.json
160
- *
161
- * If not specified, plugin will find config file from root
162
- */
163
- tsconfigPath?: string,
164
-
165
- /**
166
- * Specify custom resolvers
167
- *
168
- * @default []
169
- */
170
- resolvers?: Resolver[],
171
-
172
- /**
173
- * Parsing `paths` of tsconfig.json to aliases
174
- *
175
- * Note that these aliases only use for declaration files
176
- *
177
- * @default true
178
- * @remarks Only use first replacement of each path
179
- */
180
- pathsToAliases?: boolean,
181
-
182
- /**
183
- * Set which paths should be excluded when transforming aliases
184
- *
185
- * @default []
186
- */
187
- aliasesExclude?: (string | RegExp)[],
188
-
189
- /**
190
- * Whether to transform file names ending in '.vue.d.ts' to '.d.ts'
191
- *
192
- * @default false
193
- */
194
- cleanVueFileName?: boolean,
195
-
196
- /**
197
- * Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`)
198
- *
199
- * Value is forced to `true` when `rollupTypes` is `true`
200
- *
201
- * @default false
202
- */
203
- staticImport?: boolean,
204
-
205
- /**
206
- * Override `include` glob
207
- *
208
- * Defaults to `include` property of tsconfig.json
209
- */
210
- include?: string | string[],
211
-
212
- /**
213
- * Override `exclude` glob
214
- *
215
- * Defaults to `exclude` property of tsconfig.json or `'node_module/**'` if not supplied.
216
- */
217
- exclude?: string | string[],
218
-
219
- /**
220
- * Whether to remove `import 'xxx'`
221
- *
222
- * @default true
223
- */
224
- clearPureImport?: boolean,
225
-
226
- /**
227
- * Whether to generate types entry file(s)
228
- *
229
- * When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`
230
- *
231
- * Value is forced to `true` when `rollupTypes` is `true`
232
- *
233
- * @default false
234
- */
235
- insertTypesEntry?: boolean,
236
-
237
- /**
238
- * Rollup type declaration files after emitting them
239
- *
240
- * Powered by `@microsoft/api-extractor` - time-intensive operation
241
- *
242
- * @default false
243
- */
244
- rollupTypes?: boolean,
245
-
246
- /**
247
- * Bundled packages for `@microsoft/api-extractor`
248
- *
249
- * @default []
250
- * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
251
- */
252
- bundledPackages?: string[],
253
-
254
- /**
255
- * Whether to copy .d.ts source files to `outDir`
256
- *
257
- * @default false
258
- * @remarks Before 2.0, the default was `true`
259
- */
260
- copyDtsFiles?: boolean,
261
-
262
- /**
263
- * Logging level for this plugin
264
- *
265
- * Defaults to the 'logLevel' property of your Vite config
266
- */
267
- logLevel?: LogLevel,
268
-
269
- /**
270
- * Hook called after diagnostic is emitted
271
- *
272
- * According to the `diagnostics.length`, you can judge whether there is any type error
273
- *
274
- * @default () => {}
275
- */
276
- afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>,
277
-
278
- /**
279
- * Hook called prior to writing each declaration file
280
- *
281
- * This allows you to transform the path or content
282
- *
283
- * The file will be skipped when the return value `false` or `Promise<false>`
284
- *
285
- * @default () => {}
286
- */
287
- beforeWriteFile?: (
288
- filePath: string,
289
- content: string
290
- ) => MaybePromise<
291
- | void
292
- | false
293
- | {
294
- filePath?: string,
295
- content?: string
296
- }
297
- >,
298
-
299
- /**
300
- * Hook called after all declaration files are written
301
- *
302
- * @default () => {}
303
- */
304
- afterBuild?: () => MaybePromise<void>
305
- }
306
- ```
307
-
308
- ## Contributors
309
-
310
- Thanks for all the contributions!
311
-
312
- <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
313
- <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
314
- </a>
315
-
316
- ## Example
317
-
318
- Clone and run the following script:
319
-
320
- ```sh
321
- pnpm run test:ts
322
- ```
323
-
324
- Then check `examples/ts/types`.
325
-
326
- Also Vue and React cases under `examples`.
327
-
328
- A real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).
329
-
330
- ## License
331
-
332
- 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
+ <a href="https://github.com/qmhc/vite-plugin-dts/blob/main/LICENSE">
12
+ <img src="https://img.shields.io/npm/l/vite-plugin-dts" alt="license" />
13
+ </a>
14
+ </p>
15
+
16
+ **English** | [中文](./README.zh-CN.md)
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ pnpm i vite-plugin-dts -D
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ In `vite.config.ts`:
27
+
28
+ ```ts
29
+ import { resolve } from 'path'
30
+ import { defineConfig } from 'vite'
31
+ import dts from 'vite-plugin-dts'
32
+
33
+ export default defineConfig({
34
+ build: {
35
+ lib: {
36
+ entry: resolve(__dirname, 'src/index.ts'),
37
+ name: 'MyLib',
38
+ formats: ['es'],
39
+ fileName: 'my-lib'
40
+ }
41
+ },
42
+ plugins: [dts()]
43
+ })
44
+ ```
45
+
46
+ By default, the generated declaration files are following the source structure.
47
+
48
+ If you want to merge all declarations into one file, just specify `rollupTypes: true`:
49
+
50
+ ```ts
51
+ {
52
+ plugins: [dts({ rollupTypes: true })]
53
+ }
54
+ ```
55
+
56
+ Starting with `3.0.0`, you can use this plugin with Rollup.
57
+
58
+ ## FAQ
59
+
60
+ Here are some FAQ's and solutions.
61
+
62
+ ### Missing some declaration files after build (before `1.7.0`)
63
+
64
+ By default, the `skipDiagnostics` option is set to `true` which means type diagnostics will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). Files with type errors which interrupt the build process will not be emitted (declaration files won't be generated).
65
+
66
+ 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. Type errors during build will be logged to the terminal.
67
+
68
+ ### Type error when using both `script` and `setup-script` in Vue component (before `3.0.0`)
69
+
70
+ 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`. This results in a type error.
71
+
72
+ Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/examples/vue/components/BothScripts.vue). You should remove the `defineComponent` in `script` and export a native object directly.
73
+
74
+ ### Type errors that are unable to infer types from packages in `node_modules`
75
+
76
+ This is an existing [TypeScript issue](https://github.com/microsoft/TypeScript/issues/42873) where TypeScript infers types from packages located in `node_modules` through soft links (pnpm). A workaround is to add `baseUrl` to your `tsconfig.json` and specify the `paths` for these packages:
77
+
78
+ ```json
79
+ {
80
+ "compilerOptions": {
81
+ "baseUrl": ".",
82
+ "paths": {
83
+ "third-lib": ["node_modules/third-lib"]
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
89
+ ## Options
90
+
91
+ ```ts
92
+ import type ts from 'typescript'
93
+ import type { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
94
+ import type { LogLevel } from 'vite'
95
+
96
+ type MaybePromise<T> = T | Promise<T>
97
+
98
+ export type RollupConfig = Omit<
99
+ IExtractorConfigPrepareOptions['configObject'],
100
+ | 'projectFolder'
101
+ | 'mainEntryPointFilePath'
102
+ | 'compiler'
103
+ | 'dtsRollup'
104
+ >
105
+
106
+ export interface Resolver {
107
+ /**
108
+ * The name of the resolver
109
+ *
110
+ * The later resolver with the same name will overwrite the earlier
111
+ */
112
+ name: string,
113
+ /**
114
+ * Determine whether the resolver supports the file
115
+ */
116
+ supports: (id: string) => void | boolean,
117
+ /**
118
+ * Transform source to declaration files
119
+ *
120
+ * Note that the path of the returns should base on `outDir`, or relative path to `root`
121
+ */
122
+ transform: (payload: {
123
+ id: string,
124
+ code: string,
125
+ root: string,
126
+ outDir: string,
127
+ host: ts.CompilerHost,
128
+ program: ts.Program,
129
+ service: ts.LanguageService
130
+ }) => MaybePromise<{ path: string, content: string }[]>
131
+ }
132
+
133
+ export interface PluginOptions {
134
+ /**
135
+ * Specify root directory
136
+ *
137
+ * Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup
138
+ */
139
+ root?: string,
140
+
141
+ /**
142
+ * Output directory for declaration files
143
+ *
144
+ * Can be an array to output to multiple directories
145
+ *
146
+ * Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup
147
+ */
148
+ outDir?: string | string[],
149
+
150
+ /**
151
+ * Override root path of entry files (useful in monorepos)
152
+ *
153
+ * The output path of each file will be calculated based on the value provided
154
+ *
155
+ * The default is the smallest public path for all source files
156
+ */
157
+ entryRoot?: string,
158
+
159
+ /**
160
+ * Restrict declaration files output to `outDir`
161
+ *
162
+ * If true, generated declaration files outside `outDir` will be ignored
163
+ *
164
+ * @default true
165
+ */
166
+ strictOutput?: boolean,
167
+
168
+ /**
169
+ * Override compilerOptions
170
+ *
171
+ * @default null
172
+ */
173
+ compilerOptions?: ts.CompilerOptions | null,
174
+
175
+ /**
176
+ * Specify tsconfig.json path
177
+ *
178
+ * Plugin resolves `include` and `exclude` globs from tsconfig.json
179
+ *
180
+ * If not specified, plugin will find config file from root
181
+ */
182
+ tsconfigPath?: string,
183
+
184
+ /**
185
+ * Specify custom resolvers
186
+ *
187
+ * @default []
188
+ */
189
+ resolvers?: Resolver[],
190
+
191
+ /**
192
+ * Parsing `paths` of tsconfig.json to aliases
193
+ *
194
+ * Note that these aliases only use for declaration files
195
+ *
196
+ * @default true
197
+ * @remarks Only use first replacement of each path
198
+ */
199
+ pathsToAliases?: boolean,
200
+
201
+ /**
202
+ * Set which paths should be excluded when transforming aliases
203
+ *
204
+ * @default []
205
+ */
206
+ aliasesExclude?: (string | RegExp)[],
207
+
208
+ /**
209
+ * Whether to transform file names ending in '.vue.d.ts' to '.d.ts'
210
+ *
211
+ * @default false
212
+ */
213
+ cleanVueFileName?: boolean,
214
+
215
+ /**
216
+ * Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`)
217
+ *
218
+ * Value is forced to `true` when `rollupTypes` is `true`
219
+ *
220
+ * @default false
221
+ */
222
+ staticImport?: boolean,
223
+
224
+ /**
225
+ * Override `include` glob
226
+ *
227
+ * Defaults to `include` property of tsconfig.json
228
+ */
229
+ include?: string | string[],
230
+
231
+ /**
232
+ * Override `exclude` glob
233
+ *
234
+ * Defaults to `exclude` property of tsconfig.json or `'node_module/**'` if not supplied.
235
+ */
236
+ exclude?: string | string[],
237
+
238
+ /**
239
+ * Whether to remove `import 'xxx'`
240
+ *
241
+ * @default true
242
+ */
243
+ clearPureImport?: boolean,
244
+
245
+ /**
246
+ * Whether to generate types entry file(s)
247
+ *
248
+ * When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`
249
+ *
250
+ * Value is forced to `true` when `rollupTypes` is `true`
251
+ *
252
+ * @default false
253
+ */
254
+ insertTypesEntry?: boolean,
255
+
256
+ /**
257
+ * Rollup type declaration files after emitting them
258
+ *
259
+ * Powered by `@microsoft/api-extractor` - time-intensive operation
260
+ *
261
+ * @default false
262
+ */
263
+ rollupTypes?: boolean,
264
+
265
+ /**
266
+ * Bundled packages for `@microsoft/api-extractor`
267
+ *
268
+ * @default []
269
+ * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
270
+ */
271
+ bundledPackages?: string[],
272
+
273
+ /**
274
+ * Override the config of `@microsoft/api-extractor`
275
+ *
276
+ * @default null
277
+ * @see https://api-extractor.com/pages/setup/configure_api_report/
278
+ */
279
+ rollupConfig?: RollupConfig,
280
+
281
+ /**
282
+ * Whether to copy .d.ts source files to `outDir`
283
+ *
284
+ * @default false
285
+ * @remarks Before 2.0, the default was `true`
286
+ */
287
+ copyDtsFiles?: boolean,
288
+
289
+ /**
290
+ * Whether to emit declaration files only
291
+ *
292
+ * When `true`, all the original outputs of vite (rollup) will be force removed
293
+ *
294
+ * @default false
295
+ */
296
+ declarationOnly?: boolean,
297
+
298
+ /**
299
+ * Logging level for this plugin
300
+ *
301
+ * Defaults to the 'logLevel' property of your Vite config
302
+ */
303
+ logLevel?: LogLevel,
304
+
305
+ /**
306
+ * Hook called after diagnostic is emitted
307
+ *
308
+ * According to the `diagnostics.length`, you can judge whether there is any type error
309
+ *
310
+ * @default () => {}
311
+ */
312
+ afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>,
313
+
314
+ /**
315
+ * Hook called prior to writing each declaration file
316
+ *
317
+ * This allows you to transform the path or content
318
+ *
319
+ * The file will be skipped when the return value `false` or `Promise<false>`
320
+ *
321
+ * @default () => {}
322
+ */
323
+ beforeWriteFile?: (
324
+ filePath: string,
325
+ content: string
326
+ ) => MaybePromise<
327
+ | void
328
+ | false
329
+ | {
330
+ filePath?: string,
331
+ content?: string
332
+ }
333
+ >,
334
+
335
+ /**
336
+ * Hook called after all declaration files are written
337
+ *
338
+ * @default () => {}
339
+ */
340
+ afterBuild?: () => MaybePromise<void>
341
+ }
342
+ ```
343
+
344
+ ## Contributors
345
+
346
+ Thanks for all the contributions!
347
+
348
+ <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
349
+ <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
350
+ </a>
351
+
352
+ ## Example
353
+
354
+ Clone and run the following script:
355
+
356
+ ```sh
357
+ pnpm run test:ts
358
+ ```
359
+
360
+ Then check `examples/ts/types`.
361
+
362
+ Also Vue and React cases under `examples`.
363
+
364
+ A real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).
365
+
366
+ ## License
367
+
368
+ MIT License.