简体   繁体   English

如何使用根 package.json 依赖项发布纱线工作区?

[英]How to publish yarn workspace with root package.json dependencies?

I'm trying to publish one (yarn) workspace from a monorepo to npm repository, and include root project's dependencies in the published package.我正在尝试将一个(纱线)工作区从 monorepo 发布到 npm 存储库,并在已发布的 package 中包含根项目的依赖项。 The setup is as follows:设置如下:

  • package.json (contains shared dependencies, eg. single version of React to be used by all workspaces) package.json (包含共享依赖项,例如所有工作区都使用的单一版本的 React)
  • library/ (this is the one I want to publish, for consumption outside of this monorepo, the published package.json should contain the root dependencies as well as library local) library/ (这是我要发布的,为了在这个 monorepo 之外使用,已发布的 package.json 应该包含根依赖项以及库本地)
  • library/package.json (contains library specific dependencies) library/package.json (包含特定于库的依赖项)
  • app1/ (application that uses library ) app1/ (使用的应用程序)
  • app1/package.json (contains app local dependencies, and depends on library ) app1/package.json (包含应用本地依赖,依赖
  • app2/ (application that uses library ) app2/ (使用的应用程序)
  • app2/package.json (contains app local dependencies, and depends on library ) app2/package.json (包含应用本地依赖,依赖

So what I want to do, is to cd library and yarn publish .所以我想做的是cd libraryyarn publish What I expect to happen is that the published package will work exactly the same way it works for my monorepo local apps.我期望发生的是,已发布的 package 的工作方式与我的 monorepo 本地应用程序的工作方式完全相同。 The problem is that yarn doesn't merge-in the dependencies from the monorepo's root package.json , and the published package's package.json only contains library local dependencies, from library/package.json . The problem is that yarn doesn't merge-in the dependencies from the monorepo's root package.json , and the published package's package.json only contains library local dependencies, from library/package.json . So when anyone installs this published library package, it will be broken because it imports modules from packages (dependencies) not listed in library/package.json .因此,当任何人安装此已发布package 时,它将被破坏,因为它从library/package.json中未列出的包(依赖项)导入模块。

Do I really need to write a custom publishing shell script that merges the root package.json 's dependencies into library/package.json before running yarn publish ? Do I really need to write a custom publishing shell script that merges the root package.json 's dependencies into library/package.json before running yarn publish ?

The only type of dependencies that you can reliably put in a workspace root are dev dependencies.您可以可靠地放入工作区根目录的唯一依赖类型是开发依赖。

Each package should declare it's dependencies in it's package.json file respectively.每个 package 应分别在其 package.json 文件中声明其依赖项。

What yarn does during installation is that it now looks at all the dependencies in all your workspace packages to create a single lock file and it won't put redundant dependencies in the package node_modules folder. yarn 在安装过程中所做的是,它现在查看所有工作空间包中的所有依赖项以创建单个锁定文件,并且不会将多余的依赖项放入 package node_modules 文件夹中。

How you publish your workspace is up to you.如何发布工作空间取决于您。 I typically write a small Node.js script to run the publish command line for each package.我通常会编写一个小的 Node.js 脚本来为每个 package 运行发布命令行。 Depending on what you put in your workspace you might want to version and/or publish your packages independently, this will give you that flexibility.根据您在工作区中放置的内容,您可能希望独立地对包进行版本控制和/或发布,这将为您提供灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM