vite-plugin-dts 0.9.5 → 0.9.6
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 +174 -174
- package/README.zh-CN.md +173 -173
- package/dist/index.js +13 -32
- package/dist/index.mjs +13 -32
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
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
|
-
yarn 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
|
-
// Project init compilerOptions using by ts-morph
|
|
86
|
-
// Default: null
|
|
87
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
88
|
-
|
|
89
|
-
// Project init tsconfig.json file path by ts-morph
|
|
90
|
-
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
91
|
-
// Default: 'tsconfig.json'
|
|
92
|
-
tsConfigFilePath?: string
|
|
93
|
-
|
|
94
|
-
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
95
|
-
// Default: false
|
|
96
|
-
cleanVueFileName?: boolean
|
|
97
|
-
|
|
98
|
-
// Whether transform dynamic import to static
|
|
99
|
-
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
100
|
-
// Default: false
|
|
101
|
-
staticImport?: boolean
|
|
102
|
-
|
|
103
|
-
// Manual set include glob
|
|
104
|
-
// Defaults base on your tsconfig.json include option
|
|
105
|
-
include?: string | string[]
|
|
106
|
-
|
|
107
|
-
// Manual set exclude glob
|
|
108
|
-
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
109
|
-
exclude?: string | string[]
|
|
110
|
-
|
|
111
|
-
// Whether generate types entry file
|
|
112
|
-
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
113
|
-
// Default: false
|
|
114
|
-
insertTypesEntry?: boolean
|
|
115
|
-
|
|
116
|
-
// Whether copy .d.ts source files into outputDir
|
|
117
|
-
// Default: true
|
|
118
|
-
copyDtsFiles?: boolean
|
|
119
|
-
|
|
120
|
-
// Whether emit nothing when has any diagnostic
|
|
121
|
-
// Default: false
|
|
122
|
-
noEmitOnError?: boolean
|
|
123
|
-
|
|
124
|
-
// Whether skip typescript diagnostics
|
|
125
|
-
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
126
|
-
// But for the source files with type errors will not be emitted
|
|
127
|
-
// Default: true
|
|
128
|
-
skipDiagnostics?: boolean
|
|
129
|
-
|
|
130
|
-
// Whether log diagnostic informations
|
|
131
|
-
// Not effective when `skipDiagnostics` is true
|
|
132
|
-
// Default: false
|
|
133
|
-
logDiagnostics?: boolean
|
|
134
|
-
|
|
135
|
-
// After emit diagnostic hook
|
|
136
|
-
// According to the length to judge whether there is any type error
|
|
137
|
-
// Default: () => {}
|
|
138
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
139
|
-
|
|
140
|
-
// Before declaration file be writed hook
|
|
141
|
-
// You can transform declaration file-path and content through it
|
|
142
|
-
// Default: () => {}
|
|
143
|
-
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
144
|
-
|
|
145
|
-
// After build hook
|
|
146
|
-
// It wil be called after all declaration files are written
|
|
147
|
-
// Default: () => {}
|
|
148
|
-
afterBuild?: () => void | Promise<void>
|
|
149
|
-
}
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
## Example
|
|
153
|
-
|
|
154
|
-
Clone and run the following script:
|
|
155
|
-
|
|
156
|
-
```sh
|
|
157
|
-
yarn run test:e2e
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
Then check `example/types`.
|
|
161
|
-
|
|
162
|
-
## FAQ
|
|
163
|
-
|
|
164
|
-
Here will include some FAQ's and provide some solutions.
|
|
165
|
-
|
|
166
|
-
### Missing some declaration files after build
|
|
167
|
-
|
|
168
|
-
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).
|
|
169
|
-
|
|
170
|
-
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.
|
|
171
|
-
|
|
172
|
-
## License
|
|
173
|
-
|
|
174
|
-
MIT License.
|
|
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
|
+
yarn 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
|
+
// Project init compilerOptions using by ts-morph
|
|
86
|
+
// Default: null
|
|
87
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
88
|
+
|
|
89
|
+
// Project init tsconfig.json file path by ts-morph
|
|
90
|
+
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
91
|
+
// Default: 'tsconfig.json'
|
|
92
|
+
tsConfigFilePath?: string
|
|
93
|
+
|
|
94
|
+
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
95
|
+
// Default: false
|
|
96
|
+
cleanVueFileName?: boolean
|
|
97
|
+
|
|
98
|
+
// Whether transform dynamic import to static
|
|
99
|
+
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
100
|
+
// Default: false
|
|
101
|
+
staticImport?: boolean
|
|
102
|
+
|
|
103
|
+
// Manual set include glob
|
|
104
|
+
// Defaults base on your tsconfig.json include option
|
|
105
|
+
include?: string | string[]
|
|
106
|
+
|
|
107
|
+
// Manual set exclude glob
|
|
108
|
+
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
109
|
+
exclude?: string | string[]
|
|
110
|
+
|
|
111
|
+
// Whether generate types entry file
|
|
112
|
+
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
113
|
+
// Default: false
|
|
114
|
+
insertTypesEntry?: boolean
|
|
115
|
+
|
|
116
|
+
// Whether copy .d.ts source files into outputDir
|
|
117
|
+
// Default: true
|
|
118
|
+
copyDtsFiles?: boolean
|
|
119
|
+
|
|
120
|
+
// Whether emit nothing when has any diagnostic
|
|
121
|
+
// Default: false
|
|
122
|
+
noEmitOnError?: boolean
|
|
123
|
+
|
|
124
|
+
// Whether skip typescript diagnostics
|
|
125
|
+
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
126
|
+
// But for the source files with type errors will not be emitted
|
|
127
|
+
// Default: true
|
|
128
|
+
skipDiagnostics?: boolean
|
|
129
|
+
|
|
130
|
+
// Whether log diagnostic informations
|
|
131
|
+
// Not effective when `skipDiagnostics` is true
|
|
132
|
+
// Default: false
|
|
133
|
+
logDiagnostics?: boolean
|
|
134
|
+
|
|
135
|
+
// After emit diagnostic hook
|
|
136
|
+
// According to the length to judge whether there is any type error
|
|
137
|
+
// Default: () => {}
|
|
138
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
139
|
+
|
|
140
|
+
// Before declaration file be writed hook
|
|
141
|
+
// You can transform declaration file-path and content through it
|
|
142
|
+
// Default: () => {}
|
|
143
|
+
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
144
|
+
|
|
145
|
+
// After build hook
|
|
146
|
+
// It wil be called after all declaration files are written
|
|
147
|
+
// Default: () => {}
|
|
148
|
+
afterBuild?: () => void | Promise<void>
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Example
|
|
153
|
+
|
|
154
|
+
Clone and run the following script:
|
|
155
|
+
|
|
156
|
+
```sh
|
|
157
|
+
yarn run test:e2e
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Then check `example/types`.
|
|
161
|
+
|
|
162
|
+
## FAQ
|
|
163
|
+
|
|
164
|
+
Here will include some FAQ's and provide some solutions.
|
|
165
|
+
|
|
166
|
+
### Missing some declaration files after build
|
|
167
|
+
|
|
168
|
+
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).
|
|
169
|
+
|
|
170
|
+
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.
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
MIT License.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,173 +1,173 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**中文** | [English](./README.md)
|
|
4
|
-
|
|
5
|
-
一款用于从 `.ts` 或 `.vue` 源文件生成 `.d.ts` 文件的 Vite 插件。
|
|
6
|
-
|
|
7
|
-
## 安装
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
yarn add vite-plugin-dts -D
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## 使用
|
|
14
|
-
|
|
15
|
-
在 `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
|
-
在你的组件中:
|
|
36
|
-
|
|
37
|
-
```vue
|
|
38
|
-
<template>
|
|
39
|
-
<div></div>
|
|
40
|
-
</template>
|
|
41
|
-
|
|
42
|
-
<script lang="ts">
|
|
43
|
-
// 使用 defineComponent 来进行类型推断
|
|
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
|
-
// 尽管没有直接使用 props,你仍需要接收 defineProps 的返回值
|
|
55
|
-
const props = defineProps<{
|
|
56
|
-
color: 'blue' | 'red'
|
|
57
|
-
}>()
|
|
58
|
-
</script>
|
|
59
|
-
|
|
60
|
-
<template>
|
|
61
|
-
<div>{{ color }}</div>
|
|
62
|
-
</template>
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## 选项
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
import type { ts, Diagnostic } from 'ts-morph'
|
|
69
|
-
|
|
70
|
-
interface TransformWriteFile {
|
|
71
|
-
filePath?: string
|
|
72
|
-
content?: string
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface PluginOptions {
|
|
76
|
-
// 执行的根目录
|
|
77
|
-
// 默认基于 vite 配置的 root 选项
|
|
78
|
-
root?: string
|
|
79
|
-
|
|
80
|
-
// 声明文件的输出目录
|
|
81
|
-
// 默认基于 vite 配置的输出目录
|
|
82
|
-
outputDir?: string
|
|
83
|
-
|
|
84
|
-
// 提供给 ts-morph Project 初始化的 compilerOptions 选项
|
|
85
|
-
// 默认值: null
|
|
86
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
87
|
-
|
|
88
|
-
// 提供给 ts-morph Project 初始化的 tsconfig.json 路径
|
|
89
|
-
// 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
|
|
90
|
-
// 默认值: 'tsconfig.json'
|
|
91
|
-
tsConfigFilePath?: string
|
|
92
|
-
|
|
93
|
-
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
94
|
-
// 默认值: false
|
|
95
|
-
cleanVueFileName?: boolean
|
|
96
|
-
|
|
97
|
-
//是否将动态引入转换为静态
|
|
98
|
-
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
99
|
-
// 默认值: false
|
|
100
|
-
staticImport?: boolean
|
|
101
|
-
|
|
102
|
-
// 手动设置包含路径的 glob
|
|
103
|
-
// 默认基于 tsconfig.json 的 include 选项
|
|
104
|
-
include?: string | string[]
|
|
105
|
-
|
|
106
|
-
// 手动设置排除路径的 glob
|
|
107
|
-
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
108
|
-
exclude?: string | string[]
|
|
109
|
-
|
|
110
|
-
// 是否生成类型声明入口
|
|
111
|
-
// 当为 true 时会基于 package.json 的 tpyes 字段生成,或者 `${outputDir}/index.d.ts`
|
|
112
|
-
// 默认值: false
|
|
113
|
-
insertTypesEntry?: boolean
|
|
114
|
-
|
|
115
|
-
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
116
|
-
// 默认值: true
|
|
117
|
-
copyDtsFiles?: boolean
|
|
118
|
-
|
|
119
|
-
// 出现类型诊断信息时不生成类型文件
|
|
120
|
-
// 默认值: false
|
|
121
|
-
noEmitOnError?: boolean
|
|
122
|
-
|
|
123
|
-
// 是否跳过类型诊断
|
|
124
|
-
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
125
|
-
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
126
|
-
// 默认值: true
|
|
127
|
-
skipDiagnostics?: boolean
|
|
128
|
-
|
|
129
|
-
// 是否打印类型诊断信息
|
|
130
|
-
// 当跳过类型诊断时该属性将不会生效
|
|
131
|
-
// 默认值: false
|
|
132
|
-
logDiagnostics?: boolean
|
|
133
|
-
|
|
134
|
-
// 获取诊断信息后的钩子
|
|
135
|
-
// 可以根据参数 length 来判断有误类型错误
|
|
136
|
-
// 默认值: () => {}
|
|
137
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
138
|
-
|
|
139
|
-
// 类型声明文件被写入前的钩子
|
|
140
|
-
// 可以在钩子里转换文件路径和文件内容
|
|
141
|
-
// 默认值: () => {}
|
|
142
|
-
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
143
|
-
|
|
144
|
-
// 构建后回调钩子
|
|
145
|
-
// 将会在所有类型文件被写入后调用
|
|
146
|
-
// 默认值: () => {}
|
|
147
|
-
afterBuild?: () => void | Promise<void>
|
|
148
|
-
}
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
## 示例
|
|
152
|
-
|
|
153
|
-
克隆项目然后执行下列命令:
|
|
154
|
-
|
|
155
|
-
```sh
|
|
156
|
-
yarn run test:e2e
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
然后检查 `example/types` 目录。
|
|
160
|
-
|
|
161
|
-
## 常见问题
|
|
162
|
-
|
|
163
|
-
此处将收录一些常见的问题并提供一些解决方案。
|
|
164
|
-
|
|
165
|
-
### 打包后出现类型文件缺失
|
|
166
|
-
|
|
167
|
-
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
168
|
-
|
|
169
|
-
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
170
|
-
|
|
171
|
-
## 授权
|
|
172
|
-
|
|
173
|
-
MIT 授权。
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**中文** | [English](./README.md)
|
|
4
|
+
|
|
5
|
+
一款用于从 `.ts` 或 `.vue` 源文件生成 `.d.ts` 文件的 Vite 插件。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
yarn add vite-plugin-dts -D
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用
|
|
14
|
+
|
|
15
|
+
在 `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
|
+
在你的组件中:
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div></div>
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
<script lang="ts">
|
|
43
|
+
// 使用 defineComponent 来进行类型推断
|
|
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
|
+
// 尽管没有直接使用 props,你仍需要接收 defineProps 的返回值
|
|
55
|
+
const props = defineProps<{
|
|
56
|
+
color: 'blue' | 'red'
|
|
57
|
+
}>()
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<div>{{ color }}</div>
|
|
62
|
+
</template>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## 选项
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import type { ts, Diagnostic } from 'ts-morph'
|
|
69
|
+
|
|
70
|
+
interface TransformWriteFile {
|
|
71
|
+
filePath?: string
|
|
72
|
+
content?: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PluginOptions {
|
|
76
|
+
// 执行的根目录
|
|
77
|
+
// 默认基于 vite 配置的 root 选项
|
|
78
|
+
root?: string
|
|
79
|
+
|
|
80
|
+
// 声明文件的输出目录
|
|
81
|
+
// 默认基于 vite 配置的输出目录
|
|
82
|
+
outputDir?: string
|
|
83
|
+
|
|
84
|
+
// 提供给 ts-morph Project 初始化的 compilerOptions 选项
|
|
85
|
+
// 默认值: null
|
|
86
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
87
|
+
|
|
88
|
+
// 提供给 ts-morph Project 初始化的 tsconfig.json 路径
|
|
89
|
+
// 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
|
|
90
|
+
// 默认值: 'tsconfig.json'
|
|
91
|
+
tsConfigFilePath?: string
|
|
92
|
+
|
|
93
|
+
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
94
|
+
// 默认值: false
|
|
95
|
+
cleanVueFileName?: boolean
|
|
96
|
+
|
|
97
|
+
//是否将动态引入转换为静态
|
|
98
|
+
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
99
|
+
// 默认值: false
|
|
100
|
+
staticImport?: boolean
|
|
101
|
+
|
|
102
|
+
// 手动设置包含路径的 glob
|
|
103
|
+
// 默认基于 tsconfig.json 的 include 选项
|
|
104
|
+
include?: string | string[]
|
|
105
|
+
|
|
106
|
+
// 手动设置排除路径的 glob
|
|
107
|
+
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
108
|
+
exclude?: string | string[]
|
|
109
|
+
|
|
110
|
+
// 是否生成类型声明入口
|
|
111
|
+
// 当为 true 时会基于 package.json 的 tpyes 字段生成,或者 `${outputDir}/index.d.ts`
|
|
112
|
+
// 默认值: false
|
|
113
|
+
insertTypesEntry?: boolean
|
|
114
|
+
|
|
115
|
+
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
116
|
+
// 默认值: true
|
|
117
|
+
copyDtsFiles?: boolean
|
|
118
|
+
|
|
119
|
+
// 出现类型诊断信息时不生成类型文件
|
|
120
|
+
// 默认值: false
|
|
121
|
+
noEmitOnError?: boolean
|
|
122
|
+
|
|
123
|
+
// 是否跳过类型诊断
|
|
124
|
+
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
125
|
+
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
126
|
+
// 默认值: true
|
|
127
|
+
skipDiagnostics?: boolean
|
|
128
|
+
|
|
129
|
+
// 是否打印类型诊断信息
|
|
130
|
+
// 当跳过类型诊断时该属性将不会生效
|
|
131
|
+
// 默认值: false
|
|
132
|
+
logDiagnostics?: boolean
|
|
133
|
+
|
|
134
|
+
// 获取诊断信息后的钩子
|
|
135
|
+
// 可以根据参数 length 来判断有误类型错误
|
|
136
|
+
// 默认值: () => {}
|
|
137
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
138
|
+
|
|
139
|
+
// 类型声明文件被写入前的钩子
|
|
140
|
+
// 可以在钩子里转换文件路径和文件内容
|
|
141
|
+
// 默认值: () => {}
|
|
142
|
+
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
143
|
+
|
|
144
|
+
// 构建后回调钩子
|
|
145
|
+
// 将会在所有类型文件被写入后调用
|
|
146
|
+
// 默认值: () => {}
|
|
147
|
+
afterBuild?: () => void | Promise<void>
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## 示例
|
|
152
|
+
|
|
153
|
+
克隆项目然后执行下列命令:
|
|
154
|
+
|
|
155
|
+
```sh
|
|
156
|
+
yarn run test:e2e
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
然后检查 `example/types` 目录。
|
|
160
|
+
|
|
161
|
+
## 常见问题
|
|
162
|
+
|
|
163
|
+
此处将收录一些常见的问题并提供一些解决方案。
|
|
164
|
+
|
|
165
|
+
### 打包后出现类型文件缺失
|
|
166
|
+
|
|
167
|
+
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
168
|
+
|
|
169
|
+
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
170
|
+
|
|
171
|
+
## 授权
|
|
172
|
+
|
|
173
|
+
MIT 授权。
|
package/dist/index.js
CHANGED
|
@@ -141292,7 +141292,7 @@ function isAliasMatch(alias, importee) {
|
|
|
141292
141292
|
return false;
|
|
141293
141293
|
if (importee === alias.find)
|
|
141294
141294
|
return true;
|
|
141295
|
-
return importee.indexOf(alias.find) === 0 && importee.substring(alias.find.length)[0] === "/";
|
|
141295
|
+
return importee.indexOf(alias.find) === 0 && (alias.find.endsWith("/") || importee.substring(alias.find.length)[0] === "/");
|
|
141296
141296
|
}
|
|
141297
141297
|
var globalImportRE = /(?:(?:import|export)\s?(?:type)?\s?(?:(?:\{[^;\n]+\})|(?:[^;\n]+))\s?from\s?['"][^;\n]+['"])|(?:import\(['"][^;\n]+?['"]\))/g;
|
|
141298
141298
|
var staticImportRE = /(?:import|export)\s?(?:type)?\s?\{?.+\}?\s?from\s?['"](.+)['"]/;
|
|
@@ -141313,7 +141313,7 @@ function transformAliasImport(filePath, content, aliases) {
|
|
|
141313
141313
|
const matchedAlias = aliases.find((alias) => isAliasMatch(alias, matchResult[1]));
|
|
141314
141314
|
if (matchedAlias) {
|
|
141315
141315
|
const truthPath = (0, import_path2.isAbsolute)(matchedAlias.replacement) ? (0, import_vite.normalizePath)((0, import_path2.relative)((0, import_path2.dirname)(filePath), matchedAlias.replacement)) : (0, import_vite.normalizePath)(matchedAlias.replacement);
|
|
141316
|
-
return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, truthPath.startsWith(".") ? truthPath : `./${truthPath}`)}'${isDynamic ? ")" : ""}`);
|
|
141316
|
+
return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, (truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : ""))}'${isDynamic ? ")" : ""}`);
|
|
141317
141317
|
}
|
|
141318
141318
|
}
|
|
141319
141319
|
return str;
|
|
@@ -141527,8 +141527,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141527
141527
|
files.forEach((file) => {
|
|
141528
141528
|
includedFileSet.add(dtsRE.test(file) ? file : `${tjsRE.test(file) ? file.replace(tjsRE, "") : file}.d.ts`);
|
|
141529
141529
|
if (dtsRE.test(file)) {
|
|
141530
|
-
project.addSourceFileAtPath(file);
|
|
141531
|
-
sourceDtsFiles.add(file);
|
|
141530
|
+
sourceDtsFiles.add(project.addSourceFileAtPath(file));
|
|
141532
141531
|
}
|
|
141533
141532
|
});
|
|
141534
141533
|
if (hasJsVue) {
|
|
@@ -141553,18 +141552,22 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141553
141552
|
bundleDebug("diagnostics");
|
|
141554
141553
|
}
|
|
141555
141554
|
const service = project.getLanguageService();
|
|
141556
|
-
const outputFiles = project.getSourceFiles().map((sourceFile) => {
|
|
141557
|
-
|
|
141558
|
-
|
|
141555
|
+
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
141556
|
+
path: outputFile.getFilePath(),
|
|
141557
|
+
content: outputFile.getText()
|
|
141558
|
+
}))).flat().concat(Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
141559
|
+
path: sourceFile.getFilePath(),
|
|
141560
|
+
content: sourceFile.getFullText()
|
|
141561
|
+
})));
|
|
141559
141562
|
bundleDebug("emit");
|
|
141560
141563
|
await runParallel(import_os.default.cpus().length, outputFiles, async (outputFile) => {
|
|
141561
141564
|
var _a3, _b2;
|
|
141562
|
-
let filePath = outputFile.
|
|
141563
|
-
let content = outputFile.
|
|
141565
|
+
let filePath = outputFile.path;
|
|
141566
|
+
let content = outputFile.content;
|
|
141564
141567
|
const isMapFile = filePath.endsWith(".map");
|
|
141565
141568
|
if (!includedFileSet.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport)
|
|
141566
141569
|
return;
|
|
141567
|
-
if (!isMapFile && content !== noneExport) {
|
|
141570
|
+
if (!isMapFile && content && content !== noneExport) {
|
|
141568
141571
|
content = clearPureImport ? removePureImport(content) : content;
|
|
141569
141572
|
content = transformAliasImport(filePath, content, aliases);
|
|
141570
141573
|
content = staticImport ? transformDynamicImport(content) : content;
|
|
@@ -141581,28 +141584,6 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141581
141584
|
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf8");
|
|
141582
141585
|
});
|
|
141583
141586
|
bundleDebug("output");
|
|
141584
|
-
await Promise.all(Array.from(sourceDtsFiles).map(async (originPath) => {
|
|
141585
|
-
var _a3, _b2;
|
|
141586
|
-
let filePath = (0, import_path3.resolve)(outputDir, (0, import_path3.relative)(root, originPath));
|
|
141587
|
-
let content = "";
|
|
141588
|
-
let isRewrite = false;
|
|
141589
|
-
if (typeof beforeWriteFile === "function") {
|
|
141590
|
-
content = await import_fs_extra.default.readFile(originPath, "utf-8");
|
|
141591
|
-
const result = beforeWriteFile(filePath, content);
|
|
141592
|
-
if (result && isNativeObj(result)) {
|
|
141593
|
-
filePath = (_a3 = result.filePath) != null ? _a3 : filePath;
|
|
141594
|
-
isRewrite = result.content !== content;
|
|
141595
|
-
content = (_b2 = result.content) != null ? _b2 : content;
|
|
141596
|
-
}
|
|
141597
|
-
}
|
|
141598
|
-
await import_fs_extra.default.mkdir((0, import_path3.dirname)(filePath), { recursive: true });
|
|
141599
|
-
if (isRewrite) {
|
|
141600
|
-
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
141601
|
-
} else {
|
|
141602
|
-
await import_fs_extra.default.copyFile(originPath, filePath);
|
|
141603
|
-
}
|
|
141604
|
-
}));
|
|
141605
|
-
bundleDebug("copy dts");
|
|
141606
141587
|
if (insertTypesEntry) {
|
|
141607
141588
|
const pkgPath = (0, import_path3.resolve)(root, "package.json");
|
|
141608
141589
|
const pkg = import_fs_extra.default.existsSync(pkgPath) ? JSON.parse(await import_fs_extra.default.readFile(pkgPath, "utf-8")) : {};
|
package/dist/index.mjs
CHANGED
|
@@ -141251,7 +141251,7 @@ function isAliasMatch(alias, importee) {
|
|
|
141251
141251
|
return false;
|
|
141252
141252
|
if (importee === alias.find)
|
|
141253
141253
|
return true;
|
|
141254
|
-
return importee.indexOf(alias.find) === 0 && importee.substring(alias.find.length)[0] === "/";
|
|
141254
|
+
return importee.indexOf(alias.find) === 0 && (alias.find.endsWith("/") || importee.substring(alias.find.length)[0] === "/");
|
|
141255
141255
|
}
|
|
141256
141256
|
var globalImportRE = /(?:(?:import|export)\s?(?:type)?\s?(?:(?:\{[^;\n]+\})|(?:[^;\n]+))\s?from\s?['"][^;\n]+['"])|(?:import\(['"][^;\n]+?['"]\))/g;
|
|
141257
141257
|
var staticImportRE = /(?:import|export)\s?(?:type)?\s?\{?.+\}?\s?from\s?['"](.+)['"]/;
|
|
@@ -141272,7 +141272,7 @@ function transformAliasImport(filePath, content, aliases) {
|
|
|
141272
141272
|
const matchedAlias = aliases.find((alias) => isAliasMatch(alias, matchResult[1]));
|
|
141273
141273
|
if (matchedAlias) {
|
|
141274
141274
|
const truthPath = isAbsolute2(matchedAlias.replacement) ? normalizePath(relative(dirname(filePath), matchedAlias.replacement)) : normalizePath(matchedAlias.replacement);
|
|
141275
|
-
return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, truthPath.startsWith(".") ? truthPath : `./${truthPath}`)}'${isDynamic ? ")" : ""}`);
|
|
141275
|
+
return str.replace(isDynamic ? simpleDynamicImportRE : simpleStaticImportRE, `$1'${matchResult[1].replace(matchedAlias.find, (truthPath.startsWith(".") ? truthPath : `./${truthPath}`) + (typeof matchedAlias.find === "string" && matchedAlias.find.endsWith("/") ? "/" : ""))}'${isDynamic ? ")" : ""}`);
|
|
141276
141276
|
}
|
|
141277
141277
|
}
|
|
141278
141278
|
return str;
|
|
@@ -141485,8 +141485,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141485
141485
|
files.forEach((file) => {
|
|
141486
141486
|
includedFileSet.add(dtsRE.test(file) ? file : `${tjsRE.test(file) ? file.replace(tjsRE, "") : file}.d.ts`);
|
|
141487
141487
|
if (dtsRE.test(file)) {
|
|
141488
|
-
project.addSourceFileAtPath(file);
|
|
141489
|
-
sourceDtsFiles.add(file);
|
|
141488
|
+
sourceDtsFiles.add(project.addSourceFileAtPath(file));
|
|
141490
141489
|
}
|
|
141491
141490
|
});
|
|
141492
141491
|
if (hasJsVue) {
|
|
@@ -141511,18 +141510,22 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141511
141510
|
bundleDebug("diagnostics");
|
|
141512
141511
|
}
|
|
141513
141512
|
const service = project.getLanguageService();
|
|
141514
|
-
const outputFiles = project.getSourceFiles().map((sourceFile) => {
|
|
141515
|
-
|
|
141516
|
-
|
|
141513
|
+
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
141514
|
+
path: outputFile.getFilePath(),
|
|
141515
|
+
content: outputFile.getText()
|
|
141516
|
+
}))).flat().concat(Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
141517
|
+
path: sourceFile.getFilePath(),
|
|
141518
|
+
content: sourceFile.getFullText()
|
|
141519
|
+
})));
|
|
141517
141520
|
bundleDebug("emit");
|
|
141518
141521
|
await runParallel(os2.cpus().length, outputFiles, async (outputFile) => {
|
|
141519
141522
|
var _a3, _b2;
|
|
141520
|
-
let filePath = outputFile.
|
|
141521
|
-
let content = outputFile.
|
|
141523
|
+
let filePath = outputFile.path;
|
|
141524
|
+
let content = outputFile.content;
|
|
141522
141525
|
const isMapFile = filePath.endsWith(".map");
|
|
141523
141526
|
if (!includedFileSet.has(isMapFile ? filePath.slice(0, -4) : filePath) || clearPureImport && content === noneExport)
|
|
141524
141527
|
return;
|
|
141525
|
-
if (!isMapFile && content !== noneExport) {
|
|
141528
|
+
if (!isMapFile && content && content !== noneExport) {
|
|
141526
141529
|
content = clearPureImport ? removePureImport(content) : content;
|
|
141527
141530
|
content = transformAliasImport(filePath, content, aliases);
|
|
141528
141531
|
content = staticImport ? transformDynamicImport(content) : content;
|
|
@@ -141539,28 +141542,6 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
141539
141542
|
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf8");
|
|
141540
141543
|
});
|
|
141541
141544
|
bundleDebug("output");
|
|
141542
|
-
await Promise.all(Array.from(sourceDtsFiles).map(async (originPath) => {
|
|
141543
|
-
var _a3, _b2;
|
|
141544
|
-
let filePath = resolve2(outputDir, relative2(root, originPath));
|
|
141545
|
-
let content = "";
|
|
141546
|
-
let isRewrite = false;
|
|
141547
|
-
if (typeof beforeWriteFile === "function") {
|
|
141548
|
-
content = await fs.readFile(originPath, "utf-8");
|
|
141549
|
-
const result = beforeWriteFile(filePath, content);
|
|
141550
|
-
if (result && isNativeObj(result)) {
|
|
141551
|
-
filePath = (_a3 = result.filePath) != null ? _a3 : filePath;
|
|
141552
|
-
isRewrite = result.content !== content;
|
|
141553
|
-
content = (_b2 = result.content) != null ? _b2 : content;
|
|
141554
|
-
}
|
|
141555
|
-
}
|
|
141556
|
-
await fs.mkdir(dirname2(filePath), { recursive: true });
|
|
141557
|
-
if (isRewrite) {
|
|
141558
|
-
await fs.writeFile(filePath, content, "utf-8");
|
|
141559
|
-
} else {
|
|
141560
|
-
await fs.copyFile(originPath, filePath);
|
|
141561
|
-
}
|
|
141562
|
-
}));
|
|
141563
|
-
bundleDebug("copy dts");
|
|
141564
141545
|
if (insertTypesEntry) {
|
|
141565
141546
|
const pkgPath = resolve2(root, "package.json");
|
|
141566
141547
|
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
package/package.json
CHANGED