Building private Typescript packages in a monorepo

huangapple go评论52阅读模式
英文:

Building private Typescript packages in a monorepo

问题

  1. 应该将它们各自转译为JavaScript,或者消费的包可以直接导入TypeScript吗?
  2. 应该如何配置它们的pnpm依赖项(dependenciespeerDependencies),以便消费的包不需要列出这些依赖项,但也防止多次复制?
  3. 应该使用什么模块格式用于在Node和浏览器构建中使用的私有包?
  4. 这些库是否需要使用esbuild/webpack等进行捆绑?

以上是您提出的问题的翻译。

英文:

How should private (not published) Typescript libraries in a monorepo be built so they can be shared amongst other libraries and apps (both Node and browser) in the repo? I'm using nx and pnpm to manage the monorepo.

  1. Should they each be transpiled to Javascript, or can the consuming package import the Typescript directly?
  2. How should their pnpm dependancies be configured (dependancies, peerDependancies) such that consuming package doesn't need to list these dependancies, but also prevents multiple copies?
  3. What module format should be used for private packages used in both Node and browser builds?
  4. Do the libraries need to be bundled with esbuild/webpack/etc?

I realize there are multiple questions, but they all come down to what the build step for the libraries should look like. I am currently trying just tsc and es2020 modules, but am having issues, such as Jest failing to import these libraries in a dependent library.

答案1

得分: 2

你在一步中构建所有的软件包。它们可以有不同的 tsconfig 构建配置,如果你更喜欢不同的代码风格。

你可以引用 TypeScript(因为你甚至不需要构建成 JavaScript)。你的导入可以使用软件包名称作为引用,例如 import {Foo} from '@CompanyName/fooLib',pnpm 工作区将会将它们链接到正确的软件包。

依赖项应该在 package.json 中引用,不需要指定版本:"@CompanyName/fooLib": "*",因为你只会使用共享库的最新版本,除非进行发布。

捆绑取决于你的框架、项目规模等因素。还有 Turbopack、Vite(带有和不带有 SWC),所以没有知道速度或 LTS 对你有多重要,无法回答这个问题。

英文:

You build all the packages together in one step. They can have different tsconfig build configurations though, if you prefer different code styles.

You reference the typescript (as you don't even need to build to JS). Your imports can use the package name as reference import {Foo} from '@CompanyName/fooLib' and pnpm workspaces will link them to the right package.

Dependencies should be referenced in package.json without version: "@CompanyName/fooLib": "*"as you will only use the most current version of shared libs, unless you do publishing.

The bundling depends on your framework, project size etc. There is also Turbopack, Vite (with and without SWC), so this can't be answered without knowing how much speed or LTS is important for you.

答案2

得分: 0

Sure, here are the translations for the provided content:

  1. 它们是否应该被转译成JavaScript,还是消费包可以直接导入TypeScript?

我正在使用tsc来构建库的.js.d.ts文件,放在dist/文件夹中。

  1. 应该如何配置它们的pnpm依赖关系(dependencies,peerDependencies),以便消费包无需列出这些依赖关系,同时又防止多个副本?

简单的dependencies似乎可以工作。

  1. 用于既用于Node又用于浏览器构建的私有包应该使用什么模块格式?

package.json中的type: "module"可以工作,前提是其他工具(Node、Jest等)配置为期望ESM。

  1. 这些库是否需要使用esbuild/webpack等进行捆绑?

不需要,消费包可以在不进行捆绑的情况下获取它们需要的文件。

英文:

> 1. Should they each be transpiled to Javascript, or can the consuming package import the Typescript directly?

I'm using tsc to build .js and .d.ts for the library in a dist/ folder.

> 2. How should their pnpm dependancies be configured (dependancies, peerDependancies) such that consuming package doesn't need to list these dependancies, but also prevents multiple copies?

Simple dependancies seem to be working.

> 3. What module format should be used for private packages used in both Node and browser builds?

type: "module" in package.json is working as long as other tools (Node, Jest, etc.) are configured to expect ESM.

> 4. Do the libraries need to be bundled with esbuild/webpack/etc?

No, the consuming packages can pull in the files they need without bundling.

huangapple
  • 本文由 发表于 2023年5月17日 23:10:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定