vite-plugin-dts 1.0.3 → 1.1.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,185 +1,192 @@
1
- # vite-plugin-dts
2
-
3
- **English** | [中文](./README.zh-CN.md)
4
-
5
- A vite plugin that generate `.d.ts` files from `.ts` or `.vue` source files for lib.
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
- // Defaults base on your vite config output options
83
- outputDir?: string
84
-
85
- // Manually set the root path of the entry files
86
- // The output path of each file will be caculated base on it
87
- // Defaults is the smallest public path for all files
88
- entryRoot?: string
89
-
90
- // Project init compilerOptions using by ts-morph
91
- // Default: null
92
- compilerOptions?: ts.CompilerOptions | null
93
-
94
- // Project init tsconfig.json file path by ts-morph
95
- // Plugin also resolve incldue and exclude files from tsconfig.json
96
- // Default: 'tsconfig.json'
97
- tsConfigFilePath?: string
98
-
99
- // Whether transform file name '.vue.d.ts' to '.d.ts'
100
- // Default: false
101
- cleanVueFileName?: boolean
102
-
103
- // Whether transform dynamic import to static
104
- // eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
105
- // Default: false
106
- staticImport?: boolean
107
-
108
- // Manual set include glob
109
- // Defaults base on your tsconfig.json include option
110
- include?: string | string[]
111
-
112
- // Manual set exclude glob
113
- // Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
114
- exclude?: string | string[]
115
-
116
- // Whether generate types entry file
117
- // When true will from package.json types field if exists or `${outputDir}/index.d.ts`
118
- // Default: false
119
- insertTypesEntry?: boolean
120
-
121
- // Whether copy .d.ts source files into outputDir
122
- // Default: true
123
- copyDtsFiles?: boolean
124
-
125
- // Whether emit nothing when has any diagnostic
126
- // Default: false
127
- noEmitOnError?: boolean
128
-
129
- // Whether skip typescript diagnostics
130
- // Skip type diagnostics means that type errors will not interrupt the build process
131
- // But for the source files with type errors will not be emitted
132
- // Default: true
133
- skipDiagnostics?: boolean
134
-
135
- // Whether log diagnostic informations
136
- // Not effective when `skipDiagnostics` is true
137
- // Default: false
138
- logDiagnostics?: boolean
139
-
140
- // After emit diagnostic hook
141
- // According to the length to judge whether there is any type error
142
- // Default: () => {}
143
- afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
144
-
145
- // Before declaration file be writed hook
146
- // You can transform declaration file-path and content through it
147
- // Default: () => {}
148
- beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
149
-
150
- // After build hook
151
- // It wil be called after all declaration files are written
152
- // Default: () => {}
153
- afterBuild?: () => void | Promise<void>
154
- }
155
- ```
156
-
157
- ## Example
158
-
159
- Clone and run the following script:
160
-
161
- ```sh
162
- pnpm run test:e2e
163
- ```
164
-
165
- Then check `example/types`.
166
-
167
- ## FAQ
168
-
169
- Here will include some FAQ's and provide some solutions.
170
-
171
- ### Missing some declaration files after build
172
-
173
- By default `skipDiagnostics` option is `true`, which means that type diagnostic will be skipped during the build process (some projects may have diagnostic tool such as `vue-tsc`). If there are some files with type errors which will interrupt the build process, these files will not be emitted (not generate declaration files).
174
-
175
- If your project has not 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 to check the type errors during build and log error information to the terminal.
176
-
177
- ### Take type error when using both `script` and `setup-script` in vue component
178
-
179
- 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 in `script` will be merged into the parameter object of `defineComponent` that in `setup-script`, but there is incompatible of `defineComponent` returned types and parameter types, then a type error is caused.
180
-
181
- 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.
182
-
183
- ## License
184
-
185
- 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` 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
+ // Defaults base on your vite config output options
83
+ outputDir?: string
84
+
85
+ // Manually set the root path of the entry files
86
+ // The output path of each file will be caculated base on it
87
+ // Defaults is the smallest public path for all files
88
+ entryRoot?: string
89
+
90
+ // Project init compilerOptions using by ts-morph
91
+ // Default: null
92
+ compilerOptions?: ts.CompilerOptions | null
93
+
94
+ // Project init tsconfig.json file path by ts-morph
95
+ // Plugin also resolve incldue and exclude files from tsconfig.json
96
+ // Default: 'tsconfig.json'
97
+ tsConfigFilePath?: string
98
+
99
+ // Whether transform file name '.vue.d.ts' to '.d.ts'
100
+ // Default: false
101
+ cleanVueFileName?: boolean
102
+
103
+ // Whether transform dynamic import to static
104
+ // Force true when `rollupTypes` is effective
105
+ // eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
106
+ // Default: false
107
+ staticImport?: boolean
108
+
109
+ // Manual set include glob
110
+ // Defaults base on your tsconfig.json include option
111
+ include?: string | string[]
112
+
113
+ // Manual set exclude glob
114
+ // Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
115
+ exclude?: string | string[]
116
+
117
+ // Whether generate types entry file
118
+ // When true will from package.json types field if exists or `${outputDir}/index.d.ts`
119
+ // Force true when `rollupTypes` is effective
120
+ // Default: false
121
+ insertTypesEntry?: boolean
122
+
123
+ // Set to rollup declaration files after emit
124
+ // Power by `@microsoft/api-extractor`, it will start a new program which takes some time
125
+ // Default: false
126
+ rollupTypes?: boolean
127
+
128
+ // Whether copy .d.ts source files into outputDir
129
+ // Default: true
130
+ copyDtsFiles?: boolean
131
+
132
+ // Whether emit nothing when has any diagnostic
133
+ // Default: false
134
+ noEmitOnError?: boolean
135
+
136
+ // Whether skip typescript diagnostics
137
+ // Skip type diagnostics means that type errors will not interrupt the build process
138
+ // But for the source files with type errors will not be emitted
139
+ // Default: true
140
+ skipDiagnostics?: boolean
141
+
142
+ // Whether log diagnostic informations
143
+ // Not effective when `skipDiagnostics` is true
144
+ // Default: false
145
+ logDiagnostics?: boolean
146
+
147
+ // After emit diagnostic hook
148
+ // According to the length to judge whether there is any type error
149
+ // Default: () => {}
150
+ afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
151
+
152
+ // Before declaration file be writed hook
153
+ // You can transform declaration file-path and content through it
154
+ // Default: () => {}
155
+ beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
156
+
157
+ // After build hook
158
+ // It wil be called after all declaration files are written
159
+ // Default: () => {}
160
+ afterBuild?: () => void | Promise<void>
161
+ }
162
+ ```
163
+
164
+ ## Example
165
+
166
+ Clone and run the following script:
167
+
168
+ ```sh
169
+ pnpm run test:e2e
170
+ ```
171
+
172
+ Then check `example/types`.
173
+
174
+ ## FAQ
175
+
176
+ Here are some FAQ's and solutions.
177
+
178
+ ### Missing some declaration files after build
179
+
180
+ 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).
181
+
182
+ 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.
183
+
184
+ ### Take type error when using both `script` and `setup-script` in vue component
185
+
186
+ 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.
187
+
188
+ 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.
189
+
190
+ ## License
191
+
192
+ MIT License.