Skip to content

[Bug Report] .vue + ts 编写组件模式,编译错误 #12912

Open

Description

@beliefgp

重现链接

https://gitee.com/cyxwmm/vant-cli-test

Vant 版本

7.0.0

描述一下你遇到的问题。

在以 ts + .vue 模式开发组件时,就会出现 ts 类型编译问题,主要是 cli 的执行顺序导致。

比如组件含有连个文件 input.vue 和入口文件 index.ts:

import Input From './input.vue';

export { Input }

按现在 cli 的逻辑:
先执行 TS 声明编译:代码
这里会先 .vue 文件的预编译:代码

那么现在项目文件变成了:input.ts 和 index.ts。
此时 index.ts 里的内容未做任何改变,再执行 tsc 时,import Input From './input.vue'; 这句代码将无法编译,因为 input.vue 已经被改成了 index.ts。

虽然后续编译 ts 文件时有对 .vue 等文件后缀处理 代码,但时机已经晚了。

这里可能需要调整处理后缀逻辑的运行时机。

重现步骤

vant-cli build

设备/浏览器

No response

Activity

  1. github-actions commented on Jun 16, 2024

    @github-actions

    Hello @beliefgp. We totally like your proposal/feedback, welcome to send us a Pull Request for it. Please send your Pull Request to main branch, provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. We appreciate your effort in advance and looking forward to your contribution!

    你好 @beliefgp,我们完全同意你的提议/反馈,欢迎直接在此仓库 创建一个 Pull Request 来解决这个问题。请将 Pull Request 发到 main 分支,提供改动所需相应的 changelog、TypeScript 定义、测试用例、文档等,并确保 CI 通过,我们会尽快进行 Review,提前感谢和期待你的贡献。

  2. added a commit that references this issue on Oct 16, 2026
    44de0cf
  3. gril520 commented on Oct 16, 2026

    @gril520

    你好 #13942 该提交对编译时机进行了修改 ,具体如下:

    问题
    Closes #12912

    在 ts + .vue 模式下开发组件时,vant-cli build 会因为执行顺序问题导致 TS 编译失败。

    原因分析
    buildTypeDeclarations() 的执行流程:
    preCompileDir() → .vue 编译为 .ts/.jsfixVueImportExtensions() → 修复脚本文件中 .vue 的 import 后缀(新增)
    tsc → 正确解析 import 路径

    fixVueImportExtensions() 会递归扫描目录中的脚本文件,将引号内的 .vue 路径替换为实际编译后的扩展名(.ts 或 .js),确保 tsc 能正确解析模块。

    改动文件
    packages/vant-cli/src/commands/build.ts - 新增 fixVueImportExtensions() 函数 - 在 buildTypeDeclarations() 中调用,位于 preCompileDir 与 tsc 之间

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions