我正在阅读tsconfig中的路径映射。json,我想用它来避免使用以下丑陋的路径:

项目组织有点奇怪,因为我们有一个包含项目和库的单一存储库。项目按公司和浏览器/服务器/通用进行分组。

如何配置tsconfig中的路径?Json,而不是:

import { Something } from "../../../../../lib/src/[browser/server/universal]/...";

我可以使用:

import { Something } from "lib/src/[browser/server/universal]/...";

webpack配置中还需要其他东西吗?或者是tsconfig。json足够了吗?


当前回答

您可以通过使用子路径模式仅使用Node来实现这一点。

例如,将此添加到package.json…

{
    "imports": {
        "#lib": "./build/path/to/lib",
        "#lib/*": "./build/path/to/lib/*",
    }
}

...会让你像这样导入,避免相对路径。

import { something } from "#lib"

注意,它们必须以散列开头,并且在package中。json,它们必须指向你的构建,这样Node才能识别它。

正如其他人所说,您可以在tsconfig中添加这样的内容。json for Typescript:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "#lib": ["./src/path/to/lib"],
            "#lib/*": ["./src/path/to/lib/*"],
        },
    },
}

其他回答

如果typescript + webpack 2 + at-loader正在使用,有一个额外的步骤(@mleko的解决方案只部分适用于我):

// tsconfig.json
{
  "compilerOptions": {
    ...
    "rootDir": ".",
    "paths": {
      "lib/*": [
        "src/org/global/lib/*"
      ]
    }
  }
}    

// webpack.config.js
const { TsConfigPathsPlugin } = require('awesome-typescript-loader');

resolve: {
    plugins: [
        new TsConfigPathsPlugin(/* { tsconfig, compiler } */)
    ]
}

TypeScript 2.0中的高级路径解析

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

我不确定我们是否必须定义路径。但是在将baseUrl写入src之后 我可以像这样导入SRC文件夹下的所有文件夹

import { Home } from "pages";
import { formatDate } from "utils";
import { Navbar } from "components";

不要忘记在修改tsconfig.json后重新启动您的终端

这可以在您的tsconfig上设置。json文件,因为它是一个TS特性。

你可以这样做:

"compilerOptions": {
        "baseUrl": "src", // This must be specified if "paths" is.
         ...
        "paths": {
            "@app/*": ["app/*"],
            "@config/*": ["app/_config/*"],
            "@environment/*": ["environments/*"],
            "@shared/*": ["app/_shared/*"],
            "@helpers/*": ["helpers/*"]
        },
        ...

请记住,你想要引用的路径,它以你的baseUrl作为你所指向的路由的基础,这是强制性的,如文档所述。

字符“@”不是强制性的。

设置好之后,你可以像这样轻松地使用它:

import { Yo } from '@config/index';

你可能会注意到的唯一一件事是,智能感知在当前的最新版本中不起作用,所以我建议在导入/导出文件时遵循索引约定。

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

确保你有 ` { “compileOnSave”:没错, " compilerOptions ": { "路径":{ “@styles /”:(“资产/ scss /”) } }, }

` 这给我带来了麻烦。

2021年解决方案。

注意:CRA。最初,使用第三方库或为alias弹出应用程序的想法对我来说似乎很疯狂。然而,经过8个小时的搜索(并尝试了带有eject的变体),结果发现这个选项是最不痛苦的。

步骤1。

yarn add --dev react-app-rewired react-app-rewire-alias

步骤2。 在你的项目根目录下创建config-override .js文件,并填充如下内容:

const {alias} = require('react-app-rewire-alias')

module.exports = function override(config) {
  return alias({
    assets: './src/assets',
    '@components': './src/components',
  })(config)
}

步骤3。修复你的包裹。json文件:

  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
    "eject": "react-scripts eject"
}

如果@declarations不起作用,将它们添加到d.ts文件中。 例如: “@constants”:”。/src/constants', =>在react-app-env.d中添加Ts声明模块@constants;

仅此而已。现在你可以像往常一样继续使用yarn或npm start/build/test命令。

完整版本的文档。

注意:文档中的“使用ts / js配置”部分对我不起作用。在构建项目时仍然存在“不支持别名导入”的错误。所以我用了一个更简单的方法。幸运的是,它起作用了。