Jur*_*son 27 eslint tsconfig nrwl
当我最近将 Ng 代码库移植到 Nx 12.x 时,我对这条规则感到非常困惑。我希望这篇文章可以帮助其他开始从 Ng 迁移到 Nx 的人。
上面的代码库是一个相当小的单个存储库,现在已在生产中使用。使用 Nx 时,最好遵循 monorepo 的建议,以便将来随着代码库的增长能够使用 monorepo 的优势。(例如,这里我避免过度暴露当前存储库中的代码)。
我将上面的代码库放入my-org/apps/my-small-repo
. 通过 linting,我对规则的失败感到困惑@nrwl/nx/enforce-module-boundaries
。因此,我尝试了不同的可能性来映射编译器或 linter 或两者都失败的地方src/app
。my-org/apps/my-small-repo
我想出了以下解决方案。
就放
"compilerOptions": {
"baseUrl": "src"
},
Run Code Online (Sandbox Code Playgroud)
进入 的根目录apps/my-small-repo/tsconfig.json
,并将 中的所有导入替换apps/my-small-repo
为以 开头的导入app
。
示例DashboardComponent
:
import { DashboardComponent } from 'app/components/dashboard/dashboard.component';
Run Code Online (Sandbox Code Playgroud)
该解决方案在 nx 13.x 上进行了测试,但它可能也适用于以前版本的 nx。
放
import { DashboardComponent } from 'app/components/dashboard/dashboard.component';
Run Code Online (Sandbox Code Playgroud)
到您的存储库根paths
目录中。然后将规则放入存储库根目录中。compilerOptions
tsconfig.base.json
"allowCircularSelfDependency": true,
@nrwl/nx/enforce-module-boundaries
我们决定"allowCircularSelfDependency": true,
避免使用丑陋的相对路径,例如../../../../../
应用程序中的这个路径。我们还希望仅包含库名称空间tsconfig.base.json
。
Nev*_*r K 10
对于那些在这个问题没有得到解决的情况下来到这里的人。(nx monorepo 用法)
解决 2 个错误(TS 错误和 lint 错误):
首先是别名错误:
Cannot find module '@account/components/something' or its corresponding type declarations.
"compilerOptions":{
...
baseUrl:"." // Try "src" as well incase of boiler plates or if your resolved path (on the error) is missing an src.
path: {
"@account/*": ["app/*"],
"@account/components/*": ["app/components/*"]
}
},
Run Code Online (Sandbox Code Playgroud)
以上将解决:
import { authMiddleware } from '@account/components/something';
从
import { authMiddleware } from '../../../components/something';
对于 lint 错误:
项目应使用相对导入从同一项目中的其他文件导入 - eslint 规则 @nrwl/nx/enforce-module-boundaries 失败`
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"allowCircularSelfDependency": true, -> This may solve the lint error.
"allow": ["@account/**"], -> // White list the lint error.
...
}
Run Code Online (Sandbox Code Playgroud)
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"allow": ["@account/**"], -> // White list the lint error.
...
}
Run Code Online (Sandbox Code Playgroud)
那应该解决它。
小智 0
tsconfig.base.json
或当地tsconfig.json
。我建议在tsconfig.base.json
考虑你的道路apps/my-org/src/app/*
"compilerOptions":{
...
baseUrl:"src"
path: {
"@app/*": ["app/*"] // << Here is the change
}
},
Run Code Online (Sandbox Code Playgroud)
apps/my-org/src/app/*
到这里@app/*
归档时间: |
|
查看次数: |
18608 次 |
最近记录: |