Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should filter vite virtual files when transform #50

Closed
chrisxvin opened this issue Jan 5, 2022 · 7 comments
Closed

Should filter vite virtual files when transform #50

chrisxvin opened this issue Jan 5, 2022 · 7 comments
Labels
enhancement New feature or request

Comments

@chrisxvin
Copy link

为某个使用了 devextreme-vue 的组件生成 dts 时报错,内容如下:

[vite:dts] Start generate declaration files...
transforming (12) node_modules/devextreme/esm/ui/data_grid/ui.data_grid.base.js[vite:dts] Declaration files built in 2987ms.

[vite:dts] The argument 'path' must be a string or Uint8Array without null bytes. Received '/path-to-develop/test/vite-plugin-dts-test/\x00commonjsHelpers.js'
file: commonjsHelpers.js
error during build:
TypeError [PLUGIN_ERROR]: The argument 'path' must be a string or Uint8Array without null bytes. Received '/path-to-develop/test/vite-plugin-dts-test/\x00commonjsHelpers.js'
    at Object.openSync (node:fs:577:10)
    at Object.readFileSync (node:fs:453:35)
    at NodeRuntimeFileSystem.readFileSync (/path-to-develop/test/vite-plugin-dts-test/node_modules/@ts-morph/common/dist/ts-morph-common.js:1191:30)
    at RealFileSystemHost.readFileSync (/path-to-develop/test/vite-plugin-dts-test/node_modules/@ts-morph/common/dist/ts-morph-common.js:1894:23)
    at TransactionalFileSystem.readFileSync (/path-to-develop/test/vite-plugin-dts-test/node_modules/@ts-morph/common/dist/ts-morph-common.js:2431:32)
    at TransactionalFileSystem.readFileIfExistsSync (/path-to-develop/test/vite-plugin-dts-test/node_modules/@ts-morph/common/dist/ts-morph-common.js:2420:25)
    at CompilerFactory.addOrGetSourceFileFromFilePath (/path-to-develop/test/vite-plugin-dts-test/node_modules/ts-morph/dist/ts-morph.js:19160:61)
    at DirectoryCoordinator.addSourceFileAtPathIfExists (/path-to-develop/test/vite-plugin-dts-test/node_modules/ts-morph/dist/ts-morph.js:18633:37)
    at DirectoryCoordinator.addSourceFileAtPath (/path-to-develop/test/vite-plugin-dts-test/node_modules/ts-morph/dist/ts-morph.js:18639:33)
    at Project.addSourceFileAtPath (/path-to-develop/test/vite-plugin-dts-test/node_modules/ts-morph/dist/ts-morph.js:20086:51)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

但其实 dts 已经生成出来了。
报错的根源不知道。我跟踪出来的是 \x00commonjsHelpers.js 这个"文件"存在于 vite 里,包含在某个 vite/dist/node/chunks/dep-xxx.js 中,仅仅是个声明:

const HELPERS_ID = '\0commonjsHelpers.js';

我修改了一下 dts 插件,简单过滤了一下,不添加这些 \0 开头的文件。

@chrisxvin
Copy link
Author

chrisxvin commented Jan 5, 2022

emmm....破电脑没法 push 代码,放个 patch 吧🤣

--- plugin.ts	2022-01-05 15:47:52.824843900 +0800
+++ plugin-mod.ts	2022-01-05 15:13:31.864190100 +0800
@@ -188,7 +188,7 @@
 
           project.createSourceFile(`${id}.${ext || 'js'}`, content, { overwrite: true })
         }
-      } else if (!id.includes('.vue?vue') && (tsRE.test(id) || (allowJs && jsRE.test(id)))) {
+      } else if (!id.includes('.vue?vue') && (tsRE.test(id) || (allowJs && jsRE.test(id))) && !id.startsWith('\0')) {
         project.addSourceFileAtPath(id)
       }
 
@@ -242,7 +242,7 @@
             dtsRE.test(file) ? file : `${tjsRE.test(file) ? file.replace(tjsRE, '') : file}.d.ts`
           )
 
-          if (dtsRE.test(file)) {
+          if (dtsRE.test(file) && !file.startsWith('\0')) {
             sourceDtsFiles.add(project.addSourceFileAtPath(file))
           }
         })

@qmhc
Copy link
Owner

qmhc commented Jan 9, 2022

请问你是在什么环境下出现了这个问题?

@chrisxvin
Copy link
Author

chrisxvin commented Jan 10, 2022

RHEL 7.3
node 16.13

vite 2.7.10
vite-plugin-dts 0.9.7
vue-tsc 0.30.2
vue 3.2.6
devextreme 21.2.4
devextreme-vue 21.2.4
typescript 4.5.4

主要就这些.

@qmhc
Copy link
Owner

qmhc commented Jan 10, 2022

我使用了相同的环境创建了一个简单的项目,无法复现你描述的问题,还是希望你能提供一下可复现的测试仓库。

或者你的项目中是否有一些不同于传统 vite+vue+ts 的项目的用法,如 const xxx = require('xxx')<script> 不指定 lang="ts"

@chrisxvin
Copy link
Author

好。我稍后整理一个发上来。

@chrisxvin
Copy link
Author

chrisxvin commented Jan 10, 2022

测试工程:vite-plugin-dts-test.zip
yarn && yarn build

我又实验了一下,vite.config.ts 里面,设置 build.rollupOptions.external 的时候,不同的方式会有不同的结果。
直接给 external 设置数组,会导致报错。如果写个函数处理一下,就不会。我在测试工程里面写出来了。
给 external 设置数组的时候,导致报错的是一个用 TypeScript class 写的组件(/libs/my-data-grid),不是 setup,也不是 defineComponent.

@luozhihua
Copy link

luozhihua commented Jan 10, 2022

同样的问题,我没有设置build.rollupOptions.external,应该不是跟 external 有关,而是因为项目中有 CommonJS 模块, 导致Vite 在源码中插入了 commonjsHelpers.js 虚拟模块以处理 CommonJS 代码, 而commonjsHelpers.js 实际上 不是 一个真实存在的文件,只是一个 Vite 所支持的虚拟模块:https://vitejs.dev/guide/api-plugin.html#importing-a-virtual-file

按照 @chrisxvin 的修改,过滤掉以\0为前缀的文件是可以解决问题的,Vite 的虚拟模块约定是就是以\0开头;

@qmhc qmhc added the enhancement New feature or request label Jan 10, 2022
@qmhc qmhc closed this as completed in 5fb24ba Jan 11, 2022
@qmhc qmhc changed the title 生成 .vue 的 dts 时报错 Should filter vite virtual files when transform Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants