【问题标题】:How to properly set typescript in monorepo so it compiles private packages?如何在 monorepo 中正确设置打字稿以便编译私有包?
【发布时间】:2023-02-06 10:33:42
【问题描述】:

目前我已经设置了一个带有 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'

任何人都可以指出我的回购协议有什么问题吗?

在导入 nestjs 应用程序之前,我是否需要先构建数据库包?如果是这样,客户如何在不先构建的情况下工作?

这是我在server/core/tsconfig.json 中用于后端的 tsconfig:

{
  "extends": "tsconfig/server.json",
  "compilerOptions": {
    "outDir": "./dist",
    "baseUrl": "./",
  },
}

这是我在 apps/web/tsconfig.json 中用于前端的 tsconfig(工作正常):

{
  "extends": "tsconfig/nextjs.json",
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

和扩展,

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
  }
}

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"]
}

My repo is open source here

【问题讨论】:

    标签: reactjs typescript next.js nestjs turborepo


    【解决方案1】:

    我只是想出了一个普通的节点应用程序。我通过将这些包中的每一个构建到包本地 dist 目录中来使其工作。例如:

    // packages/mypackage/package.json
    {
      "name": "@myrepo/mypackage",
      "version": "0.0.0",
      "private": true,
      "main": "./dist/index.js",
      "source": "./src/index.ts",
      "types": "./dist/index.d.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": "*"
      }
    }
    
    // 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"]
    }
    

    虽然 Turborepo 文档表明这对于内部包来说不是必需的,但我发现了多个人们这样做以使事情正常进行的例子。我opened a discussion 询问文档是否不正确。

    【讨论】:

      猜你喜欢
      • 2019-04-23
      • 2020-06-06
      • 2022-11-15
      • 2017-12-16
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多