vite-plugin-dts 1.6.0 → 1.6.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 +214 -213
- package/README.zh-CN.md +213 -212
- package/dist/index.js +36 -39
- package/dist/index.mjs +18 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,213 +1,214 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**English** | [中文](./README.zh-CN.md)
|
|
4
|
-
|
|
5
|
-
A vite plugin that generates declaration files (`*.d.ts`) from `.ts(x)` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
pnpm add vite-plugin-dts -D
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
In `vite.config.ts`:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import { resolve } from 'path'
|
|
19
|
-
import { defineConfig } from 'vite'
|
|
20
|
-
import dts from 'vite-plugin-dts'
|
|
21
|
-
|
|
22
|
-
export default defineConfig({
|
|
23
|
-
build: {
|
|
24
|
-
lib: {
|
|
25
|
-
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
-
name: 'MyLib',
|
|
27
|
-
formats: ['es'],
|
|
28
|
-
fileName: 'my-lib'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
plugins: [dts()]
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
In your component:
|
|
36
|
-
|
|
37
|
-
```vue
|
|
38
|
-
<template>
|
|
39
|
-
<div></div>
|
|
40
|
-
</template>
|
|
41
|
-
|
|
42
|
-
<script lang="ts">
|
|
43
|
-
// using defineComponent for inferring types
|
|
44
|
-
import { defineComponent } from 'vue'
|
|
45
|
-
|
|
46
|
-
export default defineComponent({
|
|
47
|
-
name: 'Component'
|
|
48
|
-
})
|
|
49
|
-
</script>
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
```vue
|
|
53
|
-
<script setup lang="ts">
|
|
54
|
-
// Need to access the defineProps returned value to
|
|
55
|
-
// infer types although you never use the props directly
|
|
56
|
-
const props = defineProps<{
|
|
57
|
-
color: 'blue' | 'red'
|
|
58
|
-
}>()
|
|
59
|
-
</script>
|
|
60
|
-
|
|
61
|
-
<template>
|
|
62
|
-
<div>{{ color }}</div>
|
|
63
|
-
</template>
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Options
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
-
|
|
71
|
-
interface TransformWriteFile {
|
|
72
|
-
filePath?: string
|
|
73
|
-
content?: string
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface PluginOptions {
|
|
77
|
-
// Depends on the root directory
|
|
78
|
-
// Defaults base on your vite config root options
|
|
79
|
-
root?: string
|
|
80
|
-
|
|
81
|
-
// Declaration files output directory
|
|
82
|
-
// Can be specified a array to output to multiple directories
|
|
83
|
-
// Defaults base on your vite config output options
|
|
84
|
-
outputDir?: string | string[]
|
|
85
|
-
|
|
86
|
-
// Manually set the root path of the entry files
|
|
87
|
-
// The output path of each file will be caculated base on it
|
|
88
|
-
// Defaults is the smallest public path for all files
|
|
89
|
-
entryRoot?: string
|
|
90
|
-
|
|
91
|
-
// Project init compilerOptions using by ts-morph
|
|
92
|
-
// Default: null
|
|
93
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
94
|
-
|
|
95
|
-
// Project init tsconfig.json file path by ts-morph
|
|
96
|
-
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
97
|
-
// Default: 'tsconfig.json'
|
|
98
|
-
tsConfigFilePath?: string
|
|
99
|
-
|
|
100
|
-
// Set which paths should exclude when transform aliases
|
|
101
|
-
// If it's regexp, it will test the original import path directly
|
|
102
|
-
// Default: []
|
|
103
|
-
aliasesExclude?: (string | RegExp)[]
|
|
104
|
-
|
|
105
|
-
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
106
|
-
// Default: false
|
|
107
|
-
cleanVueFileName?: boolean
|
|
108
|
-
|
|
109
|
-
// Whether transform dynamic import to static
|
|
110
|
-
// Force true when `rollupTypes` is effective
|
|
111
|
-
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
112
|
-
// Default: false
|
|
113
|
-
staticImport?: boolean
|
|
114
|
-
|
|
115
|
-
// Manual set include glob
|
|
116
|
-
// Defaults base on your tsconfig.json include option
|
|
117
|
-
include?: string | string[]
|
|
118
|
-
|
|
119
|
-
// Manual set exclude glob
|
|
120
|
-
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
121
|
-
exclude?: string | string[]
|
|
122
|
-
|
|
123
|
-
// Whether generate types entry file
|
|
124
|
-
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
125
|
-
// Force true when `rollupTypes` is effective
|
|
126
|
-
// Default: false
|
|
127
|
-
insertTypesEntry?: boolean
|
|
128
|
-
|
|
129
|
-
// Set to rollup declaration files after emit
|
|
130
|
-
// Power by `@microsoft/api-extractor`, it will start a new program which takes some time
|
|
131
|
-
// Default: false
|
|
132
|
-
rollupTypes?: boolean
|
|
133
|
-
|
|
134
|
-
// Whether copy .d.ts source files into outputDir
|
|
135
|
-
// Default: true
|
|
136
|
-
copyDtsFiles?: boolean
|
|
137
|
-
|
|
138
|
-
// Whether emit nothing when has any diagnostic
|
|
139
|
-
// Default: false
|
|
140
|
-
noEmitOnError?: boolean
|
|
141
|
-
|
|
142
|
-
// Whether skip typescript diagnostics
|
|
143
|
-
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
144
|
-
// But for the source files with type errors will not be emitted
|
|
145
|
-
// Default: true
|
|
146
|
-
skipDiagnostics?: boolean
|
|
147
|
-
|
|
148
|
-
// Whether log diagnostic informations
|
|
149
|
-
// Not effective when `skipDiagnostics` is true
|
|
150
|
-
// Default: false
|
|
151
|
-
logDiagnostics?: boolean
|
|
152
|
-
|
|
153
|
-
// After emit diagnostic hook
|
|
154
|
-
// According to the length to judge whether there is any type error
|
|
155
|
-
// Default: () => {}
|
|
156
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
157
|
-
|
|
158
|
-
// Before declaration file be writed hook
|
|
159
|
-
// You can transform declaration file-path and content through it
|
|
160
|
-
//
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
"
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**English** | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A vite plugin that generates declaration files (`*.d.ts`) from `.ts(x)` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add vite-plugin-dts -D
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
In `vite.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { resolve } from 'path'
|
|
19
|
+
import { defineConfig } from 'vite'
|
|
20
|
+
import dts from 'vite-plugin-dts'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
build: {
|
|
24
|
+
lib: {
|
|
25
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
+
name: 'MyLib',
|
|
27
|
+
formats: ['es'],
|
|
28
|
+
fileName: 'my-lib'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
plugins: [dts()]
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In your component:
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div></div>
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
<script lang="ts">
|
|
43
|
+
// using defineComponent for inferring types
|
|
44
|
+
import { defineComponent } from 'vue'
|
|
45
|
+
|
|
46
|
+
export default defineComponent({
|
|
47
|
+
name: 'Component'
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
// Need to access the defineProps returned value to
|
|
55
|
+
// infer types although you never use the props directly
|
|
56
|
+
const props = defineProps<{
|
|
57
|
+
color: 'blue' | 'red'
|
|
58
|
+
}>()
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<template>
|
|
62
|
+
<div>{{ color }}</div>
|
|
63
|
+
</template>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
+
|
|
71
|
+
interface TransformWriteFile {
|
|
72
|
+
filePath?: string
|
|
73
|
+
content?: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface PluginOptions {
|
|
77
|
+
// Depends on the root directory
|
|
78
|
+
// Defaults base on your vite config root options
|
|
79
|
+
root?: string
|
|
80
|
+
|
|
81
|
+
// Declaration files output directory
|
|
82
|
+
// Can be specified a array to output to multiple directories
|
|
83
|
+
// Defaults base on your vite config output options
|
|
84
|
+
outputDir?: string | string[]
|
|
85
|
+
|
|
86
|
+
// Manually set the root path of the entry files
|
|
87
|
+
// The output path of each file will be caculated base on it
|
|
88
|
+
// Defaults is the smallest public path for all files
|
|
89
|
+
entryRoot?: string
|
|
90
|
+
|
|
91
|
+
// Project init compilerOptions using by ts-morph
|
|
92
|
+
// Default: null
|
|
93
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
94
|
+
|
|
95
|
+
// Project init tsconfig.json file path by ts-morph
|
|
96
|
+
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
97
|
+
// Default: 'tsconfig.json'
|
|
98
|
+
tsConfigFilePath?: string
|
|
99
|
+
|
|
100
|
+
// Set which paths should exclude when transform aliases
|
|
101
|
+
// If it's regexp, it will test the original import path directly
|
|
102
|
+
// Default: []
|
|
103
|
+
aliasesExclude?: (string | RegExp)[]
|
|
104
|
+
|
|
105
|
+
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
106
|
+
// Default: false
|
|
107
|
+
cleanVueFileName?: boolean
|
|
108
|
+
|
|
109
|
+
// Whether transform dynamic import to static
|
|
110
|
+
// Force true when `rollupTypes` is effective
|
|
111
|
+
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
112
|
+
// Default: false
|
|
113
|
+
staticImport?: boolean
|
|
114
|
+
|
|
115
|
+
// Manual set include glob
|
|
116
|
+
// Defaults base on your tsconfig.json include option
|
|
117
|
+
include?: string | string[]
|
|
118
|
+
|
|
119
|
+
// Manual set exclude glob
|
|
120
|
+
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
121
|
+
exclude?: string | string[]
|
|
122
|
+
|
|
123
|
+
// Whether generate types entry file
|
|
124
|
+
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
125
|
+
// Force true when `rollupTypes` is effective
|
|
126
|
+
// Default: false
|
|
127
|
+
insertTypesEntry?: boolean
|
|
128
|
+
|
|
129
|
+
// Set to rollup declaration files after emit
|
|
130
|
+
// Power by `@microsoft/api-extractor`, it will start a new program which takes some time
|
|
131
|
+
// Default: false
|
|
132
|
+
rollupTypes?: boolean
|
|
133
|
+
|
|
134
|
+
// Whether copy .d.ts source files into outputDir
|
|
135
|
+
// Default: true
|
|
136
|
+
copyDtsFiles?: boolean
|
|
137
|
+
|
|
138
|
+
// Whether emit nothing when has any diagnostic
|
|
139
|
+
// Default: false
|
|
140
|
+
noEmitOnError?: boolean
|
|
141
|
+
|
|
142
|
+
// Whether skip typescript diagnostics
|
|
143
|
+
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
144
|
+
// But for the source files with type errors will not be emitted
|
|
145
|
+
// Default: true
|
|
146
|
+
skipDiagnostics?: boolean
|
|
147
|
+
|
|
148
|
+
// Whether log diagnostic informations
|
|
149
|
+
// Not effective when `skipDiagnostics` is true
|
|
150
|
+
// Default: false
|
|
151
|
+
logDiagnostics?: boolean
|
|
152
|
+
|
|
153
|
+
// After emit diagnostic hook
|
|
154
|
+
// According to the length to judge whether there is any type error
|
|
155
|
+
// Default: () => {}
|
|
156
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
157
|
+
|
|
158
|
+
// Before declaration file be writed hook
|
|
159
|
+
// You can transform declaration file-path and content through it
|
|
160
|
+
// The file will be skipped when return exact false
|
|
161
|
+
// Default: () => {}
|
|
162
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
163
|
+
|
|
164
|
+
// After build hook
|
|
165
|
+
// It wil be called after all declaration files are written
|
|
166
|
+
// Default: () => {}
|
|
167
|
+
afterBuild?: () => void | Promise<void>
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Example
|
|
172
|
+
|
|
173
|
+
Clone and run the following script:
|
|
174
|
+
|
|
175
|
+
```sh
|
|
176
|
+
pnpm run test:e2e
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Then check `example/types`.
|
|
180
|
+
|
|
181
|
+
## FAQ
|
|
182
|
+
|
|
183
|
+
Here are some FAQ's and solutions.
|
|
184
|
+
|
|
185
|
+
### Missing some declaration files after build
|
|
186
|
+
|
|
187
|
+
By default `skipDiagnostics` option is `true`, which means that type diagnostic will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). If there are some files with type errors which interrupt the build process, these files will not be emitted (declaration files won't be generated).
|
|
188
|
+
|
|
189
|
+
If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on the diagnostic and log features of this plugin. It will help you check the type errors during build and log error information to the terminal.
|
|
190
|
+
|
|
191
|
+
### Take type error when using both `script` and `setup-script` in vue component
|
|
192
|
+
|
|
193
|
+
This is usually caused by using `defineComponent` function in both `script` and `setup-script`. When `vue/compiler-sfc` compiles these files, the default export result from `script` gets merged with the parameter object of `defineComponent` from `setup-script`. This is incompatible with parameters and types returned from `defineComponent`, which results in a type error.
|
|
194
|
+
|
|
195
|
+
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.
|
|
196
|
+
|
|
197
|
+
### Take errors that unable to infer types from packages which under `node_modules`
|
|
198
|
+
|
|
199
|
+
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:
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"compilerOptions": {
|
|
204
|
+
"baseUrl": ".",
|
|
205
|
+
"paths": {
|
|
206
|
+
"third-lib": ["node_modules/third-lib"]
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## License
|
|
213
|
+
|
|
214
|
+
MIT License.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,212 +1,213 @@
|
|
|
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
|
-
// 设置在转换别名时哪些路径需要排除
|
|
100
|
-
// 如果为正则,会直接使用 test 和原始路径进行比较
|
|
101
|
-
// 默认值: []
|
|
102
|
-
aliasesExclude?: (string | RegExp)[]
|
|
103
|
-
|
|
104
|
-
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
105
|
-
// 默认值: false
|
|
106
|
-
cleanVueFileName?: boolean
|
|
107
|
-
|
|
108
|
-
//是否将动态引入转换为静态
|
|
109
|
-
// 当开启打包类型文件时强制为 true
|
|
110
|
-
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
111
|
-
// 默认值: false
|
|
112
|
-
staticImport?: boolean
|
|
113
|
-
|
|
114
|
-
// 手动设置包含路径的 glob
|
|
115
|
-
// 默认基于 tsconfig.json 的 include 选项
|
|
116
|
-
include?: string | string[]
|
|
117
|
-
|
|
118
|
-
// 手动设置排除路径的 glob
|
|
119
|
-
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
120
|
-
exclude?: string | string[]
|
|
121
|
-
|
|
122
|
-
// 是否生成类型声明入口
|
|
123
|
-
// 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
|
|
124
|
-
// 当开启打包类型文件时强制为 true
|
|
125
|
-
// 默认值: false
|
|
126
|
-
insertTypesEntry?: boolean
|
|
127
|
-
|
|
128
|
-
// 设置是否在发出类型文件后将其打包
|
|
129
|
-
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
130
|
-
// 默认值: false
|
|
131
|
-
rollupTypes?: boolean
|
|
132
|
-
|
|
133
|
-
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
134
|
-
// 默认值: true
|
|
135
|
-
copyDtsFiles?: boolean
|
|
136
|
-
|
|
137
|
-
// 出现类型诊断信息时不生成类型文件
|
|
138
|
-
// 默认值: false
|
|
139
|
-
noEmitOnError?: boolean
|
|
140
|
-
|
|
141
|
-
// 是否跳过类型诊断
|
|
142
|
-
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
143
|
-
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
144
|
-
// 默认值: true
|
|
145
|
-
skipDiagnostics?: boolean
|
|
146
|
-
|
|
147
|
-
// 是否打印类型诊断信息
|
|
148
|
-
// 当跳过类型诊断时该属性将不会生效
|
|
149
|
-
// 默认值: false
|
|
150
|
-
logDiagnostics?: boolean
|
|
151
|
-
|
|
152
|
-
// 获取诊断信息后的钩子
|
|
153
|
-
// 可以根据参数 length 来判断有误类型错误
|
|
154
|
-
// 默认值: () => {}
|
|
155
|
-
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
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
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
"
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
+
// 设置在转换别名时哪些路径需要排除
|
|
100
|
+
// 如果为正则,会直接使用 test 和原始路径进行比较
|
|
101
|
+
// 默认值: []
|
|
102
|
+
aliasesExclude?: (string | RegExp)[]
|
|
103
|
+
|
|
104
|
+
// 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
|
|
105
|
+
// 默认值: false
|
|
106
|
+
cleanVueFileName?: boolean
|
|
107
|
+
|
|
108
|
+
//是否将动态引入转换为静态
|
|
109
|
+
// 当开启打包类型文件时强制为 true
|
|
110
|
+
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
111
|
+
// 默认值: false
|
|
112
|
+
staticImport?: boolean
|
|
113
|
+
|
|
114
|
+
// 手动设置包含路径的 glob
|
|
115
|
+
// 默认基于 tsconfig.json 的 include 选项
|
|
116
|
+
include?: string | string[]
|
|
117
|
+
|
|
118
|
+
// 手动设置排除路径的 glob
|
|
119
|
+
// 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
|
|
120
|
+
exclude?: string | string[]
|
|
121
|
+
|
|
122
|
+
// 是否生成类型声明入口
|
|
123
|
+
// 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
|
|
124
|
+
// 当开启打包类型文件时强制为 true
|
|
125
|
+
// 默认值: false
|
|
126
|
+
insertTypesEntry?: boolean
|
|
127
|
+
|
|
128
|
+
// 设置是否在发出类型文件后将其打包
|
|
129
|
+
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
130
|
+
// 默认值: false
|
|
131
|
+
rollupTypes?: boolean
|
|
132
|
+
|
|
133
|
+
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
134
|
+
// 默认值: true
|
|
135
|
+
copyDtsFiles?: boolean
|
|
136
|
+
|
|
137
|
+
// 出现类型诊断信息时不生成类型文件
|
|
138
|
+
// 默认值: false
|
|
139
|
+
noEmitOnError?: boolean
|
|
140
|
+
|
|
141
|
+
// 是否跳过类型诊断
|
|
142
|
+
// 跳过类型诊断意味着出现错误时不会中断打包进程的执行
|
|
143
|
+
// 但对于出现错误的源文件,将无法生成相应的类型文件
|
|
144
|
+
// 默认值: true
|
|
145
|
+
skipDiagnostics?: boolean
|
|
146
|
+
|
|
147
|
+
// 是否打印类型诊断信息
|
|
148
|
+
// 当跳过类型诊断时该属性将不会生效
|
|
149
|
+
// 默认值: false
|
|
150
|
+
logDiagnostics?: boolean
|
|
151
|
+
|
|
152
|
+
// 获取诊断信息后的钩子
|
|
153
|
+
// 可以根据参数 length 来判断有误类型错误
|
|
154
|
+
// 默认值: () => {}
|
|
155
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
156
|
+
|
|
157
|
+
// 类型声明文件被写入前的钩子
|
|
158
|
+
// 可以在钩子里转换文件路径和文件内容
|
|
159
|
+
// 当返回 false 时会跳过该文件
|
|
160
|
+
// 默认值: () => {}
|
|
161
|
+
beforeWriteFile?: (filePath: string, content: string) => void | false | TransformWriteFile
|
|
162
|
+
|
|
163
|
+
// 构建后回调钩子
|
|
164
|
+
// 将会在所有类型文件被写入后调用
|
|
165
|
+
// 默认值: () => {}
|
|
166
|
+
afterBuild?: () => void | Promise<void>
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## 示例
|
|
171
|
+
|
|
172
|
+
克隆项目然后执行下列命令:
|
|
173
|
+
|
|
174
|
+
```sh
|
|
175
|
+
pnpm run test:e2e
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
然后检查 `example/types` 目录。
|
|
179
|
+
|
|
180
|
+
## 常见问题
|
|
181
|
+
|
|
182
|
+
此处将收录一些常见的问题并提供一些解决方案。
|
|
183
|
+
|
|
184
|
+
### 打包后出现类型文件缺失
|
|
185
|
+
|
|
186
|
+
默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
|
|
187
|
+
|
|
188
|
+
如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
|
|
189
|
+
|
|
190
|
+
### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
|
|
191
|
+
|
|
192
|
+
这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
|
|
193
|
+
|
|
194
|
+
这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
|
|
195
|
+
|
|
196
|
+
### 打包时出现了无法从 `node_modules` 的包中推断类型的错误
|
|
197
|
+
|
|
198
|
+
这是 TypeScript 通过软链接 (pnpm) 读取 `node_modules` 中过的类型时会出现的一个已知的问题,可以参考这个 [issue](https://github.com/microsoft/TypeScript/issues/42873),目前已有的一个解决方案,在你的 `tsconfig.json` 中添加 `baseUrl` 以及在 `paths` 添加这些包的路径:
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"compilerOptions": {
|
|
203
|
+
"baseUrl": ".",
|
|
204
|
+
"paths": {
|
|
205
|
+
"third-lib": ["node_modules/third-lib"]
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## 授权
|
|
212
|
+
|
|
213
|
+
MIT 授权。
|
package/dist/index.js
CHANGED
|
@@ -32,7 +32,7 @@ module.exports = dtsPlugin;
|
|
|
32
32
|
dtsPlugin['default'] = dtsPlugin;
|
|
33
33
|
|
|
34
34
|
// src/plugin.ts
|
|
35
|
-
var
|
|
35
|
+
var import_node_path4 = require("path");
|
|
36
36
|
var import_fs_extra = __toESM(require("fs-extra"));
|
|
37
37
|
var import_os = __toESM(require("os"));
|
|
38
38
|
var import_chalk = __toESM(require("chalk"));
|
|
@@ -43,12 +43,12 @@ var import_vite2 = require("vite");
|
|
|
43
43
|
var import_typescript = require("typescript");
|
|
44
44
|
|
|
45
45
|
// src/transform.ts
|
|
46
|
-
var
|
|
46
|
+
var import_node_path2 = require("path");
|
|
47
47
|
var import_vite = require("vite");
|
|
48
48
|
|
|
49
49
|
// src/utils.ts
|
|
50
|
-
var
|
|
51
|
-
var
|
|
50
|
+
var import_node_path = require("path");
|
|
51
|
+
var import_node_fs = require("fs");
|
|
52
52
|
function isNativeObj(value) {
|
|
53
53
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
54
54
|
}
|
|
@@ -92,7 +92,7 @@ function mergeObjects(sourceObj, targetObj) {
|
|
|
92
92
|
return sourceObj;
|
|
93
93
|
}
|
|
94
94
|
function ensureAbsolute(path, root) {
|
|
95
|
-
return path ? (0,
|
|
95
|
+
return path ? (0, import_node_path.isAbsolute)(path) ? path : (0, import_node_path.resolve)(root, path) : root;
|
|
96
96
|
}
|
|
97
97
|
function ensureArray(value) {
|
|
98
98
|
return Array.isArray(value) ? value : value ? [value] : [];
|
|
@@ -118,16 +118,16 @@ function queryPublicPath(paths) {
|
|
|
118
118
|
if (paths.length === 0) {
|
|
119
119
|
return "";
|
|
120
120
|
} else if (paths.length === 1) {
|
|
121
|
-
return (0,
|
|
121
|
+
return (0, import_node_path.dirname)(paths[0]);
|
|
122
122
|
}
|
|
123
|
-
let publicPath = (0,
|
|
123
|
+
let publicPath = (0, import_node_path.normalize)((0, import_node_path.dirname)(paths[0])) + import_node_path.sep;
|
|
124
124
|
let publicUnits = publicPath.split(speRE);
|
|
125
125
|
let index2 = publicUnits.length - 1;
|
|
126
126
|
for (const path of paths.slice(1)) {
|
|
127
127
|
if (!index2) {
|
|
128
128
|
return publicPath;
|
|
129
129
|
}
|
|
130
|
-
const dirPath = (0,
|
|
130
|
+
const dirPath = (0, import_node_path.normalize)((0, import_node_path.dirname)(path)) + import_node_path.sep;
|
|
131
131
|
if (dirPath.startsWith(publicPath)) {
|
|
132
132
|
continue;
|
|
133
133
|
}
|
|
@@ -144,7 +144,7 @@ function queryPublicPath(paths) {
|
|
|
144
144
|
}
|
|
145
145
|
index2 = i - 1;
|
|
146
146
|
publicUnits = publicUnits.slice(0, index2 + 1);
|
|
147
|
-
publicPath = publicUnits.join(
|
|
147
|
+
publicPath = publicUnits.join(import_node_path.sep) + import_node_path.sep;
|
|
148
148
|
break;
|
|
149
149
|
}
|
|
150
150
|
}
|
|
@@ -152,13 +152,13 @@ function queryPublicPath(paths) {
|
|
|
152
152
|
return publicPath.slice(0, -1);
|
|
153
153
|
}
|
|
154
154
|
function removeDirIfEmpty(dir) {
|
|
155
|
-
if (!(0,
|
|
155
|
+
if (!(0, import_node_fs.existsSync)(dir)) {
|
|
156
156
|
return;
|
|
157
157
|
}
|
|
158
158
|
let onlyHasDir = true;
|
|
159
|
-
for (const file of (0,
|
|
160
|
-
const abs = (0,
|
|
161
|
-
if ((0,
|
|
159
|
+
for (const file of (0, import_node_fs.readdirSync)(dir)) {
|
|
160
|
+
const abs = (0, import_node_path.resolve)(dir, file);
|
|
161
|
+
if ((0, import_node_fs.lstatSync)(abs).isDirectory()) {
|
|
162
162
|
if (!removeDirIfEmpty(abs)) {
|
|
163
163
|
onlyHasDir = false;
|
|
164
164
|
}
|
|
@@ -167,7 +167,7 @@ function removeDirIfEmpty(dir) {
|
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
if (onlyHasDir) {
|
|
170
|
-
(0,
|
|
170
|
+
(0, import_node_fs.rmdirSync)(dir);
|
|
171
171
|
}
|
|
172
172
|
return onlyHasDir;
|
|
173
173
|
}
|
|
@@ -244,7 +244,7 @@ function transformAliasImport(filePath, content, aliases, exclude = []) {
|
|
|
244
244
|
if (exclude.some((e) => isRegExp(e) ? e.test(matchResult[1]) : String(e) === matchResult[1])) {
|
|
245
245
|
return str;
|
|
246
246
|
}
|
|
247
|
-
const truthPath = (0,
|
|
247
|
+
const truthPath = (0, import_node_path2.isAbsolute)(matchedAlias.replacement) ? (0, import_vite.normalizePath)((0, import_node_path2.relative)((0, import_node_path2.dirname)(filePath), matchedAlias.replacement)) : (0, import_vite.normalizePath)(matchedAlias.replacement);
|
|
248
248
|
return str.replace(
|
|
249
249
|
isDynamic ? simpleDynamicImportRE : simpleStaticImportRE,
|
|
250
250
|
`$1'${matchResult[1].replace(
|
|
@@ -272,8 +272,6 @@ function transferSetupPosition(content) {
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
// src/compile.ts
|
|
275
|
-
var import_node_module = require("module");
|
|
276
|
-
var import_meta = {};
|
|
277
275
|
var exportDefaultRE = /export\s+default/;
|
|
278
276
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
279
277
|
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
@@ -281,21 +279,20 @@ var index = 1;
|
|
|
281
279
|
var compileRoot = null;
|
|
282
280
|
var compiler;
|
|
283
281
|
var vue;
|
|
284
|
-
var _require = (0, import_node_module.createRequire)(import_meta.url);
|
|
285
282
|
function requireCompiler() {
|
|
286
283
|
if (!compiler) {
|
|
287
284
|
if (compileRoot) {
|
|
288
285
|
try {
|
|
289
|
-
compiler =
|
|
286
|
+
compiler = require(require.resolve("vue/compiler-sfc", { paths: [compileRoot] }));
|
|
290
287
|
} catch (e) {
|
|
291
288
|
}
|
|
292
289
|
}
|
|
293
290
|
if (!compiler) {
|
|
294
291
|
try {
|
|
295
|
-
compiler =
|
|
292
|
+
compiler = require("vue/compiler-sfc");
|
|
296
293
|
} catch (e) {
|
|
297
294
|
try {
|
|
298
|
-
compiler =
|
|
295
|
+
compiler = require("@vue/compiler-sfc");
|
|
299
296
|
} catch (e2) {
|
|
300
297
|
throw new Error("@vue/compiler-sfc is not present in the dependency tree.\n");
|
|
301
298
|
}
|
|
@@ -308,13 +305,13 @@ function isVue3() {
|
|
|
308
305
|
if (!vue) {
|
|
309
306
|
if (compileRoot) {
|
|
310
307
|
try {
|
|
311
|
-
vue =
|
|
308
|
+
vue = require(require.resolve("vue", { paths: [compileRoot] }));
|
|
312
309
|
} catch (e) {
|
|
313
310
|
}
|
|
314
311
|
}
|
|
315
312
|
if (!vue) {
|
|
316
313
|
try {
|
|
317
|
-
vue =
|
|
314
|
+
vue = require("vue");
|
|
318
315
|
} catch (e) {
|
|
319
316
|
throw new Error("vue is not present in the dependency tree.\n");
|
|
320
317
|
}
|
|
@@ -384,7 +381,7 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
384
381
|
}
|
|
385
382
|
|
|
386
383
|
// src/rollup.ts
|
|
387
|
-
var
|
|
384
|
+
var import_node_path3 = require("path");
|
|
388
385
|
var import_api_extractor = require("@microsoft/api-extractor");
|
|
389
386
|
var import_Collector = require("@microsoft/api-extractor/lib/collector/Collector");
|
|
390
387
|
var import_MessageRouter = require("@microsoft/api-extractor/lib/collector/MessageRouter");
|
|
@@ -399,7 +396,7 @@ function rollupDeclarationFiles({
|
|
|
399
396
|
fileName,
|
|
400
397
|
compilerOptions
|
|
401
398
|
}) {
|
|
402
|
-
const configObjectFullPath = (0,
|
|
399
|
+
const configObjectFullPath = (0, import_node_path3.resolve)(root, "api-extractor.json");
|
|
403
400
|
const packageJsonLookup = new import_node_core_library.PackageJsonLookup();
|
|
404
401
|
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
405
402
|
if (!dtsRE.test(fileName)) {
|
|
@@ -422,7 +419,7 @@ function rollupDeclarationFiles({
|
|
|
422
419
|
},
|
|
423
420
|
dtsRollup: {
|
|
424
421
|
enabled: true,
|
|
425
|
-
publicTrimmedFilePath: (0,
|
|
422
|
+
publicTrimmedFilePath: (0, import_node_path3.resolve)(outputDir, fileName)
|
|
426
423
|
},
|
|
427
424
|
tsdocMetadata: {
|
|
428
425
|
enabled: false
|
|
@@ -695,7 +692,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
695
692
|
const service = project.getLanguageService();
|
|
696
693
|
const outputFiles = project.getSourceFiles().map(
|
|
697
694
|
(sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
698
|
-
path: (0, import_vite2.normalizePath)((0,
|
|
695
|
+
path: (0, import_vite2.normalizePath)((0, import_node_path4.resolve)(root, outputFile.compilerObject.name)),
|
|
699
696
|
content: outputFile.getText()
|
|
700
697
|
}))
|
|
701
698
|
).flat().concat(dtsOutputFiles);
|
|
@@ -718,9 +715,9 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
718
715
|
content = transformAliasImport(filePath, content, aliases, aliasesExclude);
|
|
719
716
|
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
720
717
|
}
|
|
721
|
-
filePath = (0,
|
|
718
|
+
filePath = (0, import_node_path4.resolve)(
|
|
722
719
|
outputDir,
|
|
723
|
-
(0,
|
|
720
|
+
(0, import_node_path4.relative)(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath)
|
|
724
721
|
);
|
|
725
722
|
if (typeof beforeWriteFile === "function") {
|
|
726
723
|
const result = beforeWriteFile(filePath, content);
|
|
@@ -731,7 +728,7 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
731
728
|
content = result.content ?? content;
|
|
732
729
|
}
|
|
733
730
|
}
|
|
734
|
-
await import_fs_extra.default.mkdir((0,
|
|
731
|
+
await import_fs_extra.default.mkdir((0, import_node_path4.dirname)(filePath), { recursive: true });
|
|
735
732
|
await import_fs_extra.default.writeFile(
|
|
736
733
|
filePath,
|
|
737
734
|
cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content,
|
|
@@ -741,14 +738,14 @@ ${logPrefix} Start generate declaration files...`));
|
|
|
741
738
|
});
|
|
742
739
|
bundleDebug("output");
|
|
743
740
|
if (insertTypesEntry || rollupTypes) {
|
|
744
|
-
const pkgPath = (0,
|
|
741
|
+
const pkgPath = (0, import_node_path4.resolve)(root, "package.json");
|
|
745
742
|
const pkg = import_fs_extra.default.existsSync(pkgPath) ? JSON.parse(await import_fs_extra.default.readFile(pkgPath, "utf-8")) : {};
|
|
746
743
|
const types = pkg.types || pkg.typings;
|
|
747
|
-
let typesPath = types ? (0,
|
|
744
|
+
let typesPath = types ? (0, import_node_path4.resolve)(root, types) : (0, import_node_path4.resolve)(outputDir, indexName);
|
|
748
745
|
if (!import_fs_extra.default.existsSync(typesPath)) {
|
|
749
746
|
const entry = entries[0];
|
|
750
|
-
const outputIndex = (0,
|
|
751
|
-
let filePath = (0, import_vite2.normalizePath)((0,
|
|
747
|
+
const outputIndex = (0, import_node_path4.resolve)(outputDir, (0, import_node_path4.relative)(entryRoot, entry.replace(tsRE, ".d.ts")));
|
|
748
|
+
let filePath = (0, import_vite2.normalizePath)((0, import_node_path4.relative)((0, import_node_path4.dirname)(typesPath), outputIndex));
|
|
752
749
|
filePath = filePath.replace(dtsRE2, "");
|
|
753
750
|
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
754
751
|
let content = `export * from '${filePath}'
|
|
@@ -783,7 +780,7 @@ export default ${libName}
|
|
|
783
780
|
compilerOptions,
|
|
784
781
|
outputDir,
|
|
785
782
|
entryPath: typesPath,
|
|
786
|
-
fileName: (0,
|
|
783
|
+
fileName: (0, import_node_path4.basename)(typesPath)
|
|
787
784
|
});
|
|
788
785
|
const wroteFile = (0, import_vite2.normalizePath)(typesPath);
|
|
789
786
|
wroteFiles.delete(wroteFile);
|
|
@@ -793,7 +790,7 @@ export default ${libName}
|
|
|
793
790
|
wroteFiles.add(wroteFile);
|
|
794
791
|
if (copyDtsFiles) {
|
|
795
792
|
await runParallel(import_os.default.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
796
|
-
const filePath = (0,
|
|
793
|
+
const filePath = (0, import_node_path4.resolve)(outputDir, (0, import_node_path4.basename)(path));
|
|
797
794
|
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
798
795
|
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
799
796
|
});
|
|
@@ -804,12 +801,12 @@ export default ${libName}
|
|
|
804
801
|
if (outputDirs.length > 1) {
|
|
805
802
|
const dirs = outputDirs.slice(1);
|
|
806
803
|
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), async (wroteFile) => {
|
|
807
|
-
const relativePath = (0,
|
|
804
|
+
const relativePath = (0, import_node_path4.relative)(outputDir, wroteFile);
|
|
808
805
|
const content = await import_fs_extra.default.readFile(wroteFile, "utf-8");
|
|
809
806
|
await Promise.all(
|
|
810
807
|
dirs.map(async (dir) => {
|
|
811
|
-
const filePath = (0,
|
|
812
|
-
await import_fs_extra.default.mkdir((0,
|
|
808
|
+
const filePath = (0, import_node_path4.resolve)(dir, relativePath);
|
|
809
|
+
await import_fs_extra.default.mkdir((0, import_node_path4.dirname)(filePath), { recursive: true });
|
|
813
810
|
await import_fs_extra.default.writeFile(filePath, content, "utf-8");
|
|
814
811
|
})
|
|
815
812
|
);
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
1
9
|
// src/plugin.ts
|
|
2
|
-
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "path";
|
|
10
|
+
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "node:path";
|
|
3
11
|
import fs from "fs-extra";
|
|
4
12
|
import os from "os";
|
|
5
13
|
import chalk from "chalk";
|
|
@@ -10,12 +18,12 @@ import { normalizePath as normalizePath2 } from "vite";
|
|
|
10
18
|
import { readConfigFile } from "typescript";
|
|
11
19
|
|
|
12
20
|
// src/transform.ts
|
|
13
|
-
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "path";
|
|
21
|
+
import { dirname as dirname2, relative, isAbsolute as isAbsolute2 } from "node:path";
|
|
14
22
|
import { normalizePath } from "vite";
|
|
15
23
|
|
|
16
24
|
// src/utils.ts
|
|
17
|
-
import { resolve, isAbsolute, dirname, normalize, sep } from "path";
|
|
18
|
-
import { existsSync, readdirSync, lstatSync, rmdirSync } from "fs";
|
|
25
|
+
import { resolve, isAbsolute, dirname, normalize, sep } from "node:path";
|
|
26
|
+
import { existsSync, readdirSync, lstatSync, rmdirSync } from "node:fs";
|
|
19
27
|
function isNativeObj(value) {
|
|
20
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
21
29
|
}
|
|
@@ -239,7 +247,6 @@ function transferSetupPosition(content) {
|
|
|
239
247
|
}
|
|
240
248
|
|
|
241
249
|
// src/compile.ts
|
|
242
|
-
import { createRequire } from "node:module";
|
|
243
250
|
var exportDefaultRE = /export\s+default/;
|
|
244
251
|
var exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/;
|
|
245
252
|
var noScriptContent = "import { defineComponent } from 'vue'\nexport default defineComponent({})";
|
|
@@ -247,21 +254,20 @@ var index = 1;
|
|
|
247
254
|
var compileRoot = null;
|
|
248
255
|
var compiler;
|
|
249
256
|
var vue;
|
|
250
|
-
var _require = createRequire(import.meta.url);
|
|
251
257
|
function requireCompiler() {
|
|
252
258
|
if (!compiler) {
|
|
253
259
|
if (compileRoot) {
|
|
254
260
|
try {
|
|
255
|
-
compiler =
|
|
261
|
+
compiler = __require(__require.resolve("vue/compiler-sfc", { paths: [compileRoot] }));
|
|
256
262
|
} catch (e) {
|
|
257
263
|
}
|
|
258
264
|
}
|
|
259
265
|
if (!compiler) {
|
|
260
266
|
try {
|
|
261
|
-
compiler =
|
|
267
|
+
compiler = __require("vue/compiler-sfc");
|
|
262
268
|
} catch (e) {
|
|
263
269
|
try {
|
|
264
|
-
compiler =
|
|
270
|
+
compiler = __require("@vue/compiler-sfc");
|
|
265
271
|
} catch (e2) {
|
|
266
272
|
throw new Error("@vue/compiler-sfc is not present in the dependency tree.\n");
|
|
267
273
|
}
|
|
@@ -274,13 +280,13 @@ function isVue3() {
|
|
|
274
280
|
if (!vue) {
|
|
275
281
|
if (compileRoot) {
|
|
276
282
|
try {
|
|
277
|
-
vue =
|
|
283
|
+
vue = __require(__require.resolve("vue", { paths: [compileRoot] }));
|
|
278
284
|
} catch (e) {
|
|
279
285
|
}
|
|
280
286
|
}
|
|
281
287
|
if (!vue) {
|
|
282
288
|
try {
|
|
283
|
-
vue =
|
|
289
|
+
vue = __require("vue");
|
|
284
290
|
} catch (e) {
|
|
285
291
|
throw new Error("vue is not present in the dependency tree.\n");
|
|
286
292
|
}
|
|
@@ -350,7 +356,7 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
350
356
|
}
|
|
351
357
|
|
|
352
358
|
// src/rollup.ts
|
|
353
|
-
import { resolve as resolve2 } from "path";
|
|
359
|
+
import { resolve as resolve2 } from "node:path";
|
|
354
360
|
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
355
361
|
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
356
362
|
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|
package/package.json
CHANGED