fix: ensure rollupTypes
work if vite emptyOutDir
is false
#291
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题描述
当 vite 配置为
emptyOutDir: false
(即构建前不清空 dist 目录)。vite-plugin-dts 开启rollupTypes
模式可能会生成错误的 d.ts 内容。代码修改
在生成
my-lib.d.ts
前,清除 dist 目录下的相关 d.ts 文件。问题复现
当 vite 配置
emptyOutDir
为false
时,执行一次 build 后,第二次 build 前不会清空目录。步骤1. vite-plugin-dts 在 dist 目录生成包含
export * from '${fromPath}'
的my-lib.d.ts
,代码逻辑目前是:文件如果已存在,
my-lib.d.ts
不会再被写入vite-plugin-dts/src/plugin.ts
Lines 603 to 626 in 9262d01
步骤2. 当
rollupTypes
为true
时,会继续接下来的步骤 —— 使用@microsoft/api-extractor
提取出 rollupTypes。但由于
my-lib.d.ts
是上次 build 已经生成的文件(并且没有被 步骤1 写入预期内容),上次构建的my-lib.d.ts
会作为输入值,继续传给@microsoft/api-extractor
去解析,导致最后得到错误的 rollupTypes 结果。vite-plugin-dts/src/plugin.ts
Lines 667 to 677 in 9262d01
复现代码
https://github.com/qwqcode/vite-dts-test
其他可能的修改
在构建时仅对 dist 目录进行写的操作(不再多次对 dist 进行读写的 IO 操作)。
如果
rollupTypes
开启,目前dist/**
会被@microsoft/api-extractor
读取,然后写入,以及删除dist/src
目录。可以创建一个临时目录,例如
node_modules/.vite
,然后@microsoft/api-extractor
不再去读dist
目录,而是读这个临时目录里面的 ts 文件。然后构建的文件拷贝到 dist 里面一步到位