vite-plugin-dts 1.0.5 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +192 -185
- package/README.zh-CN.md +7 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +167 -27
- package/dist/index.mjs +169 -26
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -1,185 +1,192 @@
|
|
|
1
|
-
# vite-plugin-dts
|
|
2
|
-
|
|
3
|
-
**English** | [中文](./README.zh-CN.md)
|
|
4
|
-
|
|
5
|
-
A vite plugin that generates declaration files (`*.d.ts`) from `.ts` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```sh
|
|
10
|
-
pnpm add vite-plugin-dts -D
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
In `vite.config.ts`:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import { resolve } from 'path'
|
|
19
|
-
import { defineConfig } from 'vite'
|
|
20
|
-
import dts from 'vite-plugin-dts'
|
|
21
|
-
|
|
22
|
-
export default defineConfig({
|
|
23
|
-
build: {
|
|
24
|
-
lib: {
|
|
25
|
-
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
-
name: 'MyLib',
|
|
27
|
-
formats: ['es'],
|
|
28
|
-
fileName: 'my-lib'
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
plugins: [dts()]
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
In your component:
|
|
36
|
-
|
|
37
|
-
```vue
|
|
38
|
-
<template>
|
|
39
|
-
<div></div>
|
|
40
|
-
</template>
|
|
41
|
-
|
|
42
|
-
<script lang="ts">
|
|
43
|
-
// using defineComponent for inferring types
|
|
44
|
-
import { defineComponent } from 'vue'
|
|
45
|
-
|
|
46
|
-
export default defineComponent({
|
|
47
|
-
name: 'Component'
|
|
48
|
-
})
|
|
49
|
-
</script>
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
```vue
|
|
53
|
-
<script setup lang="ts">
|
|
54
|
-
// Need to access the defineProps returned value to
|
|
55
|
-
// infer types although you never use the props directly
|
|
56
|
-
const props = defineProps<{
|
|
57
|
-
color: 'blue' | 'red'
|
|
58
|
-
}>()
|
|
59
|
-
</script>
|
|
60
|
-
|
|
61
|
-
<template>
|
|
62
|
-
<div>{{ color }}</div>
|
|
63
|
-
</template>
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Options
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
-
|
|
71
|
-
interface TransformWriteFile {
|
|
72
|
-
filePath?: string
|
|
73
|
-
content?: string
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface PluginOptions {
|
|
77
|
-
// Depends on the root directory
|
|
78
|
-
// Defaults base on your vite config root options
|
|
79
|
-
root?: string
|
|
80
|
-
|
|
81
|
-
// Declaration files output directory
|
|
82
|
-
// Defaults base on your vite config output options
|
|
83
|
-
outputDir?: string
|
|
84
|
-
|
|
85
|
-
// Manually set the root path of the entry files
|
|
86
|
-
// The output path of each file will be caculated base on it
|
|
87
|
-
// Defaults is the smallest public path for all files
|
|
88
|
-
entryRoot?: string
|
|
89
|
-
|
|
90
|
-
// Project init compilerOptions using by ts-morph
|
|
91
|
-
// Default: null
|
|
92
|
-
compilerOptions?: ts.CompilerOptions | null
|
|
93
|
-
|
|
94
|
-
// Project init tsconfig.json file path by ts-morph
|
|
95
|
-
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
96
|
-
// Default: 'tsconfig.json'
|
|
97
|
-
tsConfigFilePath?: string
|
|
98
|
-
|
|
99
|
-
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
100
|
-
// Default: false
|
|
101
|
-
cleanVueFileName?: boolean
|
|
102
|
-
|
|
103
|
-
// Whether transform dynamic import to static
|
|
104
|
-
//
|
|
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
|
-
|
|
1
|
+
# vite-plugin-dts
|
|
2
|
+
|
|
3
|
+
**English** | [中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A vite plugin that generates declaration files (`*.d.ts`) from `.ts` or `.vue` source files when using vite in [library mode](https://vitejs.dev/guide/build.html#library-mode).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add vite-plugin-dts -D
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
In `vite.config.ts`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { resolve } from 'path'
|
|
19
|
+
import { defineConfig } from 'vite'
|
|
20
|
+
import dts from 'vite-plugin-dts'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
build: {
|
|
24
|
+
lib: {
|
|
25
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
26
|
+
name: 'MyLib',
|
|
27
|
+
formats: ['es'],
|
|
28
|
+
fileName: 'my-lib'
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
plugins: [dts()]
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
In your component:
|
|
36
|
+
|
|
37
|
+
```vue
|
|
38
|
+
<template>
|
|
39
|
+
<div></div>
|
|
40
|
+
</template>
|
|
41
|
+
|
|
42
|
+
<script lang="ts">
|
|
43
|
+
// using defineComponent for inferring types
|
|
44
|
+
import { defineComponent } from 'vue'
|
|
45
|
+
|
|
46
|
+
export default defineComponent({
|
|
47
|
+
name: 'Component'
|
|
48
|
+
})
|
|
49
|
+
</script>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
// Need to access the defineProps returned value to
|
|
55
|
+
// infer types although you never use the props directly
|
|
56
|
+
const props = defineProps<{
|
|
57
|
+
color: 'blue' | 'red'
|
|
58
|
+
}>()
|
|
59
|
+
</script>
|
|
60
|
+
|
|
61
|
+
<template>
|
|
62
|
+
<div>{{ color }}</div>
|
|
63
|
+
</template>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Options
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import type { ts, Diagnostic } from 'ts-morph'
|
|
70
|
+
|
|
71
|
+
interface TransformWriteFile {
|
|
72
|
+
filePath?: string
|
|
73
|
+
content?: string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface PluginOptions {
|
|
77
|
+
// Depends on the root directory
|
|
78
|
+
// Defaults base on your vite config root options
|
|
79
|
+
root?: string
|
|
80
|
+
|
|
81
|
+
// Declaration files output directory
|
|
82
|
+
// Defaults base on your vite config output options
|
|
83
|
+
outputDir?: string
|
|
84
|
+
|
|
85
|
+
// Manually set the root path of the entry files
|
|
86
|
+
// The output path of each file will be caculated base on it
|
|
87
|
+
// Defaults is the smallest public path for all files
|
|
88
|
+
entryRoot?: string
|
|
89
|
+
|
|
90
|
+
// Project init compilerOptions using by ts-morph
|
|
91
|
+
// Default: null
|
|
92
|
+
compilerOptions?: ts.CompilerOptions | null
|
|
93
|
+
|
|
94
|
+
// Project init tsconfig.json file path by ts-morph
|
|
95
|
+
// Plugin also resolve incldue and exclude files from tsconfig.json
|
|
96
|
+
// Default: 'tsconfig.json'
|
|
97
|
+
tsConfigFilePath?: string
|
|
98
|
+
|
|
99
|
+
// Whether transform file name '.vue.d.ts' to '.d.ts'
|
|
100
|
+
// Default: false
|
|
101
|
+
cleanVueFileName?: boolean
|
|
102
|
+
|
|
103
|
+
// Whether transform dynamic import to static
|
|
104
|
+
// Force true when `rollupTypes` is effective
|
|
105
|
+
// eg. 'import('vue').DefineComponent' to 'import { DefineComponent } from "vue"'
|
|
106
|
+
// Default: false
|
|
107
|
+
staticImport?: boolean
|
|
108
|
+
|
|
109
|
+
// Manual set include glob
|
|
110
|
+
// Defaults base on your tsconfig.json include option
|
|
111
|
+
include?: string | string[]
|
|
112
|
+
|
|
113
|
+
// Manual set exclude glob
|
|
114
|
+
// Defaults base on your tsconfig.json exclude option, be 'node_module/**' when empty
|
|
115
|
+
exclude?: string | string[]
|
|
116
|
+
|
|
117
|
+
// Whether generate types entry file
|
|
118
|
+
// When true will from package.json types field if exists or `${outputDir}/index.d.ts`
|
|
119
|
+
// Force true when `rollupTypes` is effective
|
|
120
|
+
// Default: false
|
|
121
|
+
insertTypesEntry?: boolean
|
|
122
|
+
|
|
123
|
+
// Set to rollup declaration files after emit
|
|
124
|
+
// Power by `@microsoft/api-extractor`, it will start a new program which takes some time
|
|
125
|
+
// Default: false
|
|
126
|
+
rollupTypes?: boolean
|
|
127
|
+
|
|
128
|
+
// Whether copy .d.ts source files into outputDir
|
|
129
|
+
// Default: true
|
|
130
|
+
copyDtsFiles?: boolean
|
|
131
|
+
|
|
132
|
+
// Whether emit nothing when has any diagnostic
|
|
133
|
+
// Default: false
|
|
134
|
+
noEmitOnError?: boolean
|
|
135
|
+
|
|
136
|
+
// Whether skip typescript diagnostics
|
|
137
|
+
// Skip type diagnostics means that type errors will not interrupt the build process
|
|
138
|
+
// But for the source files with type errors will not be emitted
|
|
139
|
+
// Default: true
|
|
140
|
+
skipDiagnostics?: boolean
|
|
141
|
+
|
|
142
|
+
// Whether log diagnostic informations
|
|
143
|
+
// Not effective when `skipDiagnostics` is true
|
|
144
|
+
// Default: false
|
|
145
|
+
logDiagnostics?: boolean
|
|
146
|
+
|
|
147
|
+
// After emit diagnostic hook
|
|
148
|
+
// According to the length to judge whether there is any type error
|
|
149
|
+
// Default: () => {}
|
|
150
|
+
afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
|
|
151
|
+
|
|
152
|
+
// Before declaration file be writed hook
|
|
153
|
+
// You can transform declaration file-path and content through it
|
|
154
|
+
// Default: () => {}
|
|
155
|
+
beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
|
|
156
|
+
|
|
157
|
+
// After build hook
|
|
158
|
+
// It wil be called after all declaration files are written
|
|
159
|
+
// Default: () => {}
|
|
160
|
+
afterBuild?: () => void | Promise<void>
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Example
|
|
165
|
+
|
|
166
|
+
Clone and run the following script:
|
|
167
|
+
|
|
168
|
+
```sh
|
|
169
|
+
pnpm run test:e2e
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Then check `example/types`.
|
|
173
|
+
|
|
174
|
+
## FAQ
|
|
175
|
+
|
|
176
|
+
Here are some FAQ's and solutions.
|
|
177
|
+
|
|
178
|
+
### Missing some declaration files after build
|
|
179
|
+
|
|
180
|
+
By default `skipDiagnostics` option is `true`, which means that type diagnostic will be skipped during the build process (some projects may have diagnostic tools such as `vue-tsc`). If there are some files with type errors which interrupt the build process, these files will not be emitted (declaration files won't be generated).
|
|
181
|
+
|
|
182
|
+
If your project doesn't use type diagnostic tools, you can set `skipDiagnostics: false` and `logDiagnostics: true` to turn on the diagnostic and log features of this plugin. It will help you check the type errors during build and log error information to the terminal.
|
|
183
|
+
|
|
184
|
+
### Take type error when using both `script` and `setup-script` in vue component
|
|
185
|
+
|
|
186
|
+
This is usually caused by using `defineComponent` function in both `script` and `setup-script`. When `vue/compiler-sfc` compiles these files, the default export result from `script` gets merged with the parameter object of `defineComponent` from `setup-script`. This is incompatible with parameters and types returned from `defineComponent`, which results in a type error.
|
|
187
|
+
|
|
188
|
+
Here is a simple [example](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue), you should remove the `defineComponent` which in `script` and export a native object directly.
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT License.
|
package/README.zh-CN.md
CHANGED
|
@@ -100,6 +100,7 @@ export interface PluginOptions {
|
|
|
100
100
|
cleanVueFileName?: boolean
|
|
101
101
|
|
|
102
102
|
//是否将动态引入转换为静态
|
|
103
|
+
// 当开启打包类型文件时强制为 true
|
|
103
104
|
// eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
|
|
104
105
|
// 默认值: false
|
|
105
106
|
staticImport?: boolean
|
|
@@ -114,9 +115,15 @@ export interface PluginOptions {
|
|
|
114
115
|
|
|
115
116
|
// 是否生成类型声明入口
|
|
116
117
|
// 当为 true 时会基于 package.json 的 tpyes 字段生成,或者 `${outputDir}/index.d.ts`
|
|
118
|
+
// 当开启打包类型文件时强制为 true
|
|
117
119
|
// 默认值: false
|
|
118
120
|
insertTypesEntry?: boolean
|
|
119
121
|
|
|
122
|
+
// 设置是否在发出类型文件后将其打包
|
|
123
|
+
// 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
|
|
124
|
+
// 默认值: false
|
|
125
|
+
rollupTypes?: boolean
|
|
126
|
+
|
|
120
127
|
// 是否将源码里的 .d.ts 文件复制到 outputDir
|
|
121
128
|
// 默认值: true
|
|
122
129
|
copyDtsFiles?: boolean
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -28,7 +28,7 @@ module.exports = dtsPlugin;
|
|
|
28
28
|
dtsPlugin['default'] = dtsPlugin;
|
|
29
29
|
|
|
30
30
|
// src/plugin.ts
|
|
31
|
-
var
|
|
31
|
+
var import_path4 = require("path");
|
|
32
32
|
var import_fs_extra = __toESM(require("fs-extra"));
|
|
33
33
|
var import_os = __toESM(require("os"));
|
|
34
34
|
var import_chalk = __toESM(require("chalk"));
|
|
@@ -44,6 +44,7 @@ var import_vite = require("vite");
|
|
|
44
44
|
|
|
45
45
|
// src/utils.ts
|
|
46
46
|
var import_path = require("path");
|
|
47
|
+
var import_fs = require("fs");
|
|
47
48
|
function isNativeObj(value) {
|
|
48
49
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
49
50
|
}
|
|
@@ -146,6 +147,26 @@ function queryPublicPath(paths) {
|
|
|
146
147
|
}
|
|
147
148
|
return publicPath.slice(0, -1);
|
|
148
149
|
}
|
|
150
|
+
function removeDirIfEmpty(dir) {
|
|
151
|
+
if (!(0, import_fs.existsSync)(dir)) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
let onlyHasDir = true;
|
|
155
|
+
for (const file of (0, import_fs.readdirSync)(dir)) {
|
|
156
|
+
const abs = (0, import_path.resolve)(dir, file);
|
|
157
|
+
if ((0, import_fs.lstatSync)(abs).isDirectory()) {
|
|
158
|
+
if (!removeDirIfEmpty(abs)) {
|
|
159
|
+
onlyHasDir = false;
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
onlyHasDir = false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (onlyHasDir) {
|
|
166
|
+
(0, import_fs.rmdirSync)(dir);
|
|
167
|
+
}
|
|
168
|
+
return onlyHasDir;
|
|
169
|
+
}
|
|
149
170
|
|
|
150
171
|
// src/transform.ts
|
|
151
172
|
var globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
@@ -291,24 +312,107 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
291
312
|
content += "\nexport default _sfc_main\n";
|
|
292
313
|
ext = scriptSetup.lang || "js";
|
|
293
314
|
} else if (script && script.content) {
|
|
294
|
-
content = script.content;
|
|
315
|
+
content = rewriteDefault(script.content, "_sfc_main");
|
|
316
|
+
content += "\nexport default _sfc_main\n";
|
|
295
317
|
ext = script.lang || "js";
|
|
296
318
|
}
|
|
297
319
|
}
|
|
298
320
|
return { content, ext };
|
|
299
321
|
}
|
|
300
322
|
|
|
323
|
+
// src/rollup.ts
|
|
324
|
+
var import_path3 = require("path");
|
|
325
|
+
var import_api_extractor = require("@microsoft/api-extractor");
|
|
326
|
+
var import_Collector = require("@microsoft/api-extractor/lib/collector/Collector");
|
|
327
|
+
var import_MessageRouter = require("@microsoft/api-extractor/lib/collector/MessageRouter");
|
|
328
|
+
var import_DtsRollupGenerator = require("@microsoft/api-extractor/lib/generators/DtsRollupGenerator");
|
|
329
|
+
var import_node_core_library = require("@rushstack/node-core-library");
|
|
330
|
+
var dtsRE = /\.d\.tsx?$/;
|
|
331
|
+
function rollupDeclarationFiles({
|
|
332
|
+
root,
|
|
333
|
+
tsConfigPath,
|
|
334
|
+
outputDir,
|
|
335
|
+
entryPath,
|
|
336
|
+
fileName
|
|
337
|
+
}) {
|
|
338
|
+
const configObjectFullPath = (0, import_path3.resolve)(root, "api-extractor.json");
|
|
339
|
+
const packageJsonLookup = new import_node_core_library.PackageJsonLookup();
|
|
340
|
+
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
341
|
+
if (!dtsRE.test(fileName)) {
|
|
342
|
+
fileName += ".d.ts";
|
|
343
|
+
}
|
|
344
|
+
const extractorConfig = import_api_extractor.ExtractorConfig.prepare({
|
|
345
|
+
configObject: {
|
|
346
|
+
projectFolder: root,
|
|
347
|
+
mainEntryPointFilePath: entryPath,
|
|
348
|
+
compiler: {
|
|
349
|
+
tsconfigFilePath: tsConfigPath
|
|
350
|
+
},
|
|
351
|
+
apiReport: {
|
|
352
|
+
enabled: false,
|
|
353
|
+
reportFileName: "<unscopedPackageName>.api.md"
|
|
354
|
+
},
|
|
355
|
+
docModel: {
|
|
356
|
+
enabled: false
|
|
357
|
+
},
|
|
358
|
+
dtsRollup: {
|
|
359
|
+
enabled: true,
|
|
360
|
+
publicTrimmedFilePath: (0, import_path3.resolve)(outputDir, fileName)
|
|
361
|
+
},
|
|
362
|
+
tsdocMetadata: {
|
|
363
|
+
enabled: false
|
|
364
|
+
},
|
|
365
|
+
messages: {
|
|
366
|
+
compilerMessageReporting: {
|
|
367
|
+
default: {
|
|
368
|
+
logLevel: "none"
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
extractorMessageReporting: {
|
|
372
|
+
default: {
|
|
373
|
+
logLevel: "none"
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
},
|
|
378
|
+
configObjectFullPath,
|
|
379
|
+
packageJsonFullPath
|
|
380
|
+
});
|
|
381
|
+
const compilerState = import_api_extractor.CompilerState.create(extractorConfig, {
|
|
382
|
+
localBuild: true,
|
|
383
|
+
showVerboseMessages: false
|
|
384
|
+
});
|
|
385
|
+
const messageRouter = new import_MessageRouter.MessageRouter({
|
|
386
|
+
workingPackageFolder: root,
|
|
387
|
+
messageCallback: void 0,
|
|
388
|
+
messagesConfig: extractorConfig.messages,
|
|
389
|
+
showVerboseMessages: false,
|
|
390
|
+
showDiagnostics: false,
|
|
391
|
+
tsdocConfiguration: extractorConfig.tsdocConfiguration
|
|
392
|
+
});
|
|
393
|
+
const collector = new import_Collector.Collector({
|
|
394
|
+
program: compilerState.program,
|
|
395
|
+
messageRouter,
|
|
396
|
+
extractorConfig
|
|
397
|
+
});
|
|
398
|
+
collector.analyze();
|
|
399
|
+
import_DtsRollupGenerator.DtsRollupGenerator.writeTypingsFile(collector, extractorConfig.publicTrimmedFilePath, import_DtsRollupGenerator.DtsRollupKind.PublicRelease, extractorConfig.newlineKind);
|
|
400
|
+
}
|
|
401
|
+
|
|
301
402
|
// src/plugin.ts
|
|
302
403
|
var noneExport = "export {};\n";
|
|
303
404
|
var virtualPrefix = "\0";
|
|
304
405
|
var vueRE = /\.vue$/;
|
|
305
406
|
var tsRE = /\.tsx?$/;
|
|
306
407
|
var jsRE = /\.jsx?$/;
|
|
307
|
-
var
|
|
408
|
+
var dtsRE2 = /\.d\.tsx?$/;
|
|
308
409
|
var tjsRE = /\.(t|j)sx?$/;
|
|
309
410
|
var watchExtensionRE = /\.(vue|(t|j)sx?)$/;
|
|
411
|
+
var fullRelativeRE = /^\.\.?\//;
|
|
412
|
+
var defaultIndex = "index.d.ts";
|
|
310
413
|
var noop = () => {
|
|
311
414
|
};
|
|
415
|
+
var logPrefix = import_chalk.default.cyan("[vite:dts]");
|
|
312
416
|
var bundleDebug = (0, import_debug.debug)("vite-plugin-dts:bundle");
|
|
313
417
|
function dtsPlugin(options = {}) {
|
|
314
418
|
var _a, _b;
|
|
@@ -318,6 +422,7 @@ function dtsPlugin(options = {}) {
|
|
|
318
422
|
staticImport = false,
|
|
319
423
|
clearPureImport = true,
|
|
320
424
|
insertTypesEntry = false,
|
|
425
|
+
rollupTypes = false,
|
|
321
426
|
noEmitOnError = false,
|
|
322
427
|
skipDiagnostics = true,
|
|
323
428
|
logDiagnostics = false,
|
|
@@ -329,6 +434,8 @@ function dtsPlugin(options = {}) {
|
|
|
329
434
|
const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
|
|
330
435
|
let root;
|
|
331
436
|
let entryRoot = (_b = options.entryRoot) != null ? _b : "";
|
|
437
|
+
let libName;
|
|
438
|
+
let indexName;
|
|
332
439
|
let aliases;
|
|
333
440
|
let entries;
|
|
334
441
|
let logger;
|
|
@@ -357,7 +464,7 @@ function dtsPlugin(options = {}) {
|
|
|
357
464
|
}
|
|
358
465
|
},
|
|
359
466
|
configResolved(config) {
|
|
360
|
-
var _a2, _b2;
|
|
467
|
+
var _a2, _b2, _c;
|
|
361
468
|
if (isBundle)
|
|
362
469
|
return;
|
|
363
470
|
logger = config.logger;
|
|
@@ -365,8 +472,17 @@ function dtsPlugin(options = {}) {
|
|
|
365
472
|
logger.warn(import_chalk.default.yellow(`
|
|
366
473
|
${import_chalk.default.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
|
|
367
474
|
`));
|
|
475
|
+
libName = "_default";
|
|
476
|
+
indexName = defaultIndex;
|
|
477
|
+
} else {
|
|
478
|
+
const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
|
|
479
|
+
libName = config.build.lib.name || "_default";
|
|
480
|
+
indexName = typeof filename === "string" ? filename : filename("es");
|
|
481
|
+
if (!dtsRE2.test(indexName)) {
|
|
482
|
+
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
483
|
+
}
|
|
368
484
|
}
|
|
369
|
-
root = ensureAbsolute((
|
|
485
|
+
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
370
486
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
371
487
|
outputDir = options.outputDir ? ensureAbsolute(options.outputDir, root) : ensureAbsolute(config.build.outDir, root);
|
|
372
488
|
if (!outputDir) {
|
|
@@ -390,10 +506,10 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
390
506
|
tsConfigFilePath: tsConfigPath,
|
|
391
507
|
skipAddingFilesFromTsConfig: true
|
|
392
508
|
});
|
|
393
|
-
allowJs = (
|
|
509
|
+
allowJs = (_c = project.getCompilerOptions().allowJs) != null ? _c : false;
|
|
394
510
|
},
|
|
395
511
|
buildStart(inputOptions) {
|
|
396
|
-
if (insertTypesEntry) {
|
|
512
|
+
if (!isBundle && (insertTypesEntry || rollupTypes)) {
|
|
397
513
|
entries = Array.isArray(inputOptions.input) ? inputOptions.input : Object.values(inputOptions.input);
|
|
398
514
|
}
|
|
399
515
|
},
|
|
@@ -427,7 +543,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Can not resolve declaration directory
|
|
|
427
543
|
if (!outputDir || !project || isBundle)
|
|
428
544
|
return;
|
|
429
545
|
logger.info(import_chalk.default.green(`
|
|
430
|
-
${
|
|
546
|
+
${logPrefix} Start generate declaration files...`));
|
|
431
547
|
bundleDebug("start");
|
|
432
548
|
isBundle = true;
|
|
433
549
|
sourceDtsFiles.clear();
|
|
@@ -444,7 +560,7 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
444
560
|
ignore: ensureArray(exclude != null ? exclude : ["node_modules/**"]).map(normalizeGlob)
|
|
445
561
|
});
|
|
446
562
|
files.forEach((file) => {
|
|
447
|
-
if (
|
|
563
|
+
if (dtsRE2.test(file)) {
|
|
448
564
|
if (!copyDtsFiles) {
|
|
449
565
|
return;
|
|
450
566
|
}
|
|
@@ -475,19 +591,21 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
475
591
|
}
|
|
476
592
|
bundleDebug("diagnostics");
|
|
477
593
|
}
|
|
594
|
+
const dtsOutputFiles = Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
595
|
+
path: sourceFile.getFilePath(),
|
|
596
|
+
content: sourceFile.getFullText()
|
|
597
|
+
}));
|
|
478
598
|
const service = project.getLanguageService();
|
|
479
599
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
480
600
|
path: outputFile.getFilePath(),
|
|
481
601
|
content: outputFile.getText()
|
|
482
|
-
}))).flat().concat(
|
|
483
|
-
|
|
484
|
-
content: sourceFile.getFullText()
|
|
485
|
-
})));
|
|
602
|
+
}))).flat().concat(dtsOutputFiles);
|
|
603
|
+
bundleDebug("emit");
|
|
486
604
|
if (!entryRoot) {
|
|
487
605
|
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
488
606
|
}
|
|
489
607
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
490
|
-
|
|
608
|
+
const wroteFiles = /* @__PURE__ */ new Set();
|
|
491
609
|
await runParallel(import_os.default.cpus().length, outputFiles, async (outputFile) => {
|
|
492
610
|
var _a3, _b3;
|
|
493
611
|
let filePath = outputFile.path;
|
|
@@ -499,9 +617,9 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
499
617
|
if (!isMapFile && content && content !== noneExport) {
|
|
500
618
|
content = clearPureImport ? removePureImport(content) : content;
|
|
501
619
|
content = transformAliasImport(filePath, content, aliases);
|
|
502
|
-
content = staticImport ? transformDynamicImport(content) : content;
|
|
620
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
503
621
|
}
|
|
504
|
-
filePath = (0,
|
|
622
|
+
filePath = (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath));
|
|
505
623
|
if (typeof beforeWriteFile === "function") {
|
|
506
624
|
const result = beforeWriteFile(filePath, content);
|
|
507
625
|
if (result && isNativeObj(result)) {
|
|
@@ -509,21 +627,24 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
509
627
|
content = (_b3 = result.content) != null ? _b3 : content;
|
|
510
628
|
}
|
|
511
629
|
}
|
|
512
|
-
await import_fs_extra.default.mkdir((0,
|
|
630
|
+
await import_fs_extra.default.mkdir((0, import_path4.dirname)(filePath), { recursive: true });
|
|
513
631
|
await import_fs_extra.default.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf8");
|
|
632
|
+
wroteFiles.add((0, import_vite2.normalizePath)(filePath));
|
|
514
633
|
});
|
|
515
634
|
bundleDebug("output");
|
|
516
|
-
if (insertTypesEntry) {
|
|
517
|
-
const pkgPath = (0,
|
|
635
|
+
if (insertTypesEntry || rollupTypes) {
|
|
636
|
+
const pkgPath = (0, import_path4.resolve)(root, "package.json");
|
|
518
637
|
const pkg = import_fs_extra.default.existsSync(pkgPath) ? JSON.parse(await import_fs_extra.default.readFile(pkgPath, "utf-8")) : {};
|
|
519
|
-
let typesPath = pkg.types ? (0,
|
|
638
|
+
let typesPath = pkg.types ? (0, import_path4.resolve)(root, pkg.types) : (0, import_path4.resolve)(outputDir, indexName);
|
|
520
639
|
if (!import_fs_extra.default.existsSync(typesPath)) {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
640
|
+
const entry = entries[0];
|
|
641
|
+
let filePath = (0, import_vite2.normalizePath)((0, import_path4.relative)((0, import_path4.dirname)(typesPath), (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, entry))));
|
|
642
|
+
filePath = filePath.replace(tsRE, "");
|
|
643
|
+
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
644
|
+
let content = `import ${libName} from '${filePath}'
|
|
645
|
+
export default ${libName}
|
|
646
|
+
export * from '${filePath}'
|
|
647
|
+
`;
|
|
527
648
|
if (typeof beforeWriteFile === "function") {
|
|
528
649
|
const result = beforeWriteFile(typesPath, content);
|
|
529
650
|
if (result && isNativeObj(result)) {
|
|
@@ -534,13 +655,32 @@ ${import_chalk.default.cyan("[vite:dts]")} Start generate declaration files...`)
|
|
|
534
655
|
await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
|
|
535
656
|
}
|
|
536
657
|
bundleDebug("insert index");
|
|
658
|
+
if (rollupTypes) {
|
|
659
|
+
logger.info(import_chalk.default.green(`${logPrefix} Start rollup declaration files...`));
|
|
660
|
+
rollupDeclarationFiles({
|
|
661
|
+
root,
|
|
662
|
+
tsConfigPath,
|
|
663
|
+
outputDir,
|
|
664
|
+
entryPath: (0, import_path4.resolve)(outputDir, "src/index.d.ts"),
|
|
665
|
+
fileName: (0, import_path4.basename)(typesPath)
|
|
666
|
+
});
|
|
667
|
+
wroteFiles.delete((0, import_vite2.normalizePath)(typesPath));
|
|
668
|
+
await runParallel(import_os.default.cpus().length, Array.from(wroteFiles), (f) => import_fs_extra.default.unlink(f));
|
|
669
|
+
removeDirIfEmpty(outputDir);
|
|
670
|
+
if (copyDtsFiles) {
|
|
671
|
+
await runParallel(import_os.default.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
672
|
+
await import_fs_extra.default.writeFile((0, import_path4.resolve)(outputDir, (0, import_path4.basename)(path)), content, "utf8");
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
bundleDebug("rollup");
|
|
676
|
+
}
|
|
537
677
|
}
|
|
538
678
|
if (typeof afterBuild === "function") {
|
|
539
679
|
const result = afterBuild();
|
|
540
680
|
isPromise(result) && await result;
|
|
541
681
|
}
|
|
542
682
|
bundleDebug("finish");
|
|
543
|
-
logger.info(import_chalk.default.green(`${
|
|
683
|
+
logger.info(import_chalk.default.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
544
684
|
`));
|
|
545
685
|
}
|
|
546
686
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -7,7 +7,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
});
|
|
8
8
|
|
|
9
9
|
// src/plugin.ts
|
|
10
|
-
import { resolve as
|
|
10
|
+
import { resolve as resolve3, dirname as dirname3, relative as relative2, basename } from "path";
|
|
11
11
|
import fs from "fs-extra";
|
|
12
12
|
import os from "os";
|
|
13
13
|
import chalk from "chalk";
|
|
@@ -23,6 +23,7 @@ import { normalizePath } from "vite";
|
|
|
23
23
|
|
|
24
24
|
// src/utils.ts
|
|
25
25
|
import { resolve, isAbsolute, dirname, normalize, sep } from "path";
|
|
26
|
+
import { existsSync, readdirSync, lstatSync, rmdirSync } from "fs";
|
|
26
27
|
function isNativeObj(value) {
|
|
27
28
|
return Object.prototype.toString.call(value) === "[object Object]";
|
|
28
29
|
}
|
|
@@ -125,6 +126,26 @@ function queryPublicPath(paths) {
|
|
|
125
126
|
}
|
|
126
127
|
return publicPath.slice(0, -1);
|
|
127
128
|
}
|
|
129
|
+
function removeDirIfEmpty(dir) {
|
|
130
|
+
if (!existsSync(dir)) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
let onlyHasDir = true;
|
|
134
|
+
for (const file of readdirSync(dir)) {
|
|
135
|
+
const abs = resolve(dir, file);
|
|
136
|
+
if (lstatSync(abs).isDirectory()) {
|
|
137
|
+
if (!removeDirIfEmpty(abs)) {
|
|
138
|
+
onlyHasDir = false;
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
onlyHasDir = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (onlyHasDir) {
|
|
145
|
+
rmdirSync(dir);
|
|
146
|
+
}
|
|
147
|
+
return onlyHasDir;
|
|
148
|
+
}
|
|
128
149
|
|
|
129
150
|
// src/transform.ts
|
|
130
151
|
var globSuffixRE = /^((?:.*\.[^.]+)|(?:\*+))$/;
|
|
@@ -270,24 +291,110 @@ const _sfc_main = ${classMatch[1]}`;
|
|
|
270
291
|
content += "\nexport default _sfc_main\n";
|
|
271
292
|
ext = scriptSetup.lang || "js";
|
|
272
293
|
} else if (script && script.content) {
|
|
273
|
-
content = script.content;
|
|
294
|
+
content = rewriteDefault(script.content, "_sfc_main");
|
|
295
|
+
content += "\nexport default _sfc_main\n";
|
|
274
296
|
ext = script.lang || "js";
|
|
275
297
|
}
|
|
276
298
|
}
|
|
277
299
|
return { content, ext };
|
|
278
300
|
}
|
|
279
301
|
|
|
302
|
+
// src/rollup.ts
|
|
303
|
+
import { resolve as resolve2 } from "path";
|
|
304
|
+
import { ExtractorConfig, CompilerState } from "@microsoft/api-extractor";
|
|
305
|
+
import { Collector } from "@microsoft/api-extractor/lib/collector/Collector";
|
|
306
|
+
import { MessageRouter } from "@microsoft/api-extractor/lib/collector/MessageRouter";
|
|
307
|
+
import {
|
|
308
|
+
DtsRollupGenerator,
|
|
309
|
+
DtsRollupKind
|
|
310
|
+
} from "@microsoft/api-extractor/lib/generators/DtsRollupGenerator";
|
|
311
|
+
import { PackageJsonLookup } from "@rushstack/node-core-library";
|
|
312
|
+
var dtsRE = /\.d\.tsx?$/;
|
|
313
|
+
function rollupDeclarationFiles({
|
|
314
|
+
root,
|
|
315
|
+
tsConfigPath,
|
|
316
|
+
outputDir,
|
|
317
|
+
entryPath,
|
|
318
|
+
fileName
|
|
319
|
+
}) {
|
|
320
|
+
const configObjectFullPath = resolve2(root, "api-extractor.json");
|
|
321
|
+
const packageJsonLookup = new PackageJsonLookup();
|
|
322
|
+
const packageJsonFullPath = packageJsonLookup.tryGetPackageJsonFilePathFor(configObjectFullPath);
|
|
323
|
+
if (!dtsRE.test(fileName)) {
|
|
324
|
+
fileName += ".d.ts";
|
|
325
|
+
}
|
|
326
|
+
const extractorConfig = ExtractorConfig.prepare({
|
|
327
|
+
configObject: {
|
|
328
|
+
projectFolder: root,
|
|
329
|
+
mainEntryPointFilePath: entryPath,
|
|
330
|
+
compiler: {
|
|
331
|
+
tsconfigFilePath: tsConfigPath
|
|
332
|
+
},
|
|
333
|
+
apiReport: {
|
|
334
|
+
enabled: false,
|
|
335
|
+
reportFileName: "<unscopedPackageName>.api.md"
|
|
336
|
+
},
|
|
337
|
+
docModel: {
|
|
338
|
+
enabled: false
|
|
339
|
+
},
|
|
340
|
+
dtsRollup: {
|
|
341
|
+
enabled: true,
|
|
342
|
+
publicTrimmedFilePath: resolve2(outputDir, fileName)
|
|
343
|
+
},
|
|
344
|
+
tsdocMetadata: {
|
|
345
|
+
enabled: false
|
|
346
|
+
},
|
|
347
|
+
messages: {
|
|
348
|
+
compilerMessageReporting: {
|
|
349
|
+
default: {
|
|
350
|
+
logLevel: "none"
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
extractorMessageReporting: {
|
|
354
|
+
default: {
|
|
355
|
+
logLevel: "none"
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
configObjectFullPath,
|
|
361
|
+
packageJsonFullPath
|
|
362
|
+
});
|
|
363
|
+
const compilerState = CompilerState.create(extractorConfig, {
|
|
364
|
+
localBuild: true,
|
|
365
|
+
showVerboseMessages: false
|
|
366
|
+
});
|
|
367
|
+
const messageRouter = new MessageRouter({
|
|
368
|
+
workingPackageFolder: root,
|
|
369
|
+
messageCallback: void 0,
|
|
370
|
+
messagesConfig: extractorConfig.messages,
|
|
371
|
+
showVerboseMessages: false,
|
|
372
|
+
showDiagnostics: false,
|
|
373
|
+
tsdocConfiguration: extractorConfig.tsdocConfiguration
|
|
374
|
+
});
|
|
375
|
+
const collector = new Collector({
|
|
376
|
+
program: compilerState.program,
|
|
377
|
+
messageRouter,
|
|
378
|
+
extractorConfig
|
|
379
|
+
});
|
|
380
|
+
collector.analyze();
|
|
381
|
+
DtsRollupGenerator.writeTypingsFile(collector, extractorConfig.publicTrimmedFilePath, DtsRollupKind.PublicRelease, extractorConfig.newlineKind);
|
|
382
|
+
}
|
|
383
|
+
|
|
280
384
|
// src/plugin.ts
|
|
281
385
|
var noneExport = "export {};\n";
|
|
282
386
|
var virtualPrefix = "\0";
|
|
283
387
|
var vueRE = /\.vue$/;
|
|
284
388
|
var tsRE = /\.tsx?$/;
|
|
285
389
|
var jsRE = /\.jsx?$/;
|
|
286
|
-
var
|
|
390
|
+
var dtsRE2 = /\.d\.tsx?$/;
|
|
287
391
|
var tjsRE = /\.(t|j)sx?$/;
|
|
288
392
|
var watchExtensionRE = /\.(vue|(t|j)sx?)$/;
|
|
393
|
+
var fullRelativeRE = /^\.\.?\//;
|
|
394
|
+
var defaultIndex = "index.d.ts";
|
|
289
395
|
var noop = () => {
|
|
290
396
|
};
|
|
397
|
+
var logPrefix = chalk.cyan("[vite:dts]");
|
|
291
398
|
var bundleDebug = debug("vite-plugin-dts:bundle");
|
|
292
399
|
function dtsPlugin(options = {}) {
|
|
293
400
|
var _a, _b;
|
|
@@ -297,6 +404,7 @@ function dtsPlugin(options = {}) {
|
|
|
297
404
|
staticImport = false,
|
|
298
405
|
clearPureImport = true,
|
|
299
406
|
insertTypesEntry = false,
|
|
407
|
+
rollupTypes = false,
|
|
300
408
|
noEmitOnError = false,
|
|
301
409
|
skipDiagnostics = true,
|
|
302
410
|
logDiagnostics = false,
|
|
@@ -308,6 +416,8 @@ function dtsPlugin(options = {}) {
|
|
|
308
416
|
const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
|
|
309
417
|
let root;
|
|
310
418
|
let entryRoot = (_b = options.entryRoot) != null ? _b : "";
|
|
419
|
+
let libName;
|
|
420
|
+
let indexName;
|
|
311
421
|
let aliases;
|
|
312
422
|
let entries;
|
|
313
423
|
let logger;
|
|
@@ -336,7 +446,7 @@ function dtsPlugin(options = {}) {
|
|
|
336
446
|
}
|
|
337
447
|
},
|
|
338
448
|
configResolved(config) {
|
|
339
|
-
var _a2, _b2;
|
|
449
|
+
var _a2, _b2, _c;
|
|
340
450
|
if (isBundle)
|
|
341
451
|
return;
|
|
342
452
|
logger = config.logger;
|
|
@@ -344,8 +454,17 @@ function dtsPlugin(options = {}) {
|
|
|
344
454
|
logger.warn(chalk.yellow(`
|
|
345
455
|
${chalk.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
|
|
346
456
|
`));
|
|
457
|
+
libName = "_default";
|
|
458
|
+
indexName = defaultIndex;
|
|
459
|
+
} else {
|
|
460
|
+
const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
|
|
461
|
+
libName = config.build.lib.name || "_default";
|
|
462
|
+
indexName = typeof filename === "string" ? filename : filename("es");
|
|
463
|
+
if (!dtsRE2.test(indexName)) {
|
|
464
|
+
indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
|
|
465
|
+
}
|
|
347
466
|
}
|
|
348
|
-
root = ensureAbsolute((
|
|
467
|
+
root = ensureAbsolute((_b2 = options.root) != null ? _b2 : "", config.root);
|
|
349
468
|
tsConfigPath = ensureAbsolute(tsConfigFilePath, root);
|
|
350
469
|
outputDir = options.outputDir ? ensureAbsolute(options.outputDir, root) : ensureAbsolute(config.build.outDir, root);
|
|
351
470
|
if (!outputDir) {
|
|
@@ -369,10 +488,10 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
369
488
|
tsConfigFilePath: tsConfigPath,
|
|
370
489
|
skipAddingFilesFromTsConfig: true
|
|
371
490
|
});
|
|
372
|
-
allowJs = (
|
|
491
|
+
allowJs = (_c = project.getCompilerOptions().allowJs) != null ? _c : false;
|
|
373
492
|
},
|
|
374
493
|
buildStart(inputOptions) {
|
|
375
|
-
if (insertTypesEntry) {
|
|
494
|
+
if (!isBundle && (insertTypesEntry || rollupTypes)) {
|
|
376
495
|
entries = Array.isArray(inputOptions.input) ? inputOptions.input : Object.values(inputOptions.input);
|
|
377
496
|
}
|
|
378
497
|
},
|
|
@@ -406,7 +525,7 @@ ${chalk.cyan("[vite:dts]")} Can not resolve declaration directory, please check
|
|
|
406
525
|
if (!outputDir || !project || isBundle)
|
|
407
526
|
return;
|
|
408
527
|
logger.info(chalk.green(`
|
|
409
|
-
${
|
|
528
|
+
${logPrefix} Start generate declaration files...`));
|
|
410
529
|
bundleDebug("start");
|
|
411
530
|
isBundle = true;
|
|
412
531
|
sourceDtsFiles.clear();
|
|
@@ -423,7 +542,7 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
423
542
|
ignore: ensureArray(exclude != null ? exclude : ["node_modules/**"]).map(normalizeGlob)
|
|
424
543
|
});
|
|
425
544
|
files.forEach((file) => {
|
|
426
|
-
if (
|
|
545
|
+
if (dtsRE2.test(file)) {
|
|
427
546
|
if (!copyDtsFiles) {
|
|
428
547
|
return;
|
|
429
548
|
}
|
|
@@ -454,19 +573,21 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
454
573
|
}
|
|
455
574
|
bundleDebug("diagnostics");
|
|
456
575
|
}
|
|
576
|
+
const dtsOutputFiles = Array.from(sourceDtsFiles).map((sourceFile) => ({
|
|
577
|
+
path: sourceFile.getFilePath(),
|
|
578
|
+
content: sourceFile.getFullText()
|
|
579
|
+
}));
|
|
457
580
|
const service = project.getLanguageService();
|
|
458
581
|
const outputFiles = project.getSourceFiles().map((sourceFile) => service.getEmitOutput(sourceFile, true).getOutputFiles().map((outputFile) => ({
|
|
459
582
|
path: outputFile.getFilePath(),
|
|
460
583
|
content: outputFile.getText()
|
|
461
|
-
}))).flat().concat(
|
|
462
|
-
|
|
463
|
-
content: sourceFile.getFullText()
|
|
464
|
-
})));
|
|
584
|
+
}))).flat().concat(dtsOutputFiles);
|
|
585
|
+
bundleDebug("emit");
|
|
465
586
|
if (!entryRoot) {
|
|
466
587
|
entryRoot = queryPublicPath(outputFiles.map((file) => file.path));
|
|
467
588
|
}
|
|
468
589
|
entryRoot = ensureAbsolute(entryRoot, root);
|
|
469
|
-
|
|
590
|
+
const wroteFiles = /* @__PURE__ */ new Set();
|
|
470
591
|
await runParallel(os.cpus().length, outputFiles, async (outputFile) => {
|
|
471
592
|
var _a3, _b3;
|
|
472
593
|
let filePath = outputFile.path;
|
|
@@ -478,9 +599,9 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
478
599
|
if (!isMapFile && content && content !== noneExport) {
|
|
479
600
|
content = clearPureImport ? removePureImport(content) : content;
|
|
480
601
|
content = transformAliasImport(filePath, content, aliases);
|
|
481
|
-
content = staticImport ? transformDynamicImport(content) : content;
|
|
602
|
+
content = staticImport || rollupTypes ? transformDynamicImport(content) : content;
|
|
482
603
|
}
|
|
483
|
-
filePath =
|
|
604
|
+
filePath = resolve3(outputDir, relative2(entryRoot, cleanVueFileName ? filePath.replace(".vue.d.ts", ".d.ts") : filePath));
|
|
484
605
|
if (typeof beforeWriteFile === "function") {
|
|
485
606
|
const result = beforeWriteFile(filePath, content);
|
|
486
607
|
if (result && isNativeObj(result)) {
|
|
@@ -490,19 +611,22 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
490
611
|
}
|
|
491
612
|
await fs.mkdir(dirname3(filePath), { recursive: true });
|
|
492
613
|
await fs.writeFile(filePath, cleanVueFileName ? content.replace(/['"](.+)\.vue['"]/g, '"$1"') : content, "utf8");
|
|
614
|
+
wroteFiles.add(normalizePath2(filePath));
|
|
493
615
|
});
|
|
494
616
|
bundleDebug("output");
|
|
495
|
-
if (insertTypesEntry) {
|
|
496
|
-
const pkgPath =
|
|
617
|
+
if (insertTypesEntry || rollupTypes) {
|
|
618
|
+
const pkgPath = resolve3(root, "package.json");
|
|
497
619
|
const pkg = fs.existsSync(pkgPath) ? JSON.parse(await fs.readFile(pkgPath, "utf-8")) : {};
|
|
498
|
-
let typesPath = pkg.types ?
|
|
620
|
+
let typesPath = pkg.types ? resolve3(root, pkg.types) : resolve3(outputDir, indexName);
|
|
499
621
|
if (!fs.existsSync(typesPath)) {
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
622
|
+
const entry = entries[0];
|
|
623
|
+
let filePath = normalizePath2(relative2(dirname3(typesPath), resolve3(outputDir, relative2(entryRoot, entry))));
|
|
624
|
+
filePath = filePath.replace(tsRE, "");
|
|
625
|
+
filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
|
|
626
|
+
let content = `import ${libName} from '${filePath}'
|
|
627
|
+
export default ${libName}
|
|
628
|
+
export * from '${filePath}'
|
|
629
|
+
`;
|
|
506
630
|
if (typeof beforeWriteFile === "function") {
|
|
507
631
|
const result = beforeWriteFile(typesPath, content);
|
|
508
632
|
if (result && isNativeObj(result)) {
|
|
@@ -513,13 +637,32 @@ ${chalk.cyan("[vite:dts]")} Start generate declaration files...`));
|
|
|
513
637
|
await fs.writeFile(typesPath, content, "utf-8");
|
|
514
638
|
}
|
|
515
639
|
bundleDebug("insert index");
|
|
640
|
+
if (rollupTypes) {
|
|
641
|
+
logger.info(chalk.green(`${logPrefix} Start rollup declaration files...`));
|
|
642
|
+
rollupDeclarationFiles({
|
|
643
|
+
root,
|
|
644
|
+
tsConfigPath,
|
|
645
|
+
outputDir,
|
|
646
|
+
entryPath: resolve3(outputDir, "src/index.d.ts"),
|
|
647
|
+
fileName: basename(typesPath)
|
|
648
|
+
});
|
|
649
|
+
wroteFiles.delete(normalizePath2(typesPath));
|
|
650
|
+
await runParallel(os.cpus().length, Array.from(wroteFiles), (f) => fs.unlink(f));
|
|
651
|
+
removeDirIfEmpty(outputDir);
|
|
652
|
+
if (copyDtsFiles) {
|
|
653
|
+
await runParallel(os.cpus().length, dtsOutputFiles, async ({ path, content }) => {
|
|
654
|
+
await fs.writeFile(resolve3(outputDir, basename(path)), content, "utf8");
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
bundleDebug("rollup");
|
|
658
|
+
}
|
|
516
659
|
}
|
|
517
660
|
if (typeof afterBuild === "function") {
|
|
518
661
|
const result = afterBuild();
|
|
519
662
|
isPromise(result) && await result;
|
|
520
663
|
}
|
|
521
664
|
bundleDebug("finish");
|
|
522
|
-
logger.info(chalk.green(`${
|
|
665
|
+
logger.info(chalk.green(`${logPrefix} Declaration files built in ${Date.now() - startTime}ms.
|
|
523
666
|
`));
|
|
524
667
|
}
|
|
525
668
|
};
|
package/package.json
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
"url": "https://github.com/qmhc/vite-plugin-dts/issues"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
+
"@microsoft/api-extractor": "^7.20.0",
|
|
10
|
+
"@rushstack/node-core-library": "^3.45.1",
|
|
9
11
|
"chalk": "^4.1.2",
|
|
10
12
|
"debug": "^4.3.4",
|
|
11
13
|
"fast-glob": "^3.2.11",
|
|
@@ -91,5 +93,5 @@
|
|
|
91
93
|
"test:e2e": "cd example && cross-env DEBUG=\"vite-plugin-dts:bundle\" vite build"
|
|
92
94
|
},
|
|
93
95
|
"types": "dist/index.d.ts",
|
|
94
|
-
"version": "1.0
|
|
96
|
+
"version": "1.1.0"
|
|
95
97
|
}
|