如何在 monorepo 中正确设置 typescript 以便编译私有包?

Rob*_*iam 1 typescript reactjs next.js nestjs turborepo

目前我已经设置了一个带有turborepo的monorepo,其中Nestjs作为BE,Nextjs作为FE。

我想重用 prisma 定义,所以很自然地将它分成自己的包并实现自己的 tsconfig。在我的数据库包的索引(棱镜所在的位置),我有这个简单的代码:

export * from "@prisma/client";

我的后端和前端现在都具有相同的依赖项: backend -> databasefrontend -> database

我的 FE 编译得很好,我可以使用我的 prisma 中的定义,但是 NestJS 应用程序没有在数据库包中编译 TS 并且有这个错误,我认为它与 tsconfig 有关,似乎 NestJS (我的后端)确实如此不想编译私有包依赖项,因此它无法识别“导出”。

core:dev: export * from "@prisma/client";
core:dev: ^^^^^^
core:dev: 
core:dev: SyntaxError: Unexpected token 'export'
Run Code Online (Sandbox Code Playgroud)

谁能指出我的回购协议有什么问题吗?

在导入 Nestjs 应用程序之前,我需要先构建数据库包吗?如果是这样,客户端为什么不先构建它就可以工作?

这是我的后端 tsconfig server/core/tsconfig.json

{
  "extends": "tsconfig/server.json",
  "compilerOptions": {
    "outDir": "./dist",
    "baseUrl": "./",
  },
}
Run Code Online (Sandbox Code Playgroud)

这是我的前端 tsconfig(工作正常)apps/web/tsconfig.json

{
  "extends": "tsconfig/nextjs.json",
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

和扩展,

tsconfig/server.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "extends": "./base.json",
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig/nextjs.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "Next.js",
  "extends": "./base.json",
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "incremental": true,
    "esModuleInterop": true,
    "module": "esnext",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["src", "next-env.d.ts"],
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

我的仓库在这里是开源的

Joe*_*app 5

更新:请参阅我的关于与 Nx 和 Turborepo 共享 TypeScript 的系列

问题是 TypeScript 没有被转译。Next.js 构建器会自动转换依赖包,这就是为什么 Next.js 应用程序不存在该问题的原因。所有其他应用程序都必须手动进行转译。

对于非 Next.js 应用程序所需的每个包,将包构建到包本地dist目录。例如:

// packages/mypackage/package.json
{
  "name": "@myrepo/mypackage",
  "version": "0.0.0",
  "private": true,
  "main": "dist/index.js",
  "source": "src/index.ts",
  "types": "src/index.ts",
  "files": [
    "dist/**"
  ],
  "scripts": {
    "build": "tsc",
    "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
    "dev": "tsc -w",
    "lint": "TIMING=1 eslint \"src/**/*.ts*\"",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-presets/node"
  },
  "devDependencies": {
    "eslint-config-custom": "*",
    "jest-presets": "*",
    "tsconfig": "*"
  }
}
Run Code Online (Sandbox Code Playgroud)
// packages/mypackage/tsconfig.json
{
  "extends": "tsconfig/base.json",
  "compilerOptions": {
    "lib": ["ES2019"],
    "module": "commonjs",
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "include": ["*.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["dist", "build", "node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

Vercel 的许多示例存储库(例如厨房水槽示例)的配置types如下package.json

"types": "dist/index.d.ts"

但这需要您在每次更改包后重建包,以便 VSCode 识别来自其​​他包的更改。要让 VSCode 立即针对非 Next.js 内部包进行类型检查,请使用以下命令:

"types": "src/index.ts"

注意:您可能必须types在可发布的存储库中使用前者。我还没试过。