vite-plugin-dts 1.2.1 → 1.4.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 +18 -2
- package/README.zh-CN.md +207 -191
- package/dist/index.d.ts +1 -1
- package/dist/index.js +77 -13
- package/dist/index.mjs +77 -13
- 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
|
@@ -256,9 +256,11 @@ function transferSetupPosition(content) {
|
|
|
256
256
|
// src/compile.ts
|
|
257
257
|
var exportDefaultRE = /export\s+default/;
|
|
258
258
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
259
|
+
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
259
260
|
var index = 1;
|
|
260
261
|
var compileRoot = null;
|
|
261
262
|
var compiler;
|
|
263
|
+
var vue;
|
|
262
264
|
function requireCompiler() {
|
|
263
265
|
if (!compiler) {
|
|
264
266
|
if (compileRoot) {
|
|
@@ -281,15 +283,43 @@ function requireCompiler() {
|
|
|
281
283
|
}
|
|
282
284
|
return compiler;
|
|
283
285
|
}
|
|
286
|
+
function isVue3() {
|
|
287
|
+
if (!vue) {
|
|
288
|
+
if (compileRoot) {
|
|
289
|
+
try {
|
|
290
|
+
vue = require(require.resolve("vue", { paths: [compileRoot] }));
|
|
291
|
+
} catch (e) {
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (!vue) {
|
|
295
|
+
try {
|
|
296
|
+
vue = require("vue");
|
|
297
|
+
} catch (e) {
|
|
298
|
+
throw new Error("vue is not present in the dependency tree.\n");
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return vue.version.startsWith("3");
|
|
303
|
+
}
|
|
284
304
|
function setCompileRoot(root) {
|
|
285
305
|
if (root && root !== compileRoot) {
|
|
286
306
|
compileRoot = root;
|
|
287
307
|
compiler = null;
|
|
288
308
|
}
|
|
289
309
|
}
|
|
310
|
+
function parseCode(code) {
|
|
311
|
+
const { parse } = requireCompiler();
|
|
312
|
+
let descriptor;
|
|
313
|
+
if (isVue3()) {
|
|
314
|
+
descriptor = parse(code).descriptor;
|
|
315
|
+
} else {
|
|
316
|
+
descriptor = parse({ source: code });
|
|
317
|
+
}
|
|
318
|
+
return descriptor;
|
|
319
|
+
}
|
|
290
320
|
function compileVueCode(code) {
|
|
291
|
-
const {
|
|
292
|
-
const
|
|
321
|
+
const { compileScript, rewriteDefault } = requireCompiler();
|
|
322
|
+
const descriptor = parseCode(code);
|
|
293
323
|
const { script, scriptSetup } = descriptor;
|
|
294
324
|
let content = null;
|
|
295
325
|
let ext = null;
|
|
@@ -317,6 +347,9 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
317
347
|
content += "\nexport default _sfc_main\n";
|
|
318
348
|
ext = script.lang || "js";
|
|
319
349
|
}
|
|
350
|
+
} else {
|
|
351
|
+
content = noScriptContent;
|
|
352
|
+
ext = "ts";
|
|
320
353
|
}
|
|
321
354
|
return { content, ext };
|
|
322
355
|
}
|
|
@@ -438,13 +471,14 @@ function dtsPlugin(options = {}) {
|
|
|
438
471
|
const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
|
|
439
472
|
let root;
|
|
440
473
|
let entryRoot = (_b = options.entryRoot) != null ? _b : "";
|
|
474
|
+
let libName;
|
|
441
475
|
let indexName;
|
|
442
476
|
let aliases;
|
|
443
477
|
let entries;
|
|
444
478
|
let logger;
|
|
445
479
|
let project;
|
|
446
480
|
let tsConfigPath;
|
|
447
|
-
let
|
|
481
|
+
let outputDirs;
|
|
448
482
|
let isBundle = false;
|
|
449
483
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
450
484
|
let hasJsVue = false;
|
|
@@ -468,7 +502,7 @@ function dtsPlugin(options = {}) {
|
|
|
468
502
|
if (aliasesExclude.length > 0) {
|
|
469
503
|
aliases = aliases.filter(({ find }) => !aliasesExclude.some((alias) => {
|
|
470
504
|
var _a3;
|
|
471
|
-
return
|
|
505
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a3 = find.match(alias)) == null ? void 0 : _a3[0] : find === alias);
|
|
472
506
|
}));
|
|
473
507
|
}
|
|
474
508
|
},
|
|
@@ -481,9 +515,11 @@ function dtsPlugin(options = {}) {
|
|
|
481
515
|
logger.warn(import_chalk.default.yellow(`
|
|
482
516
|
${import_chalk.default.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
|
|
483
517
|
`));
|
|
518
|
+
libName = "_default";
|
|
484
519
|
indexName = defaultIndex;
|
|
485
520
|
} else {
|
|
486
521
|
const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
|
|
522
|
+
libName = config.build.lib.name || "_default";
|
|
487
523
|
indexName = typeof filename === "string" ? filename : filename("es");
|
|
488
524
|
if (!dtsRE2.test(indexName)) {
|
|
489
525
|
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
@@ -491,8 +527,8 @@ ${import_chalk.default.cyan("[vite:dts]")} You building not a library that may n
|
|
|
491
527
|
}
|
|
492
528
|
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
493
529
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
494
|
-
|
|
495
|
-
if (!
|
|
530
|
+
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
531
|
+
if (!outputDirs) {
|
|
496
532
|
logger.error(import_chalk.default.red(`
|
|
497
533
|
${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
498
534
|
`));
|
|
@@ -547,7 +583,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
547
583
|
},
|
|
548
584
|
async closeBundle() {
|
|
549
585
|
var _a2, _b2, _c, _d, _e, _f;
|
|
550
|
-
if (!
|
|
586
|
+
if (!outputDirs || !project || isBundle)
|
|
551
587
|
return;
|
|
552
588
|
logger.info(import_chalk.default.green(`
|
|
553
589
|
${logPrefix} Start generate declaration files...`));
|
|
@@ -604,7 +640,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
604
640
|
}));
|
|
605
641
|
const service = project.getLanguageService();
|
|
606
642
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
607
|
-
path:
|
|
643
|
+
path: (0, import_vite2.normalizePath)((0, import_path4.resolve)(root, outputFile.compilerObject.name)),
|
|
608
644
|
content: outputFile.getText()
|
|
609
645
|
}))).flat().concat(dtsOutputFiles);
|
|
610
646
|
bundleDebug("emit");
|
|
@@ -613,6 +649,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
613
649
|
}
|
|
614
650
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
615
651
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
652
|
+
const outputDir = outputDirs[0];
|
|
616
653
|
await runParallel(import_os.default.cpus().length, outputFiles, async (outputFile) => {
|
|
617
654
|
var _a3, _b3;
|
|
618
655
|
let filePath = outputFile.path;
|
|
@@ -635,7 +672,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
635
672
|
}
|
|
636
673
|
}
|
|
637
674
|
await import_fs_extra.default.mkdir((0, import_path4.dirname)(filePath), { recursive: true });
|
|
638
|
-
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "
|
|
675
|
+
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf-8");
|
|
639
676
|
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
640
677
|
});
|
|
641
678
|
bundleDebug("output");
|
|
@@ -646,11 +683,20 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
646
683
|
let typesPath = types ? (0, import_path4.resolve)(root, types) : (0, import_path4.resolve)(outputDir, indexName);
|
|
647
684
|
if (!import_fs_extra.default.existsSync(typesPath)) {
|
|
648
685
|
const entry = entries[0];
|
|
649
|
-
|
|
650
|
-
filePath =
|
|
686
|
+
const outputIndex = (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
687
|
+
let filePath = (0, import_vite2.normalizePath)((0, import_path4.relative)((0, import_path4.dirname)(typesPath), outputIndex));
|
|
688
|
+
filePath = filePath.replace(dtsRE2, "");
|
|
651
689
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
652
690
|
let content = `export * from '${filePath}'
|
|
653
691
|
`;
|
|
692
|
+
if (import_fs_extra.default.existsSync(outputIndex)) {
|
|
693
|
+
const entryCodes = await import_fs_extra.default.readFile(outputIndex, "utf-8");
|
|
694
|
+
if (entryCodes.includes("export default")) {
|
|
695
|
+
content += `import ${libName} from '${filePath}'
|
|
696
|
+
export default ${libName}
|
|
697
|
+
`;
|
|
698
|
+
}
|
|
699
|
+
}
|
|
654
700
|
if (typeof beforeWriteFile === "function") {
|
|
655
701
|
const result = beforeWriteFile(typesPath, content);
|
|
656
702
|
if (result && isNativeObj(result)) {
|
|
@@ -659,6 +705,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
659
705
|
}
|
|
660
706
|
}
|
|
661
707
|
await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
|
|
708
|
+
wroteFiles.add((0, import_vite2.normalizePath)(typesPath));
|
|
662
709
|
}
|
|
663
710
|
bundleDebug("insert index");
|
|
664
711
|
if (rollupTypes) {
|
|
@@ -671,17 +718,34 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
671
718
|
entryPath: typesPath,
|
|
672
719
|
fileName: (0, import_path4.basename)(typesPath)
|
|
673
720
|
});
|
|
674
|
-
|
|
721
|
+
const wroteFile = (0, import_vite2.normalizePath)(typesPath);
|
|
722
|
+
wroteFiles.delete(wroteFile);
|
|
675
723
|
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), (f) => import_fs_extra.default.unlink(f));
|
|
676
724
|
removeDirIfEmpty(outputDir);
|
|
725
|
+
wroteFiles.clear();
|
|
726
|
+
wroteFiles.add(wroteFile);
|
|
677
727
|
if (copyDtsFiles) {
|
|
678
728
|
await runParallel(import_os.default.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
679
|
-
|
|
729
|
+
const filePath = (0, import_path4.resolve)(outputDir, (0, import_path4.basename)(path));
|
|
730
|
+
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
731
|
+
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
680
732
|
});
|
|
681
733
|
}
|
|
682
734
|
bundleDebug("rollup");
|
|
683
735
|
}
|
|
684
736
|
}
|
|
737
|
+
if (outputDirs.length > 1) {
|
|
738
|
+
const dirs = outputDirs.slice(1);
|
|
739
|
+
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
740
|
+
const relativePath = (0, import_path4.relative)(outputDir, wroteFile);
|
|
741
|
+
const content = await import_fs_extra.default.readFile(wroteFile, "utf-8");
|
|
742
|
+
await Promise.all(dirs.map(async (dir) => {
|
|
743
|
+
const filePath = (0, import_path4.resolve)(dir, relativePath);
|
|
744
|
+
await import_fs_extra.default.mkdir((0, import_path4.dirname)(filePath), { recursive: true });
|
|
745
|
+
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
746
|
+
}));
|
|
747
|
+
});
|
|
748
|
+
}
|
|
685
749
|
if (typeof afterBuild === "function") {
|
|
686
750
|
const result = afterBuild();
|
|
687
751
|
isPromise(result) && await result;
|
package/dist/index.mjs
CHANGED
|
@@ -235,9 +235,11 @@ function transferSetupPosition(content) {
|
|
|
235
235
|
// src/compile.ts
|
|
236
236
|
var exportDefaultRE = /export\s+default/;
|
|
237
237
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
238
|
+
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
238
239
|
var index = 1;
|
|
239
240
|
var compileRoot = null;
|
|
240
241
|
var compiler;
|
|
242
|
+
var vue;
|
|
241
243
|
function requireCompiler() {
|
|
242
244
|
if (!compiler) {
|
|
243
245
|
if (compileRoot) {
|
|
@@ -260,15 +262,43 @@ function requireCompiler() {
|
|
|
260
262
|
}
|
|
261
263
|
return compiler;
|
|
262
264
|
}
|
|
265
|
+
function isVue3() {
|
|
266
|
+
if (!vue) {
|
|
267
|
+
if (compileRoot) {
|
|
268
|
+
try {
|
|
269
|
+
vue = __require(__require.resolve("vue", { paths: [compileRoot] }));
|
|
270
|
+
} catch (e) {
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!vue) {
|
|
274
|
+
try {
|
|
275
|
+
vue = __require("vue");
|
|
276
|
+
} catch (e) {
|
|
277
|
+
throw new Error("vue is not present in the dependency tree.\n");
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return vue.version.startsWith("3");
|
|
282
|
+
}
|
|
263
283
|
function setCompileRoot(root) {
|
|
264
284
|
if (root && root !== compileRoot) {
|
|
265
285
|
compileRoot = root;
|
|
266
286
|
compiler = null;
|
|
267
287
|
}
|
|
268
288
|
}
|
|
289
|
+
function parseCode(code) {
|
|
290
|
+
const { parse } = requireCompiler();
|
|
291
|
+
let descriptor;
|
|
292
|
+
if (isVue3()) {
|
|
293
|
+
descriptor = parse(code).descriptor;
|
|
294
|
+
} else {
|
|
295
|
+
descriptor = parse({ source: code });
|
|
296
|
+
}
|
|
297
|
+
return descriptor;
|
|
298
|
+
}
|
|
269
299
|
function compileVueCode(code) {
|
|
270
|
-
const {
|
|
271
|
-
const
|
|
300
|
+
const { compileScript, rewriteDefault } = requireCompiler();
|
|
301
|
+
const descriptor = parseCode(code);
|
|
272
302
|
const { script, scriptSetup } = descriptor;
|
|
273
303
|
let content = null;
|
|
274
304
|
let ext = null;
|
|
@@ -296,6 +326,9 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
296
326
|
content += "\nexport default _sfc_main\n";
|
|
297
327
|
ext = script.lang || "js";
|
|
298
328
|
}
|
|
329
|
+
} else {
|
|
330
|
+
content = noScriptContent;
|
|
331
|
+
ext = "ts";
|
|
299
332
|
}
|
|
300
333
|
return { content, ext };
|
|
301
334
|
}
|
|
@@ -420,13 +453,14 @@ function dtsPlugin(options = {}) {
|
|
|
420
453
|
const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
|
|
421
454
|
let root;
|
|
422
455
|
let entryRoot = (_b = options.entryRoot) != null ? _b : "";
|
|
456
|
+
let libName;
|
|
423
457
|
let indexName;
|
|
424
458
|
let aliases;
|
|
425
459
|
let entries;
|
|
426
460
|
let logger;
|
|
427
461
|
let project;
|
|
428
462
|
let tsConfigPath;
|
|
429
|
-
let
|
|
463
|
+
let outputDirs;
|
|
430
464
|
let isBundle = false;
|
|
431
465
|
const sourceDtsFiles = /* @__PURE__ */ new Set();
|
|
432
466
|
let hasJsVue = false;
|
|
@@ -450,7 +484,7 @@ function dtsPlugin(options = {}) {
|
|
|
450
484
|
if (aliasesExclude.length > 0) {
|
|
451
485
|
aliases = aliases.filter(({ find }) => !aliasesExclude.some((alias) => {
|
|
452
486
|
var _a3;
|
|
453
|
-
return
|
|
487
|
+
return alias && (isRegExp(find) ? find.toString() === alias.toString() : isRegExp(alias) ? (_a3 = find.match(alias)) == null ? void 0 : _a3[0] : find === alias);
|
|
454
488
|
}));
|
|
455
489
|
}
|
|
456
490
|
},
|
|
@@ -463,9 +497,11 @@ function dtsPlugin(options = {}) {
|
|
|
463
497
|
logger.warn(chalk.yellow(`
|
|
464
498
|
${chalk.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
|
|
465
499
|
`));
|
|
500
|
+
libName = "_default";
|
|
466
501
|
indexName = defaultIndex;
|
|
467
502
|
} else {
|
|
468
503
|
const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
|
|
504
|
+
libName = config.build.lib.name || "_default";
|
|
469
505
|
indexName = typeof filename === "string" ? filename : filename("es");
|
|
470
506
|
if (!dtsRE2.test(indexName)) {
|
|
471
507
|
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
@@ -473,8 +509,8 @@ ${chalk.cyan("[vite:dts]")} You building not a library that may not need to gene
|
|
|
473
509
|
}
|
|
474
510
|
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
475
511
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
476
|
-
|
|
477
|
-
if (!
|
|
512
|
+
outputDirs = options.outputDir ? ensureArray(options.outputDir).map((d) => ensureAbsolute(d, root)) : [ensureAbsolute(config.build.outDir, root)];
|
|
513
|
+
if (!outputDirs) {
|
|
478
514
|
logger.error(chalk.red(`
|
|
479
515
|
${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check your vite config and plugin options.
|
|
480
516
|
`));
|
|
@@ -529,7 +565,7 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
529
565
|
},
|
|
530
566
|
async closeBundle() {
|
|
531
567
|
var _a2, _b2, _c, _d, _e, _f;
|
|
532
|
-
if (!
|
|
568
|
+
if (!outputDirs || !project || isBundle)
|
|
533
569
|
return;
|
|
534
570
|
logger.info(chalk.green(`
|
|
535
571
|
${logPrefix} Start generate declaration files...`));
|
|
@@ -586,7 +622,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
586
622
|
}));
|
|
587
623
|
const service = project.getLanguageService();
|
|
588
624
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
589
|
-
path: outputFile.
|
|
625
|
+
path: normalizePath2(resolve3(root, outputFile.compilerObject.name)),
|
|
590
626
|
content: outputFile.getText()
|
|
591
627
|
}))).flat().concat(dtsOutputFiles);
|
|
592
628
|
bundleDebug("emit");
|
|
@@ -595,6 +631,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
595
631
|
}
|
|
596
632
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
597
633
|
const wroteFiles = /* @__PURE__ */ new Set();
|
|
634
|
+
const outputDir = outputDirs[0];
|
|
598
635
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
599
636
|
var _a3, _b3;
|
|
600
637
|
let filePath = outputFile.path;
|
|
@@ -617,7 +654,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
617
654
|
}
|
|
618
655
|
}
|
|
619
656
|
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
620
|
-
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "
|
|
657
|
+
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf-8");
|
|
621
658
|
wroteFiles.add(normalizePath2(filePath));
|
|
622
659
|
});
|
|
623
660
|
bundleDebug("output");
|
|
@@ -628,11 +665,20 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
628
665
|
let typesPath = types ? resolve3(root, types) : resolve3(outputDir, indexName);
|
|
629
666
|
if (!fs.existsSync(typesPath)) {
|
|
630
667
|
const entry = entries[0];
|
|
631
|
-
|
|
632
|
-
filePath =
|
|
668
|
+
const outputIndex = resolve3(outputDir, relative2(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
669
|
+
let filePath = normalizePath2(relative2(dirname3(typesPath), outputIndex));
|
|
670
|
+
filePath = filePath.replace(dtsRE2, "");
|
|
633
671
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
634
672
|
let content = `export * from '${filePath}'
|
|
635
673
|
`;
|
|
674
|
+
if (fs.existsSync(outputIndex)) {
|
|
675
|
+
const entryCodes = await fs.readFile(outputIndex, "utf-8");
|
|
676
|
+
if (entryCodes.includes("export default")) {
|
|
677
|
+
content += `import ${libName} from '${filePath}'
|
|
678
|
+
export default ${libName}
|
|
679
|
+
`;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
636
682
|
if (typeof beforeWriteFile === "function") {
|
|
637
683
|
const result = beforeWriteFile(typesPath, content);
|
|
638
684
|
if (result && isNativeObj(result)) {
|
|
@@ -641,6 +687,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
641
687
|
}
|
|
642
688
|
}
|
|
643
689
|
await fs.writeFile(typesPath, content, "utf-8");
|
|
690
|
+
wroteFiles.add(normalizePath2(typesPath));
|
|
644
691
|
}
|
|
645
692
|
bundleDebug("insert index");
|
|
646
693
|
if (rollupTypes) {
|
|
@@ -653,17 +700,34 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
653
700
|
entryPath: typesPath,
|
|
654
701
|
fileName: basename(typesPath)
|
|
655
702
|
});
|
|
656
|
-
|
|
703
|
+
const wroteFile = normalizePath2(typesPath);
|
|
704
|
+
wroteFiles.delete(wroteFile);
|
|
657
705
|
await runParallel(os.cpus().length, Array.from(wroteFiles), (f) => fs.unlink(f));
|
|
658
706
|
removeDirIfEmpty(outputDir);
|
|
707
|
+
wroteFiles.clear();
|
|
708
|
+
wroteFiles.add(wroteFile);
|
|
659
709
|
if (copyDtsFiles) {
|
|
660
710
|
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
661
|
-
|
|
711
|
+
const filePath = resolve3(outputDir, basename(path));
|
|
712
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
713
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
662
714
|
});
|
|
663
715
|
}
|
|
664
716
|
bundleDebug("rollup");
|
|
665
717
|
}
|
|
666
718
|
}
|
|
719
|
+
if (outputDirs.length > 1) {
|
|
720
|
+
const dirs = outputDirs.slice(1);
|
|
721
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
722
|
+
const relativePath = relative2(outputDir, wroteFile);
|
|
723
|
+
const content = await fs.readFile(wroteFile, "utf-8");
|
|
724
|
+
await Promise.all(dirs.map(async (dir) => {
|
|
725
|
+
const filePath = resolve3(dir, relativePath);
|
|
726
|
+
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
727
|
+
await fs.writeFile(filePath, content, "utf-8");
|
|
728
|
+
}));
|
|
729
|
+
});
|
|
730
|
+
}
|
|
667
731
|
if (typeof afterBuild === "function") {
|
|
668
732
|
const result = afterBuild();
|
|
669
733
|
isPromise(result) && await result;
|
package/package.json
CHANGED