Typescript pnpm monorepo,构建期间本地依赖项之间的路径别名冲突

Nik*_*vic 15 typescript reactjs monorepo next.js pnpm

我有一个带有 TurbeRepo monorepo 的 PNPM,使用了这个模板。在这个 monorepo 中,我有一个使用ui-shared包(本地共享代码,实际上不是 npm 包)的 NextJS 应用程序。

\n

ui-shared按照类似的方式制作的,但我的包裹当然要大得多。index.ts使用导出所有组件和共享实用程序的a 。

\n

更新:添加了重现问题的最小存储库

\n

构建时ui-shared它成功构建,但是当我构建 Nextjs 应用程序时,ui-shared构建失败,因为它无法解析tsconfig. 因此,经过几天的侦探工作,我成功地找出了我认为的问题所在。当构建下一个应用程序时,它也会构建 ,ui-shared但会失败,因为现在的路径基于 nextjs 而不是库本身。我不确定这是原因,但我相信是这样,而且我不知道如何解决这个问题

\n

(我将指的是共享包或包ui-shared

\n

构建共享包成功

\n
ESM \xe2\x9a\xa1\xef\xb8\x8f Build success in 112ms\nESM dist/index.mjs 220.69 KB\nDTS Build start\nDTS \xe2\x9a\xa1\xef\xb8\x8f Build success in 4792ms\n
Run Code Online (Sandbox Code Playgroud)\n

package.json 的部分内容

\n
  "main": "./src/index.ts",\n  "module": "./dist/index.mjs",\n  "types": "./src/index.ts",\n  "scripts": {\n    "lint": "eslint *.ts*",\n    "build": "tsup ./src/index.ts --format esm,cjs --dts --external react",\n    "dev": "tsup ./src/index.ts --format esm,cjs --watch --dts --external react"\n  },\n
Run Code Online (Sandbox Code Playgroud)\n

包的 tsconfig

\n
{\n  "extends": "tsconfig/react-library.json",\n  "compilerOptions": {\n    "baseUrl": ".",\n    "resolveJsonModule": true,\n    "allowJs": true,\n    "paths": {\n      "@/*": ["src/*"]\n    }\n  },\n  "include": ["**/*.ts", "**/*.tsx"],\n  "exclude": ["dist", "build", "node_modules"]\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

当我运行 NextJs 应用程序的构建时,出现此错误

\n
pnpm build\n\n> @company/business-frontend@1.0.0 build /work/company/monorepo/apps/business\n> next build\n\nSee more info here: https://nextjs.org/docs/messages/invalid-next-config\ninfo  - Skipping linting\ninfo  - Checking validity of types .Failed to compile.\n\n../../packages/ui-shared/src/components/AnimatedLoadingMedia.tsx:9:23\nType error: Cannot find module \'@/utils/clsxm\' or its corresponding type declarations.\n\n   7 | } from "react";\n   8 | \n>  9 | import { clsxm } from "@/utils/clsxm";\n     |                       ^\n  10 | \n  11 | type AnimatedLoadingMediaProps = {\n  12 |   isVideo: boolean;\n\n> Build error occurred\nError: Call retries were exceeded\n    at ChildProcessWorker.initialize (/work/company/monorepo/node_modules/.pnpm/next@12.2.4_twoewwu6sg7cdf3ao6njtbftne/node_modules/next/dist/compiled/jest-worker/index.js:1:11661)\n    at ChildProcessWorker._onExit (/work/company/monorepo/node_modules/.pnpm/next@12.2.4_twoewwu6sg7cdf3ao6njtbftne/node_modules/next/dist/compiled/jest-worker/index.js:1:12599)\n    at ChildProcess.emit (node:events:390:28)\n    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12) {\n  type: \'WorkerError\'\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Nextjs 应用程序tsconfig

\n
{\n  "extends": "tsconfig/nextjs.json",\n  "compilerOptions": {\n    "lib": [\n      "dom",\n      "dom.iterable",\n      "esnext"\n    ],\n    "allowJs": true,\n    "baseUrl": ".",\n    "module": "esnext",\n    "paths": {\n      "@/*": ["src/*"],\n      "@": ["src"],\n      "@public/*": ["public/*"],\n      "@public": ["public"]\n    },\n  },\n  "include": [\n    "next-env.d.ts",\n    "**/*.ts",\n    "**/*.tsx"\n  ],\n  "exclude": [\n    "node_modules"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

不知何故,该库在从 nextjs 构建时必须解析自己的路径。或者更好的是,库应该在自己的范围内构建,而 nextjs 应该只使用输出。我不知道它是否可以这样工作

\n

我尝试添加第三方库,但喜欢tsc-alias tsconfig-path等等。但构建一直失败。我试图查看并以某种方式阻止 nextjs 构建软件包,但我找不到方法

\n