Giter Site home page Giter Site logo

package-inherit's Introduction

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 [email protected] with any additional questions or comments.

package-inherit's People

Contributors

jasongore avatar kenotron avatar microsoft-github-operations[bot] avatar microsoftopensource avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

package-inherit's Issues

Maybe better documentation

Thanks for this great package.
Yet after glancing at the readme, I am still wondering as to the results.

Maybe add a section that shows the resulting package.json will help.

Does not support `npm` - update `workspace-tools` to latest version

npm has support for workspaces since v7.24 (current: v9.8), but this tool only works with yarn, pnpm and rush.

Also it is not documented.

I had to debug the code to notice it cannot read workspace the packages from package-lock.json since it uses workspace-tools v0.9.8 which only supports those three package managers, while the latest version supports lerna and npn as well.

update should fail when conflicting versions are requested

Scenario:

  • Package a inherits from foo and bar.
  • foo requests webpack v1.
  • bar requests webpack v2.

Expected

package-inherit update prints "unresolvable conflict" and exits with status code != 0 and does not modify any package.json files.

Actual

  • package-inherit update completes successfully.
  • package-inherit check fails

throw new Error(`${file} does not exist`);

No matter how I configure this project, the only output I get when I run it is this:

D:\_Programming\aaronmeese.com\common\scripts\package-inherit-cli.js:36656
                    throw new Error(`${file} does not exist`);
                    ^

Error: undefined does not exist

Any more helpful documentation would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.