首页 > 解决方案 > 如何将 monorepo 节点项目捆绑到单个文件中?尝试使用 ncc

问题描述

所以我有一棵树

├── package.json
├── tsconfig.json
└── packages
    ├── lib1
    │   ├── package.json
    │   ├── src
    │   │   ├── index.ts
    │   └── tsconfig.json
    ├── lib2
    │   ├── package.json
    │   ├── src
    │   │   ├── index.ts
    │   └── tsconfig.json
    ├── graph
    │   ├── package.json
    │   ├── src
    │   │   ├── index.ts
    │   └── tsconfig.json
    └── peer
        ├── package.json
        ├── src
        │   └── index.ts
        └── tsconfig.json

其中graph取决于lib2,它取决于lib1。

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "lib": ["es2018"],
    "moduleResolution": "node",
    "declaration": true,
    "strict": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "resolveJsonModule": true,
    "outDir": "build"
  },
  "exclude": ["**/node_modules", "**/build", "**/dist"]
}
{
  "extends": "../tsconfig-build.json",
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "build"
  }
}

当我使用它构建时,编译时一切都很好


  "scripts": {
    ":g:tsc": "cd $INIT_CWD && tsc --project tsconfig-build.json",

但是如果我尝试使用@vercel/ncc,我会得到编译时错误,比如'rootDir' is expected to contain all source files.

    "build": "ncc build src/index.ts",

我已经尝试在我的 中使用pathsand ,但它们都不适合我的目的,并且打字稿似乎没有正确查找不同的模块。当我将它指向对等体的 index.ts 时,它可以正常工作,但是没有工作空间依赖项。referencestsconfig.json

我的最终目标是能够将单个 js 文件发送到 docker 容器。我怎样才能达到我的目标?

标签: typescriptyarnpkgvercelyarn-workspacesyarnpkg-v2

解决方案


我们使用 Lerna 来管理我们的 Monorepo。

https://github.com/lerna/lerna

它允许拥有公共和私人包。公共包可以发布到注册表,并且任何依赖于来自同一个 Monorepo 的其他包的任何包都必须作为依赖项添加到 package.json 中。Lerna 可以管理依赖包中最终发生的任何更改的版本增量。

我们通过使用 GitHub 操作实现了自动部署,让 Lerna 将公共包发布到 GitHub 注册表并增加所有包,包括私有和提交到主分支的更改。

lerna.json

{
  "version": "independent",
  "packages": ["packages/*"],
  "npmClient": "yarn",
  "ignoreChanges": ["**/*.md"],
  "useWorkspaces": true,
  "command": {
    "version": {
      "conventionalCommits": true,
      "createRelease": "github",
      "exact": true,
      "message": "chore(release): publish",
      "preid": "next"
    },
    "publish": {
      "distTag": "latest",
      "preDistTag": "next",
      "registry": "https://npm.pkg.github.com/"
    }
  }
}

推荐阅读