vite-plugin-dts 1.2.0 → 1.3.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 +18 -2
- package/README.zh-CN.md +207 -191
- package/dist/index.d.ts +1 -1
- package/dist/index.js +43 -16
- package/dist/index.mjs +43 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**English** | [中文](./README.zh-CN.md)
|
|
4
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).
|
|
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
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -79,8 +79,9 @@ export interface PluginOptions {
|
|
|
79
79
|
root?: string
|
|
80
80
|
|
|
81
81
|
// Declaration files output directory
|
|
82
|
+
// Can be specified a array to output to multiple directories
|
|
82
83
|
// Defaults base on your vite config output options
|
|
83
|
-
outputDir?: string
|
|
84
|
+
outputDir?: string | string[]
|
|
84
85
|
|
|
85
86
|
// Manually set the root path of the entry files
|
|
86
87
|
// The output path of each file will be caculated base on it
|
|
@@ -187,6 +188,21 @@ This is usually caused by using `defineComponent` function in both `script` and
|
|
|
187
188
|
|
|
188
189
|
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
|
|
|
191
|
+
### Take errors that unable to infer types from packages which under `node_modules`
|
|
192
|
+
|
|
193
|
+
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:
|
|
194
|
+
|
|
195
|
+
```json
|
|
196
|
+
{
|
|
197
|
+
"compilerOptions": {
|
|
198
|
+
"baseUrl": ".",
|
|
199
|
+
"paths": {
|
|
200
|
+
"third-lib": ["node_modules/third-lib"]
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
190
206
|
## License
|
|
191
207
|
|
|
192
208
|
MIT License.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,191 +1,207 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**中文** | [English](./README.md)
|
|
4
|
-
|
|
5
|
-
一款用于在 [库模式](https://cn.vitejs.dev/guide/build.html#library-mode) 中,从 `.ts` 或 `.vue` 源文件生成类型文件(`.d.ts`)的 Vite 插件。
|
|
6
|
-
|
|
7
|
-
## 安装
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
pnpm 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
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
//
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
//
|
|
114
|
-
exclude
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
//
|
|
137
|
-
//
|
|
138
|
-
//
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
//
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
//
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
//
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**中文** | [English](./README.md)
|
|
4
|
+
|
|
5
|
+
一款用于在 [库模式](https://cn.vitejs.dev/guide/build.html#library-mode) 中,从 `.ts(x)` 或 `.vue` 源文件生成类型文件(`.d.ts`)的 Vite 插件。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm 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
|
+
// 可以指定一个数组来输出到多个目录中
|
|
82
|
+
// 默认基于 vite 配置的输出目录
|
|
83
|
+
outputDir?: string | string[]
|
|
84
|
+
|
|
85
|
+
// 用于手动设置入口文件的根路径
|
|
86
|
+
// 在计算每个文件的输出路径时将基于该路径
|
|
87
|
+
// 默认为所有文件的最小公共路径
|
|
88
|
+
entryRoot?: string
|
|
89
|
+
|
|
90
|
+
// 提供给 ts-morph Project 初始化的 compilerOptions 选项
|
|
91
|
+
// 默认值: null
|
|
92
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
93
|
+
|
|
94
|
+
// 提供给 ts-morph Project 初始化的 tsconfig.json 路径
|
|
95
|
+
// 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
|
|
96
|
+
// 默认值: 'tsconfig.json'
|
|
97
|
+
tsConfigFilePath?: string
|
|
98
|
+
|
|
99
|
+
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
100
|
+
// 默认值: false
|
|
101
|
+
cleanVueFileName?: boolean
|
|
102
|
+
|
|
103
|
+
//是否将动态引入转换为静态
|
|
104
|
+
// 当开启打包类型文件时强制为 true
|
|
105
|
+
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
106
|
+
// 默认值: false
|
|
107
|
+
staticImport?: boolean
|
|
108
|
+
|
|
109
|
+
// 手动设置包含路径的 glob
|
|
110
|
+
// 默认基于 tsconfig.json 的 include 选项
|
|
111
|
+
include?: string | string[]
|
|
112
|
+
|
|
113
|
+
// 手动设置排除路径的 glob
|
|
114
|
+
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
115
|
+
exclude?: string | string[]
|
|
116
|
+
|
|
117
|
+
// 是否生成类型声明入口
|
|
118
|
+
// 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
|
|
119
|
+
// 当开启打包类型文件时强制为 true
|
|
120
|
+
// 默认值: false
|
|
121
|
+
insertTypesEntry?: boolean
|
|
122
|
+
|
|
123
|
+
// 设置是否在发出类型文件后将其打包
|
|
124
|
+
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
125
|
+
// 默认值: false
|
|
126
|
+
rollupTypes?: boolean
|
|
127
|
+
|
|
128
|
+
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
129
|
+
// 默认值: true
|
|
130
|
+
copyDtsFiles?: boolean
|
|
131
|
+
|
|
132
|
+
// 出现类型诊断信息时不生成类型文件
|
|
133
|
+
// 默认值: false
|
|
134
|
+
noEmitOnError?: boolean
|
|
135
|
+
|
|
136
|
+
// 是否跳过类型诊断
|
|
137
|
+
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
138
|
+
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
139
|
+
// 默认值: true
|
|
140
|
+
skipDiagnostics?: boolean
|
|
141
|
+
|
|
142
|
+
// 是否打印类型诊断信息
|
|
143
|
+
// 当跳过类型诊断时该属性将不会生效
|
|
144
|
+
// 默认值: false
|
|
145
|
+
logDiagnostics?: boolean
|
|
146
|
+
|
|
147
|
+
// 获取诊断信息后的钩子
|
|
148
|
+
// 可以根据参数 length 来判断有误类型错误
|
|
149
|
+
// 默认值: () => {}
|
|
150
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
151
|
+
|
|
152
|
+
// 类型声明文件被写入前的钩子
|
|
153
|
+
// 可以在钩子里转换文件路径和文件内容
|
|
154
|
+
// 默认值: () => {}
|
|
155
|
+
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
156
|
+
|
|
157
|
+
// 构建后回调钩子
|
|
158
|
+
// 将会在所有类型文件被写入后调用
|
|
159
|
+
// 默认值: () => {}
|
|
160
|
+
afterBuild?: () => void | Promise<void>
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 示例
|
|
165
|
+
|
|
166
|
+
克隆项目然后执行下列命令:
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
pnpm run test:e2e
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
然后检查 `example/types` 目录。
|
|
173
|
+
|
|
174
|
+
## 常见问题
|
|
175
|
+
|
|
176
|
+
此处将收录一些常见的问题并提供一些解决方案。
|
|
177
|
+
|
|
178
|
+
### 打包后出现类型文件缺失
|
|
179
|
+
|
|
180
|
+
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
181
|
+
|
|
182
|
+
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
183
|
+
|
|
184
|
+
### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
|
|
185
|
+
|
|
186
|
+
这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
|
|
187
|
+
|
|
188
|
+
这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
|
|
189
|
+
|
|
190
|
+
### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
|
|
191
|
+
|
|
192
|
+
这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
|
|
193
|
+
|
|
194
|
+
```json
|
|
195
|
+
{
|
|
196
|
+
"compilerOptions": {
|
|
197
|
+
"baseUrl": ".",
|
|
198
|
+
"paths": {
|
|
199
|
+
"third-lib": ["node_modules/third-lib"]
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## 授权
|
|
206
|
+
|
|
207
|
+
MIT 授权。
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ interface PluginOptions {
|
|
|
9
9
|
include?: string | string[];
|
|
10
10
|
exclude?: string | string[];
|
|
11
11
|
root?: string;
|
|
12
|
-
outputDir?: string;
|
|
12
|
+
outputDir?: string | string[];
|
|
13
13
|
entryRoot?: string;
|
|
14
14
|
compilerOptions?: ts.CompilerOptions | null;
|
|
15
15
|
tsConfigFilePath?: string;
|
package/dist/index.js
CHANGED
|
@@ -299,20 +299,21 @@ function compileVueCode(code) {
|
|
|
299
299
|
id: `${index++}`
|
|
300
300
|
});
|
|
301
301
|
const classMatch = compiled.content.match(exportDefaultClassRE);
|
|
302
|
+
const plugins = scriptSetup.lang === "ts" ? ["typescript"] : void 0;
|
|
302
303
|
if (classMatch) {
|
|
303
304
|
content = compiled.content.replace(exportDefaultClassRE, "\nclass $1") + `
|
|
304
305
|
const _sfc_main = ${classMatch[1]}`;
|
|
305
306
|
if (exportDefaultRE.test(content)) {
|
|
306
|
-
content = rewriteDefault(compiled.content, "_sfc_main");
|
|
307
|
+
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
307
308
|
}
|
|
308
309
|
} else {
|
|
309
|
-
content = rewriteDefault(compiled.content, "_sfc_main");
|
|
310
|
+
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
310
311
|
}
|
|
311
312
|
content = transferSetupPosition(content);
|
|
312
313
|
content += "\nexport default _sfc_main\n";
|
|
313
314
|
ext = scriptSetup.lang || "js";
|
|
314
315
|
} else if (script && script.content) {
|
|
315
|
-
content = rewriteDefault(script.content, "_sfc_main");
|
|
316
|
+
content = rewriteDefault(script.content, "_sfc_main", script.lang === "ts" ? ["typescript"] : void 0);
|
|
316
317
|
content += "\nexport default _sfc_main\n";
|
|
317
318
|
ext = script.lang || "js";
|
|
318
319
|
}
|
|
@@ -444,7 +445,7 @@ function dtsPlugin(options = {}) {
|
|
|
444
445
|
let logger;
|
|
445
446
|
let project;
|
|
446
447
|
let tsConfigPath;
|
|
447
|
-
let
|
|
448
|
+
let outputDirs;
|
|
448
449
|
let isBundle = false;
|
|
449
450
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
450
451
|
let hasJsVue = false;
|
|
@@ -468,7 +469,7 @@ function dtsPlugin(options = {}) {
|
|
|
468
469
|
if (aliasesExclude.length > 0) {
|
|
469
470
|
aliases = aliases.filter(({ find }) => !aliasesExclude.some((alias) => {
|
|
470
471
|
var _a3;
|
|
471
|
-
return
|
|
472
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a3 = find.match(alias)) == null ? void 0 : _a3[0] : find === alias);
|
|
472
473
|
}));
|
|
473
474
|
}
|
|
474
475
|
},
|
|
@@ -493,8 +494,8 @@ ${import_chalk.default.cyan("[vite:dts]")} You building not a library that may n
|
|
|
493
494
|
}
|
|
494
495
|
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
495
496
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
496
|
-
|
|
497
|
-
if (!
|
|
497
|
+
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
498
|
+
if (!outputDirs) {
|
|
498
499
|
logger.error(import_chalk.default.red(`
|
|
499
500
|
${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
500
501
|
`));
|
|
@@ -549,7 +550,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
549
550
|
},
|
|
550
551
|
async closeBundle() {
|
|
551
552
|
var _a2, _b2, _c, _d, _e, _f;
|
|
552
|
-
if (!
|
|
553
|
+
if (!outputDirs || !project || isBundle)
|
|
553
554
|
return;
|
|
554
555
|
logger.info(import_chalk.default.green(`
|
|
555
556
|
${logPrefix} Start generate declaration files...`));
|
|
@@ -606,7 +607,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
606
607
|
}));
|
|
607
608
|
const service = project.getLanguageService();
|
|
608
609
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
609
|
-
path:
|
|
610
|
+
path: (0, import_vite2.normalizePath)((0, import_path4.resolve)(root, outputFile.compilerObject.name)),
|
|
610
611
|
content: outputFile.getText()
|
|
611
612
|
}))).flat().concat(dtsOutputFiles);
|
|
612
613
|
bundleDebug("emit");
|
|
@@ -615,6 +616,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
615
616
|
}
|
|
616
617
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
617
618
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
619
|
+
const outputDir = outputDirs[0];
|
|
618
620
|
await runParallel(import_os.default.cpus().length, outputFiles, async (outputFile) => {
|
|
619
621
|
var _a3, _b3;
|
|
620
622
|
let filePath = outputFile.path;
|
|
@@ -637,7 +639,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
637
639
|
}
|
|
638
640
|
}
|
|
639
641
|
await import_fs_extra.default.mkdir((0, import_path4.dirname)(filePath), { recursive: true });
|
|
640
|
-
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "
|
|
642
|
+
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf-8");
|
|
641
643
|
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
642
644
|
});
|
|
643
645
|
bundleDebug("output");
|
|
@@ -648,13 +650,20 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
648
650
|
let typesPath = types ? (0, import_path4.resolve)(root, types) : (0, import_path4.resolve)(outputDir, indexName);
|
|
649
651
|
if (!import_fs_extra.default.existsSync(typesPath)) {
|
|
650
652
|
const entry = entries[0];
|
|
651
|
-
|
|
652
|
-
filePath =
|
|
653
|
+
const outputIndex = (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
654
|
+
let filePath = (0, import_vite2.normalizePath)((0, import_path4.relative)((0, import_path4.dirname)(typesPath), outputIndex));
|
|
655
|
+
filePath = filePath.replace(dtsRE2, "");
|
|
653
656
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
654
|
-
let content = `
|
|
657
|
+
let content = `export * from '${filePath}'
|
|
658
|
+
`;
|
|
659
|
+
if (import_fs_extra.default.existsSync(outputIndex)) {
|
|
660
|
+
const entryCodes = await import_fs_extra.default.readFile(outputIndex, "utf-8");
|
|
661
|
+
if (entryCodes.includes("export default")) {
|
|
662
|
+
content += `import ${libName} from '${filePath}'
|
|
655
663
|
export default ${libName}
|
|
656
|
-
export * from '${filePath}'
|
|
657
664
|
`;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
658
667
|
if (typeof beforeWriteFile === "function") {
|
|
659
668
|
const result = beforeWriteFile(typesPath, content);
|
|
660
669
|
if (result && isNativeObj(result)) {
|
|
@@ -663,6 +672,7 @@ export * from '${filePath}'
|
|
|
663
672
|
}
|
|
664
673
|
}
|
|
665
674
|
await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
|
|
675
|
+
wroteFiles.add((0, import_vite2.normalizePath)(typesPath));
|
|
666
676
|
}
|
|
667
677
|
bundleDebug("insert index");
|
|
668
678
|
if (rollupTypes) {
|
|
@@ -675,17 +685,34 @@ export * from '${filePath}'
|
|
|
675
685
|
entryPath: typesPath,
|
|
676
686
|
fileName: (0, import_path4.basename)(typesPath)
|
|
677
687
|
});
|
|
678
|
-
|
|
688
|
+
const wroteFile = (0, import_vite2.normalizePath)(typesPath);
|
|
689
|
+
wroteFiles.delete(wroteFile);
|
|
679
690
|
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), (f) => import_fs_extra.default.unlink(f));
|
|
680
691
|
removeDirIfEmpty(outputDir);
|
|
692
|
+
wroteFiles.clear();
|
|
693
|
+
wroteFiles.add(wroteFile);
|
|
681
694
|
if (copyDtsFiles) {
|
|
682
695
|
await runParallel(import_os.default.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
683
|
-
|
|
696
|
+
const filePath = (0, import_path4.resolve)(outputDir, (0, import_path4.basename)(path));
|
|
697
|
+
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
698
|
+
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
684
699
|
});
|
|
685
700
|
}
|
|
686
701
|
bundleDebug("rollup");
|
|
687
702
|
}
|
|
688
703
|
}
|
|
704
|
+
if (outputDirs.length > 1) {
|
|
705
|
+
const dirs = outputDirs.slice(1);
|
|
706
|
+
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
707
|
+
const relativePath = (0, import_path4.relative)(outputDir, wroteFile);
|
|
708
|
+
const content = await import_fs_extra.default.readFile(wroteFile, "utf-8");
|
|
709
|
+
await Promise.all(dirs.map(async (dir) => {
|
|
710
|
+
const filePath = (0, import_path4.resolve)(dir, relativePath);
|
|
711
|
+
await import_fs_extra.default.mkdir((0, import_path4.dirname)(filePath), { recursive: true });
|
|
712
|
+
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
713
|
+
}));
|
|
714
|
+
});
|
|
715
|
+
}
|
|
689
716
|
if (typeof afterBuild === "function") {
|
|
690
717
|
const result = afterBuild();
|
|
691
718
|
isPromise(result) && await result;
|
package/dist/index.mjs
CHANGED
|
@@ -278,20 +278,21 @@ function compileVueCode(code) {
|
|
|
278
278
|
id: `${index++}`
|
|
279
279
|
});
|
|
280
280
|
const classMatch = compiled.content.match(exportDefaultClassRE);
|
|
281
|
+
const plugins = scriptSetup.lang === "ts" ? ["typescript"] : void 0;
|
|
281
282
|
if (classMatch) {
|
|
282
283
|
content = compiled.content.replace(exportDefaultClassRE, "\nclass $1") + `
|
|
283
284
|
const _sfc_main = ${classMatch[1]}`;
|
|
284
285
|
if (exportDefaultRE.test(content)) {
|
|
285
|
-
content = rewriteDefault(compiled.content, "_sfc_main");
|
|
286
|
+
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
286
287
|
}
|
|
287
288
|
} else {
|
|
288
|
-
content = rewriteDefault(compiled.content, "_sfc_main");
|
|
289
|
+
content = rewriteDefault(compiled.content, "_sfc_main", plugins);
|
|
289
290
|
}
|
|
290
291
|
content = transferSetupPosition(content);
|
|
291
292
|
content += "\nexport default _sfc_main\n";
|
|
292
293
|
ext = scriptSetup.lang || "js";
|
|
293
294
|
} else if (script && script.content) {
|
|
294
|
-
content = rewriteDefault(script.content, "_sfc_main");
|
|
295
|
+
content = rewriteDefault(script.content, "_sfc_main", script.lang === "ts" ? ["typescript"] : void 0);
|
|
295
296
|
content += "\nexport default _sfc_main\n";
|
|
296
297
|
ext = script.lang || "js";
|
|
297
298
|
}
|
|
@@ -426,7 +427,7 @@ function dtsPlugin(options = {}) {
|
|
|
426
427
|
let logger;
|
|
427
428
|
let project;
|
|
428
429
|
let tsConfigPath;
|
|
429
|
-
let
|
|
430
|
+
let outputDirs;
|
|
430
431
|
let isBundle = false;
|
|
431
432
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
432
433
|
let hasJsVue = false;
|
|
@@ -450,7 +451,7 @@ function dtsPlugin(options = {}) {
|
|
|
450
451
|
if (aliasesExclude.length > 0) {
|
|
451
452
|
aliases = aliases.filter(({ find }) => !aliasesExclude.some((alias) => {
|
|
452
453
|
var _a3;
|
|
453
|
-
return
|
|
454
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a3 = find.match(alias)) == null ? void 0 : _a3[0] : find === alias);
|
|
454
455
|
}));
|
|
455
456
|
}
|
|
456
457
|
},
|
|
@@ -475,8 +476,8 @@ ${chalk.cyan("[vite:dts]")} You building not a library that may not need to gene
|
|
|
475
476
|
}
|
|
476
477
|
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
477
478
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
478
|
-
|
|
479
|
-
if (!
|
|
479
|
+
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
480
|
+
if (!outputDirs) {
|
|
480
481
|
logger.error(chalk.red(`
|
|
481
482
|
${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
482
483
|
`));
|
|
@@ -531,7 +532,7 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
531
532
|
},
|
|
532
533
|
async closeBundle() {
|
|
533
534
|
var _a2, _b2, _c, _d, _e, _f;
|
|
534
|
-
if (!
|
|
535
|
+
if (!outputDirs || !project || isBundle)
|
|
535
536
|
return;
|
|
536
537
|
logger.info(chalk.green(`
|
|
537
538
|
${logPrefix} Start generate declaration files...`));
|
|
@@ -588,7 +589,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
588
589
|
}));
|
|
589
590
|
const service = project.getLanguageService();
|
|
590
591
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
591
|
-
path: outputFile.
|
|
592
|
+
path: normalizePath2(resolve3(root, outputFile.compilerObject.name)),
|
|
592
593
|
content: outputFile.getText()
|
|
593
594
|
}))).flat().concat(dtsOutputFiles);
|
|
594
595
|
bundleDebug("emit");
|
|
@@ -597,6 +598,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
597
598
|
}
|
|
598
599
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
599
600
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
601
|
+
const outputDir = outputDirs[0];
|
|
600
602
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
601
603
|
var _a3, _b3;
|
|
602
604
|
let filePath = outputFile.path;
|
|
@@ -619,7 +621,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
619
621
|
}
|
|
620
622
|
}
|
|
621
623
|
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
622
|
-
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "
|
|
624
|
+
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf-8");
|
|
623
625
|
wroteFiles.add(normalizePath2(filePath));
|
|
624
626
|
});
|
|
625
627
|
bundleDebug("output");
|
|
@@ -630,13 +632,20 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
630
632
|
let typesPath = types ? resolve3(root, types) : resolve3(outputDir, indexName);
|
|
631
633
|
if (!fs.existsSync(typesPath)) {
|
|
632
634
|
const entry = entries[0];
|
|
633
|
-
|
|
634
|
-
filePath =
|
|
635
|
+
const outputIndex = resolve3(outputDir, relative2(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
636
|
+
let filePath = normalizePath2(relative2(dirname3(typesPath), outputIndex));
|
|
637
|
+
filePath = filePath.replace(dtsRE2, "");
|
|
635
638
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
636
|
-
let content = `
|
|
639
|
+
let content = `export * from '${filePath}'
|
|
640
|
+
`;
|
|
641
|
+
if (fs.existsSync(outputIndex)) {
|
|
642
|
+
const entryCodes = await fs.readFile(outputIndex, "utf-8");
|
|
643
|
+
if (entryCodes.includes("export default")) {
|
|
644
|
+
content += `import ${libName} from '${filePath}'
|
|
637
645
|
export default ${libName}
|
|
638
|
-
export * from '${filePath}'
|
|
639
646
|
`;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
640
649
|
if (typeof beforeWriteFile === "function") {
|
|
641
650
|
const result = beforeWriteFile(typesPath, content);
|
|
642
651
|
if (result && isNativeObj(result)) {
|
|
@@ -645,6 +654,7 @@ export * from '${filePath}'
|
|
|
645
654
|
}
|
|
646
655
|
}
|
|
647
656
|
await fs.writeFile(typesPath, content, "utf-8");
|
|
657
|
+
wroteFiles.add(normalizePath2(typesPath));
|
|
648
658
|
}
|
|
649
659
|
bundleDebug("insert index");
|
|
650
660
|
if (rollupTypes) {
|
|
@@ -657,17 +667,34 @@ export * from '${filePath}'
|
|
|
657
667
|
entryPath: typesPath,
|
|
658
668
|
fileName: basename(typesPath)
|
|
659
669
|
});
|
|
660
|
-
|
|
670
|
+
const wroteFile = normalizePath2(typesPath);
|
|
671
|
+
wroteFiles.delete(wroteFile);
|
|
661
672
|
await runParallel(os.cpus().length, Array.from(wroteFiles), (f) => fs.unlink(f));
|
|
662
673
|
removeDirIfEmpty(outputDir);
|
|
674
|
+
wroteFiles.clear();
|
|
675
|
+
wroteFiles.add(wroteFile);
|
|
663
676
|
if (copyDtsFiles) {
|
|
664
677
|
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
665
|
-
|
|
678
|
+
const filePath = resolve3(outputDir, basename(path));
|
|
679
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
680
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
666
681
|
});
|
|
667
682
|
}
|
|
668
683
|
bundleDebug("rollup");
|
|
669
684
|
}
|
|
670
685
|
}
|
|
686
|
+
if (outputDirs.length > 1) {
|
|
687
|
+
const dirs = outputDirs.slice(1);
|
|
688
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
689
|
+
const relativePath = relative2(outputDir, wroteFile);
|
|
690
|
+
const content = await fs.readFile(wroteFile, "utf-8");
|
|
691
|
+
await Promise.all(dirs.map(async (dir) => {
|
|
692
|
+
const filePath = resolve3(dir, relativePath);
|
|
693
|
+
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
694
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
695
|
+
}));
|
|
696
|
+
});
|
|
697
|
+
}
|
|
671
698
|
if (typeof afterBuild === "function") {
|
|
672
699
|
const result = afterBuild();
|
|
673
700
|
isPromise(result) && await result;
|
package/package.json
CHANGED