V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Lostbit
V2EX  ›  程序员

tsconfig.json 中使用 paths 来映射文件,经过 tsc 编译后映射路径还是编译之前的?

  •  
  •   Lostbit · Jun 16, 2020 · 2762 views
    This topic created in 2142 days ago, the information mentioned may be changed or developed.

    tsconfig.json 内 paths 的配置如下:

    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "baseUrl": ".",
        "paths": {
          "services/*": ["app/services/*"]
        }
      }
    

    编译前:

    编译后:

    这样会报错:can not fond module xxx

    尝试使用了module-alias@momothepug/tsmodule-alias等插件都无效,原因是因为使用了 import x from 'x' 语法。 但我仍然想用 import 语法来引用模块。

    有大佬遇到过这种问题吗? 如何解决?😞😞😞

    2 replies    2020-06-17 11:24:20 +08:00
    buhi
        1
    buhi  
       Jun 16, 2020   ❤️ 1
    是的 tsc 不会去转换路径, 需要你自己解决, 如果是 webpack, 就是用 resolve.modules 或者 resolve.alias, 如果是 nodejs, 就是用类似 https://github.com/dividab/tsconfig-paths 的东西
    dubenshu
        2
    dubenshu  
       Jun 17, 2020
    可以考虑使用 babel 来编译
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1189 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:12 · PVG 07:12 · LAX 16:12 · JFK 19:12
    ♥ Do have faith in what you're doing.