Angular 库项目的路径别名不工作

时间:2021-02-10 09:42:44

标签: angular typescript tsconfig

我正在尝试使用 ui 元素实现一个 Angular 库。该库是一个 monorepo,然后像这样发布

@lib/core
@lib/i18n
@lib/ui

开发正在 Storybook 下完成。

这是我的结构。

projects
    lib
        core
        i18n
        ui
tsconfig.json

ui 目录中的组件在 core 和 i18n 目录中有依赖项。

父 tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": [
      "es2018",
      "dom"
    ],
      "paths": {
        "@lib/core": ["projects/lib/core/src/*"],
        "@lib/i18n": ["projects/lib/i18n"]
    }
  },
  "angularCompilerOptions": {
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}
  • i18n 目录不包含 src 文件夹。

i18n 目录中的文件已正确解析,我可以按 ctrl+单击它们(使用 phpstorm)移动到文件。

文件形式@lib/core 在编译过程中抛出这些错误

ERROR in /path/to/project/projects/lib/ui/src/lib/widgets/help/components/widget-button/cl-widget-button.component.ts
TS2307: Cannot find module '@lib/core' or its corresponding type declarations.

还有核心目录的结构

Also the structure of core directory

public-api.ts 是我的入口点,包括所有导出

// index.ts
export * from './public-api.ts';

我使用类型别名的原因是模拟生产,如导入,其中所有包都将通过 npm 单独安装。

如果我使用相对路径 (../../../../path/to/core),那么一切都会按原样构建和运行,但在生产中,这条路径当然不存在。

我尝试了什么:

  • rootDirs 打字稿选项没有任何运气。
  • "@lib/core/*": ["projects/lib/core/src/*"],在没有任何运气的路径中。
  • 将路径选项放在不同的 tsconfig 文件中,例如在 ui/tsconfig.json 中没有任何运气。事实上,在这种情况下,i18n 也停止了工作。

我开始相信我不能为这样的结构取别名。

我在这里遗漏了什么?

0 个答案:

没有答案