vite-plugin-dts 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md 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
- // 默认基于 vite 配置的输出目录
82
- outputDir?: string
83
-
84
- // 用于手动设置入口文件的根路径
85
- // 在计算每个文件的输出路径时将基于该路径
86
- // 默认为所有文件的最小公共路径
87
- entryRoot?: string
88
-
89
- // 提供给 ts-morph Project 初始化的 compilerOptions 选项
90
- // 默认值: null
91
- compilerOptions?: ts.CompilerOptions | null
92
-
93
- // 提供给 ts-morph Project 初始化的 tsconfig.json 路径
94
- // 插件也会读取 tsconfig.json 的 incldue 和 exclude 选项来解析文件
95
- // 默认值: 'tsconfig.json'
96
- tsConfigFilePath?: string
97
-
98
- // 是否将 '.vue.d.ts' 文件名转换为 '.d.ts'
99
- // 默认值: false
100
- cleanVueFileName?: boolean
101
-
102
- //是否将动态引入转换为静态
103
- // 当开启打包类型文件时强制为 true
104
- // eg. 'import('vue').DefineComponent' 转换为 'import { DefineComponent } from "vue"'
105
- // 默认值: false
106
- staticImport?: boolean
107
-
108
- // 手动设置包含路径的 glob
109
- // 默认基于 tsconfig.json 的 include 选项
110
- include?: string | string[]
111
-
112
- // 手动设置排除路径的 glob
113
- // 默认基于 tsconfig.json 的 exclude 选线,未设置时为 'node_module/**'
114
- exclude?: string | string[]
115
-
116
- // 是否生成类型声明入口
117
- // 当为 true 时会基于 package.json 的 types 字段生成,或者 `${outputDir}/index.d.ts`
118
- // 当开启打包类型文件时强制为 true
119
- // 默认值: false
120
- insertTypesEntry?: boolean
121
-
122
- // 设置是否在发出类型文件后将其打包
123
- // 基于 `@microsoft/api-extractor`,由于这开启了一个新的进程,将会消耗一些时间
124
- // 默认值: false
125
- rollupTypes?: boolean
126
-
127
- // 是否将源码里的 .d.ts 文件复制到 outputDir
128
- // 默认值: true
129
- copyDtsFiles?: boolean
130
-
131
- // 出现类型诊断信息时不生成类型文件
132
- // 默认值: false
133
- noEmitOnError?: boolean
134
-
135
- // 是否跳过类型诊断
136
- // 跳过类型诊断意味着出现错误时不会中断打包进程的执行
137
- // 但对于出现错误的源文件,将无法生成相应的类型文件
138
- // 默认值: true
139
- skipDiagnostics?: boolean
140
-
141
- // 是否打印类型诊断信息
142
- // 当跳过类型诊断时该属性将不会生效
143
- // 默认值: false
144
- logDiagnostics?: boolean
145
-
146
- // 获取诊断信息后的钩子
147
- // 可以根据参数 length 来判断有误类型错误
148
- // 默认值: () => {}
149
- afterDiagnostic?: (diagnostics: Diagnostic[]) => void | Promise<void>
150
-
151
- // 类型声明文件被写入前的钩子
152
- // 可以在钩子里转换文件路径和文件内容
153
- // 默认值: () => {}
154
- beforeWriteFile?: (filePath: string, content: string) => void | TransformWriteFile
155
-
156
- // 构建后回调钩子
157
- // 将会在所有类型文件被写入后调用
158
- // 默认值: () => {}
159
- afterBuild?: () => void | Promise<void>
160
- }
161
- ```
162
-
163
- ## 示例
164
-
165
- 克隆项目然后执行下列命令:
166
-
167
- ```sh
168
- pnpm run test:e2e
169
- ```
170
-
171
- 然后检查 `example/types` 目录。
172
-
173
- ## 常见问题
174
-
175
- 此处将收录一些常见的问题并提供一些解决方案。
176
-
177
- ### 打包后出现类型文件缺失
178
-
179
- 默认情况下 `skipDiagnostics` 选项的值为 `true`,这意味着打包过程中将跳过类型检查(一些项目通常有 `vue-tsc` 等的类型检查工具),这时如果出现存在类型错误的文件,并且这是错误会中断打包过程,那么这些文件对应的类型文件将不会被生成。
180
-
181
- 如果您的项目没有依赖外部的类型检查工具,这时候可以您可以设置 `skipDiagnostics: false` 和 `logDiagnostics: true` 来打开插件的诊断与输出功能,这将帮助您检查打包过程中出现的类型错误并将错误信息输出至终端。
182
-
183
- ### Vue 组件中同时使用了 `script` 和 `setup-script` 后出现类型错误
184
-
185
- 这通常是由于分别在 `script` 和 `setup-script` 中同时使用了 `defineComponent` 方法导致的。 `vue/compiler-sfc` 为这类文件编译时会将 `script` 中的默认导出结果合并到 `setup-script` 的 `defineComponent` 的参数定义中,而 `defineComponent` 的参数类型与结果类型并不兼容,这一行为将会导致类型错误。
186
-
187
- 这是一个简单的[示例](https://github.com/qmhc/vite-plugin-dts/blob/main/example/components/BothScripts.vue),您应该将位于 `script` 中的 `defineComponent` 方法移除,直接导出一个原始的对象。
188
-
189
- ## 授权
190
-
191
- MIT 授权。
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.js CHANGED
@@ -438,6 +438,7 @@ function dtsPlugin(options = {}) {
438
438
  const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
439
439
  let root;
440
440
  let entryRoot = (_b = options.entryRoot) != null ? _b : "";
441
+ let libName;
441
442
  let indexName;
442
443
  let aliases;
443
444
  let entries;
@@ -481,9 +482,11 @@ function dtsPlugin(options = {}) {
481
482
  logger.warn(import_chalk.default.yellow(`
