Defrag
De-fragment the dependencies your monorepo, reducing the number of copies of in-range dependencies
This will sync all the versions in your monorepo to the same version, within the configured range.
Works with all package-managers (pnpm, yarn, npm, etc)
Usage
Run in the root of your monorepo
npx defrag
or debug with
DEBUG=defrag npx defrag
Erroring in C.I.
In GitHub Actions
ensure-no-divergence:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npx defrag
- run: git diff --exit-code
No need to install dependencies for your monorepo to have this verification
Config
Example .defragrc.yaml
write-as: pinned
overrides:
- path:
- packages/*/addon/package.json
- packages/*/*/package.json
devDependencies: pinned
dependencies: minors
update-range:
"~":
- ember-source
- ember-data
- "@ember-data/*"
Questions
Disable for sub-folders?
overrides:
- path: packages/**/*
devDependencies: false
dependencies: false
What does this do?
The algorithm is this:
scan all workspaces' package.json,
find the dependencies, adding their versions to a list
for each workspace
for each dependency
re-assign an in-range version to the highest the monorepo was already using
- reduces lockfile size
- reduces duplicate depenedncies
- allows package managers that "hoist" dependencies to be likely more correct
How is this different from dedupe?
Dedupe operates on the lockfile, only and defrag
gives you more control over what dedupes, based on ranges in a configured .defragrc.yaml
.
additionally, this tool gives the ability to pin
versions, whereas dedupe would use whatever resolved dependency version satisfies the pre-existing ^
range.
Can this break my lockfile?
If a package.json using a version format that isn't actually a version (and not yet accounted for), this is likely a bug -- the desired behavior is to ignore invalid versions and opt them out of being changed by this tool.