A utility to help package.json in a monorepo in sync with an "inheritance" model
Перейти к файлу
Kenneth Chau bdee81a662
Merge pull request #3 from microsoft/jagore/check-update-symmetry
Make sure inherit check is symmetric with update behavior.
2021-01-22 13:03:18 -08:00
dist Fix typo. 2021-01-22 12:59:42 -08:00
src Fix typo. 2021-01-22 12:59:42 -08:00
.gitignore adding generated output 2020-09-01 14:47:12 -07:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2020-09-01 14:23:20 -07:00
LICENSE Initial LICENSE commit 2020-09-01 14:23:21 -07:00
README.md typo 2020-09-02 10:24:06 -07:00
SECURITY.md Initial SECURITY.md commit 2020-09-01 14:23:23 -07:00
package.json fixed workspace-tools 2020-09-10 16:45:52 -07:00
rollup.config.js changed banner to be more serious in the warning 2020-09-02 08:50:08 -07:00
tsconfig.json initial commit 2020-09-01 14:25:39 -07:00
yarn.lock fixed workspace-tools 2020-09-10 16:45:52 -07:00

README.md

Monorepo package.json inheritance

The problem

In a monorepo, most of the time you would want to have your packages have their own package.json. Most of these repos have a central build script that help build, test, and bundle those packages. Further, these central build scripts usually are themselves packages. The issue here is that if you placed the devDependencies inside a central build package, all those build tools (and their dependencies) are "phantom dependencies". Generally you have to update the configurations of each build tools to accommodate the node resolutions (e.g. webpack loaders, plugins).

The solution

To support a better workflow, we introduce the idea of package "inheritance". It is by no means true inheritance, but it helps repo admins to centrally manage a set of devDependencies, dependencies in a central location while avoiding the issue of phantom dependencies.

The central build script package can provide a set of shared partial package.json files. The monorepo packages can then declare that they inherit from those package.json partial files. This tool can then be used to make sure the package.json's actually respect those partial files.

Install the tool

It is recommended to "install" this tool by just copying the dist/package-inherit-cli.js somewhere in your repo. This is because this is a tool that manipulates the package.json files and will affect the installation itself. You'll probably want this tool to run on preinstall step of the npm lifecycle (at the root):

{
  "scripts": {
    "preinstall": "./package-inherit-cli.js update"
  }
}

Using the tool

The nature of this tool is that it should be run in one of two ways:

  1. as an updater
  2. as a validator

Updating package.json

  1. Create these files in this structure in a monorepo:
/
  packages/foo/package.json
  packages/build-tool/package.json
  packages/build-tool/package.webpack.json
  1. Create a partial package.webpack.json file:
{
  "devDependencies": {
    "webpack": "^4.10.0",
    "webpack-cli": "^3.1.0"
  }
}
  1. Modify the foo/package.json to inherit from the package.webpack.json:
{
  "name": "foo",
  "version": "0.1.0",
  "inherits": ["build-tool/package.webpack.json"]
}
  1. Run the package-inherit-cli.js via npm install
$ npm install

Checking package.json

It is highly recommended to FAIL at a PR build if the tool noticed inconsistencies. This will enforce the versions to be consistent by the inheritance declaration:

$ ./package-inherit-cli.js check

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.