482
483
  ${import_chalk.default.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
483
484
  `));
485
+ libName = "_default";
484
486
  indexName = defaultIndex;
485
487
  } else {
486
488
  const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
489
+ libName = config.build.lib.name || "_default";
487
490
  indexName = typeof filename === "string" ? filename : filename("es");
488
491
  if (!dtsRE2.test(indexName)) {
489
492
  indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
@@ -647,11 +650,20 @@ ${logPrefix} Start generate declaration files...`));
647
650
  let typesPath = types ? (0, import_path4.resolve)(root, types) : (0, import_path4.resolve)(outputDir, indexName);
648
651
  if (!import_fs_extra.default.existsSync(typesPath)) {
649
652
  const entry = entries[0];
650
- 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))));
651
- filePath = filePath.replace(tsRE, "");
653
+ const outputIndex = (0, import_path4.resolve)(outputDir, (0, import_path4.relative)(entryRoot, entry.replace(tsRE, ".d.ts")));
654
+ let filePath = (0, import_vite2.normalizePath)((0, import_path4.relative)((0, import_path4.dirname)(typesPath), outputIndex));
655
+ filePath = filePath.replace(dtsRE2, "");
652
656
  filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
653
657
  let content = `export * from '${filePath}'
654
658
  `;
659
+ if (import_fs_extra.default.existsSync(outputIndex)) {
660
+ const entryCodes = await import_fs_extra.default.readFile(outputIndex, "utf-8");
661
+ if (entryCodes.includes("export default")) {
662
+ content += `import ${libName} from '${filePath}'
663
+ export default ${libName}
664
+ `;
665
+ }
666
+ }
655
667
  if (typeof beforeWriteFile === "function") {
656
668
  const result = beforeWriteFile(typesPath, content);
657
669
  if (result && isNativeObj(result)) {
@@ -660,6 +672,7 @@ ${logPrefix} Start generate declaration files...`));
660
672
  }
661
673
  }
662
674
  await import_fs_extra.default.writeFile(typesPath, content, "utf-8");
675
+ wroteFiles.add((0, import_vite2.normalizePath)(typesPath));
663
676
  }
664
677
  bundleDebug("insert index");
665
678
  if (rollupTypes) {
package/dist/index.mjs CHANGED
@@ -420,6 +420,7 @@ function dtsPlugin(options = {}) {
420
420
  const compilerOptions = (_a = options.compilerOptions) != null ? _a : {};
421
421
  let root;
422
422
  let entryRoot = (_b = options.entryRoot) != null ? _b : "";
423
+ let libName;
423
424
  let indexName;
424
425
  let aliases;
425
426
  let entries;
@@ -463,9 +464,11 @@ function dtsPlugin(options = {}) {
463
464
  logger.warn(chalk.yellow(`
464
465
  ${chalk.cyan("[vite:dts]")} You building not a library that may not need to generate declaration files.
465
466
  `));
467
+ libName = "_default";
466
468
  indexName = defaultIndex;
467
469
  } else {
468
470
  const filename = (_a2 = config.build.lib.fileName) != null ? _a2 : defaultIndex;
471
+ libName = config.build.lib.name || "_default";
469
472
  indexName = typeof filename === "string" ? filename : filename("es");
470
473
  if (!dtsRE2.test(indexName)) {
471
474
  indexName = `${tjsRE.test(indexName) ? indexName.replace(tjsRE, "") : indexName}.d.ts`;
@@ -629,11 +632,20 @@ ${logPrefix} Start generate declaration files...`));
629
632
  let typesPath = types ? resolve3(root, types) : resolve3(outputDir, indexName);
630
633
  if (!fs.existsSync(typesPath)) {
631
634
  const entry = entries[0];
632
- let filePath = normalizePath2(relative2(dirname3(typesPath), resolve3(outputDir, relative2(entryRoot, entry))));
633
- filePath = filePath.replace(tsRE, "");
635
+ const outputIndex = resolve3(outputDir, relative2(entryRoot, entry.replace(tsRE, ".d.ts")));
636
+ let filePath = normalizePath2(relative2(dirname3(typesPath), outputIndex));
637
+ filePath = filePath.replace(dtsRE2, "");
634
638
  filePath = fullRelativeRE.test(filePath) ? filePath : `./${filePath}`;
635
639
  let content = `export * from '${filePath}'
636
640
  `;
641
+ if (fs.existsSync(outputIndex)) {
642
+ const entryCodes = await fs.readFile(outputIndex, "utf-8");
643
+ if (entryCodes.includes("export default")) {
644
+ content += `import ${libName} from '${filePath}'
645
+ export default ${libName}
646
+ `;
647
+ }
648
+ }
637
649
  if (typeof beforeWriteFile === "function") {
638
650
  const result = beforeWriteFile(typesPath, content);
639
651
  if (result && isNativeObj(result)) {
@@ -642,6 +654,7 @@ ${logPrefix} Start generate declaration files...`));
642
654
  }
643
655
  }
644
656
  await fs.writeFile(typesPath, content, "utf-8");
657
+ wroteFiles.add(normalizePath2(typesPath));
645
658
  }
646
659
  bundleDebug("insert index");
647
660
  if (rollupTypes) {
package/package.json CHANGED
@@ -93,5 +93,5 @@
93
93
  "test:e2e": "cd example && cross-env DEBUG=\"vite-plugin-dts:bundle\" vite build"
94
94
  },
95
95
  "types": "dist/index.d.ts",
96
- "version": "1.3.0"
96
+ "version": "1.3.1"
97
97
  }