打字稿不替换 tsconfig 中定义的非相对路径

Gar*_*med 7 javascript node.js typescript

我试图使用自定义路径来简化常用模块的导入,我设置了这个配置:

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "outDir": "build",
        "allowJs": true,
        "target": "es5",
        "sourceMap": true,
        "baseUrl": ".",
        "paths": {
            "config": ["app/config"]
        }

    },
    "exclude": [
        "node_modules”,
        "build"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用“config”导入配置模块,但应用程序无法要求配置文件。编译文件中的 require 路径仍然是“config”。

// result:
var config = require("config");
// what is should be:
var config = require("../../config");
Run Code Online (Sandbox Code Playgroud)

甚至认为模块解析日志显示已解决。

======== Resolving module 'config' from '/abs/path/routes/internal/signin/index.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
'baseUrl' option is set to '/abs/path', using this value to resolve non-relative module name 'config'
'paths' option is specified, looking for a pattern to match module name 'config'.
Module name 'config', matched pattern 'config'.
Trying substitution 'config', candidate module location: 'config'.
Loading module as file / folder, candidate module location '/abs/path/config', target file type 'TypeScript'.
File '/abs/path/config.ts' does not exist.
File '/abs/path/config.tsx' does not exist.
File '/abs/path/config.d.ts' does not exist.
File '/abs/path/config/package.json' does not exist.
File '/abs/path/config/index.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to '/abs/path/config/index.ts'. ========
Run Code Online (Sandbox Code Playgroud)

在编译后更改路径以指向正确的模块时,我缺少什么?

lap*_*tou 3

显然paths从未打算实际将 url 解析为其相对版本。您应该使用某种后处理器来完成此操作。我正在使用这个 Babel 插件。

  • 我认为这非常愚蠢,但是你能做什么呢? (6认同)