V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cl903254852
V2EX  ›  程序员

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

  •  
  •   cl903254852 · 2020-06-16 15:48:07 +08:00 · 1991 次点击
    这是一个创建于 1402 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2020-06-17 11:24:20 +08:00
    buhi
        1
    buhi  
       2020-06-16 16:40:43 +08:00   ❤️ 1
    是的 tsc 不会去转换路径, 需要你自己解决, 如果是 webpack, 就是用 resolve.modules 或者 resolve.alias, 如果是 nodejs, 就是用类似 https://github.com/dividab/tsconfig-paths 的东西
    dubenshu
        2
    dubenshu  
       2020-06-17 11:24:20 +08:00
    可以考虑使用 babel 来编译
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3427 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.