vite-plugin-dts 3.4.0 → 3.5.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,349 +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 { IExtractorConfigPrepareOptions } from '@microsoft/api-extractor'
84
- import type { LogLevel } from 'vite'
85
-
86
- type MaybePromise<T> = T | Promise<T>
87
-
88
- export type RollupConfig = Omit<
89
- IExtractorConfigPrepareOptions['configObject'],
90
- | 'projectFolder'
91
- | 'mainEntryPointFilePath'
92
- | 'compiler'
93
- | 'dtsRollup'
94
- >
95
-
96
- export interface Resolver {
97
- /**
98
- * The name of the resolver
99
- *
100
- * The later resolver with the same name will overwrite the earlier
101
- */
102
- name: string,
103
- /**
104
- * Determine whether the resolver supports the file
105
- */
106
- supports: (id: string) => void | boolean,
107
- /**
108
- * Transform source to declaration files
109
- *
110
- * Note that the path of the returns should base on `outDir`, or relative path to `root`
111
- */
112
- transform: (payload: {
113
- id: string,
114
- code: string,
115
- root: string,
116
- outDir: string,
117
- host: ts.CompilerHost,
118
- program: ts.Program,
119
- service: ts.LanguageService
120
- }) => MaybePromise<{ path: string, content: string }[]>
121
- }
122
-
123
- export interface PluginOptions {
124
- /**
125
- * Specify root directory
126
- *
127
- * Defaults to the 'root' of the Vite config, or `process.cwd()` if using Rollup
128
- */
129
- root?: string,
130
-
131
- /**
132
- * Output directory for declaration files
133
- *
134
- * Can be an array to output to multiple directories
135
- *
136
- * Defaults to 'build.outDir' of the Vite config, or `outDir` of tsconfig.json if using Rollup
137
- */
138
- outDir?: string | string[],
139
-
140
- /**
141
- * Override root path of entry files (useful in monorepos)
142
- *
143
- * The output path of each file will be calculated based on the value provided
144
- *
145
- * The default is the smallest public path for all source files
146
- */
147
- entryRoot?: string,
148
-
149
- /**
150
- * Restrict declaration files output to `outDir`
151
- *
152
- * If true, generated declaration files outside `outDir` will be ignored
153
- *
154
- * @default true
155
- */
156
- strictOutput?: boolean,
157
-
158
- /**
159
- * Override compilerOptions
160
- *
161
- * @default null
162
- */
163
- compilerOptions?: ts.CompilerOptions | null,
164
-
165
- /**
166
- * Specify tsconfig.json path
167
- *
168
- * Plugin resolves `include` and `exclude` globs from tsconfig.json
169
- *
170
- * If not specified, plugin will find config file from root
171
- */
172
- tsconfigPath?: string,
173
-
174
- /**
175
- * Specify custom resolvers
176
- *
177
- * @default []
178
- */
179
- resolvers?: Resolver[],
180
-
181
- /**
182
- * Parsing `paths` of tsconfig.json to aliases
183
- *
184
- * Note that these aliases only use for declaration files
185
- *
186
- * @default true
187
- * @remarks Only use first replacement of each path
188
- */
189
- pathsToAliases?: boolean,
190
-
191
- /**
192
- * Set which paths should be excluded when transforming aliases
193
- *
194
- * @default []
195
- */
196
- aliasesExclude?: (string | RegExp)[],
197
-
198
- /**
199
- * Whether to transform file names ending in '.vue.d.ts' to '.d.ts'
200
- *
201
- * @default false
202
- */
203
- cleanVueFileName?: boolean,
204
-
205
- /**
206
- * Whether to transform dynamic imports to static (eg `import('vue').DefineComponent` to `import { DefineComponent } from 'vue'`)
207
- *
208
- * Value is forced to `true` when `rollupTypes` is `true`
209
- *
210
- * @default false
211
- */
212
- staticImport?: boolean,
213
-
214
- /**
215
- * Override `include` glob
216
- *
217
- * Defaults to `include` property of tsconfig.json
218
- */
219
- include?: string | string[],
220
-
221
- /**
222
- * Override `exclude` glob
223
- *
224
- * Defaults to `exclude` property of tsconfig.json or `'node_module/**'` if not supplied.
225
- */
226
- exclude?: string | string[],
227
-
228
- /**
229
- * Whether to remove `import 'xxx'`
230
- *
231
- * @default true
232
- */
233
- clearPureImport?: boolean,
234
-
235
- /**
236
- * Whether to generate types entry file(s)
237
- *
238
- * When `true`, uses package.json `types` property if it exists or `${outDir}/index.d.ts`
239
- *
240
- * Value is forced to `true` when `rollupTypes` is `true`
241
- *
242
- * @default false
243
- */
244
- insertTypesEntry?: boolean,
245
-
246
- /**
247
- * Rollup type declaration files after emitting them
248
- *
249
- * Powered by `@microsoft/api-extractor` - time-intensive operation
250
- *
251
- * @default false
252
- */
253
- rollupTypes?: boolean,
254
-
255
- /**
256
- * Bundled packages for `@microsoft/api-extractor`
257
- *
258
- * @default []
259
- * @see https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
260
- */
261
- bundledPackages?: string[],
262
-
263
- /**
264
- * Override the config of `@microsoft/api-extractor`
265
- *
266
- * @default null
267
- * @see https://api-extractor.com/pages/setup/configure_api_report/
268
- */
269
- rollupConfig?: RollupConfig,
270
-
271
- /**
272
- * Whether to copy .d.ts source files to `outDir`
273
- *
274
- * @default false
275
- * @remarks Before 2.0, the default was `true`
276
- */
277
- copyDtsFiles?: boolean,
278
-
279
- /**
280
- * Logging level for this plugin
281
- *
282
- * Defaults to the 'logLevel' property of your Vite config
283
- */
284
- logLevel?: LogLevel,
285
-
286
- /**
287
- * Hook called after diagnostic is emitted
288
- *
289
- * According to the `diagnostics.length`, you can judge whether there is any type error
290
- *
291
- * @default () => {}
292
- */
293
- afterDiagnostic?: (diagnostics: readonly ts.Diagnostic[]) => MaybePromise<void>,
294
-
295
- /**
296
- * Hook called prior to writing each declaration file
297
- *
298
- * This allows you to transform the path or content
299
- *
300
- * The file will be skipped when the return value `false` or `Promise<false>`
301
- *
302
- * @default () => {}
303
- */
304
- beforeWriteFile?: (
305
- filePath: string,
306
- content: string
307
- ) => MaybePromise<
308
- | void
309
- | false
310
- | {
311
- filePath?: string,
312
- content?: string
313
- }
314
- >,
315
-
316
- /**
317
- * Hook called after all declaration files are written
318
- *
319
- * @default () => {}
320
- */
321
- afterBuild?: () => MaybePromise<void>
322
- }
323
- ```
324
-
325
- ## Contributors
326
-
327
- Thanks for all the contributions!
328
-
329
- <a href="https://github.com/qmhc/vite-plugin-dts/graphs/contributors">
330
- <img src="https://contrib.rocks/image?repo=qmhc/vite-plugin-dts" />
331
- </a>
332
-
333
- ## Example
334
-
335
- Clone and run the following script:
336
-
337
- ```sh
338
- pnpm run test:ts
339
- ```
340
-
341
- Then check `examples/ts/types`.
342
-
343
- Also Vue and React cases under `examples`.
344
-
345
- A real project using this plugin: [Vexip UI](https://github.com/vexip-ui/vexip-ui).
346
-
347
- ## License
348
-
349
- 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.