0

我有一个使用纱线工作区和以下文件夹结构创建的 monorepo:

├── foo                   (workspace @project/foo)
│   ├── package.json
│   └── src
│       └── index.ts
├── bar                   (workspace @project/bar)
│   ├── package.json
│   └── src
│       └── index.ts
├── package.json          (monorepo root)
└── tsconfig.json         (base tsconfig)

以及以下设置tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "paths": {
      "@project/foo/*": "./packages/foo/src/*",
      "@project/bar/*": "./packages/bar/src/*"
    },
    ...
  }
}

@project/bar工作区中,我想从以下位置导入模块@project/foo

import foo from "@project/foo";

但我收到以下错误:

找不到模块“@project/foo”或其对应的类型声明。ts(2307)

如果我*从“路径”对象的键和值中删除符号,代码就会编译。这是为什么?如何保持*全局模式并对本地模块进行非相对导入?

4

1 回答 1

0

根应该单独定义,因为只有和才允许导入/块。@project/foo@project/foo/@project/foo/something

"@project/foo": "./packages/foo/src",
"@project/bar": "./packages/foo/bar",
"@project/foo/*": "./packages/foo/src/*",
"@project/bar/*": "./packages/bar/src/*"

在 monorepo 的情况下,您不能使用/*,因为编译后它将无法以与 monorepo 运行时相同的方式访问。

正确的方法是指向项目的public_api

"@project/foo": "./packages/foo/src/public_api",
"@project/bar": "./packages/foo/bar/public_api",

子项目也一样:

"@project/foo/sub": "./packages/foo/src/sub/public_api",
于 2020-06-03T08:15:44.503 回答