vite-plugin-dts 1.5.0 → 1.6.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,213 +1,213 @@
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
- // Default: () => {}
161
- beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
162
-
163
- // After build hook
164
- // It wil be called after all declaration files are written
165
- // Default: () => {}
166
- afterBuild?: () => void | Promise<void>
167
- }
168
- ```
169
-
170
- ## Example
171
-
172
- Clone and run the following script:
173
-
174
- ```sh
175
- pnpm run test:e2e
176
- ```
177
-
178
- Then check `example/types`.
179
-
180
- ## FAQ
181
-
182
- Here are some FAQ's and solutions.
183
-
184
- ### Missing some declaration files after build
185
-
186
- 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).
187
-
188
- 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.
189
-
190
- ### Take type error when using both `script` and `setup-script` in vue component
191
-
192
- 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.
193
-
194
- 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.
195
-
196
- ### Take errors that unable to infer types from packages which under `node_modules`
197
-
198
- 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:
199
-
200
- ```json
201
- {
202
- "compilerOptions": {
203
- "baseUrl": ".",
204
- "paths": {
205
- "third-lib": ["node_modules/third-lib"]
206
- }
207
- }
208
- }
209
- ```
210
-
211
- ## License
212
-
213
- 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
+ ## 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
+ // Default: () => {}
161
+ beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
162
+
163
+ // After build hook
164
+ // It wil be called after all declaration files are written
165
+ // Default: () => {}
166
+ afterBuild?: () => void | Promise<void>
167
+ }
168
+ ```
169
+
170
+ ## Example
171
+
172
+ Clone and run the following script:
173
+
174
+ ```sh
175
+ pnpm run test:e2e
176
+ ```
177
+
178
+ Then check `example/types`.
179
+
180
+ ## FAQ
181
+
182
+ Here are some FAQ's and solutions.
183
+
184
+ ### Missing some declaration files after build
185
+
186
+ 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).
187
+
188
+ 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.
189
+
190
+ ### Take type error when using both `script` and `setup-script` in vue component
191
+
192
+ 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.
193
+
194
+ 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.
195
+
196
+ ### Take errors that unable to infer types from packages which under `node_modules`
197
+
198
+ 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:
199
+
200
+ ```json
201
+ {
202
+ "compilerOptions": {
203
+ "baseUrl": ".",
204
+ "paths": {
205
+ "third-lib": ["node_modules/third-lib"]
206
+ }
207
+ }
208
+ }
209
+ ```
210
+
211
+ ## License
212
+
213
+ MIT License.