-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support peerDependencyRules for muting peer dep issues (#4212)
close #4183
- Loading branch information
Showing
9 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
"pnpm": minor | ||
"@pnpm/plugin-commands-installation": minor | ||
--- | ||
|
||
In order to mute some types of peer dependency warnings, a new section in `package.json` may be used for declaring peer dependency warning rules. For example, the next configuration will turn off any warnings about missing `babel-loader` peer dependency and about `@angular/common`, when the wanted version of `@angular/common` is not v13. | ||
|
||
```json | ||
{ | ||
"name": "foo", | ||
"version": "0.0.0", | ||
"pnpm": { | ||
"peerDependencyRules": { | ||
"ignoreMissing": ["babel-loader"], | ||
"allowedVersions": { | ||
"@angular/common": "13" | ||
} | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pnpm/core": minor | ||
--- | ||
|
||
New optional option supported: `peerDependencyRules`. This setting allows to mute specific peer dependency warnings. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@pnpm/types": minor | ||
--- | ||
|
||
New field added to package.json.pnpm section: peerDependencyRules. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { PeerDependencyRules, ReadPackageHook } from '@pnpm/types' | ||
import isEmpty from 'ramda/src/isEmpty' | ||
|
||
export default function ( | ||
peerDependencyRules: PeerDependencyRules | ||
): ReadPackageHook { | ||
const ignoreMissing = new Set(peerDependencyRules.ignoreMissing ?? []) | ||
return ((pkg) => { | ||
if (isEmpty(pkg.peerDependencies)) return pkg | ||
for (const [peerName, peerVersion] of Object.entries(pkg.peerDependencies ?? {})) { | ||
if (ignoreMissing.has(peerName) && !pkg.peerDependenciesMeta?.[peerName]?.optional) { | ||
pkg.peerDependenciesMeta = pkg.peerDependenciesMeta ?? {} | ||
pkg.peerDependenciesMeta[peerName] = { | ||
optional: true, | ||
} | ||
} | ||
if ( | ||
peerDependencyRules.allowedVersions?.[peerName] && | ||
peerVersion !== '*' | ||
) { | ||
pkg.peerDependencies![peerName] += ` || ${peerDependencyRules.allowedVersions[peerName]}` | ||
} | ||
} | ||
return pkg | ||
}) as ReadPackageHook | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/core/test/install/createPeerDependencyPatcher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import createPeerDependencyPatcher from '@pnpm/core/lib/install/createPeerDependencyPatcher' | ||
|
||
test('createPeerDependencyPatcher() ignores missing', () => { | ||
const patcher = createPeerDependencyPatcher({ | ||
ignoreMissing: ['foo'], | ||
}) | ||
const patchedPkg = patcher({ | ||
peerDependencies: { | ||
foo: '*', | ||
bar: '*', | ||
}, | ||
}) | ||
expect(patchedPkg['peerDependenciesMeta']).toStrictEqual({ | ||
foo: { | ||
optional: true, | ||
}, | ||
}) | ||
}) | ||
|
||
test('createPeerDependencyPatcher() extends peer ranges', () => { | ||
const patcher = createPeerDependencyPatcher({ | ||
allowedVersions: { | ||
foo: '1', | ||
qar: '1', | ||
}, | ||
}) | ||
const patchedPkg = patcher({ | ||
peerDependencies: { | ||
foo: '0', | ||
bar: '0', | ||
qar: '*', | ||
}, | ||
}) | ||
expect(patchedPkg['peerDependencies']).toStrictEqual({ | ||
foo: '0 || 1', | ||
bar: '0', | ||
qar: '*', | ||
}) | ||
}) |
15 changes: 13 additions & 2 deletions
15
packages/plugin-commands-installation/src/getOptionsFromRootManifest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
import { ProjectManifest } from '@pnpm/types' | ||
import { | ||
PackageExtension, | ||
PeerDependencyRules, | ||
ProjectManifest, | ||
} from '@pnpm/types' | ||
|
||
export default function getOptionsFromRootManifest (manifest: ProjectManifest) { | ||
export default function getOptionsFromRootManifest (manifest: ProjectManifest): { | ||
overrides?: Record<string, string> | ||
neverBuiltDependencies?: string[] | ||
packageExtensions?: Record<string, PackageExtension> | ||
peerDependencyRules?: PeerDependencyRules | ||
} { | ||
// We read Yarn's resolutions field for compatibility | ||
// but we really replace the version specs to any other version spec, not only to exact versions, | ||
// so we cannot call it resolutions | ||
const overrides = manifest.pnpm?.overrides ?? manifest.resolutions | ||
const neverBuiltDependencies = manifest.pnpm?.neverBuiltDependencies ?? [] | ||
const packageExtensions = manifest.pnpm?.packageExtensions | ||
const peerDependencyRules = manifest.pnpm?.peerDependencyRules | ||
return { | ||
overrides, | ||
neverBuiltDependencies, | ||
packageExtensions, | ||
peerDependencyRules, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters