forked from Automattic/jetpack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
.pnpmfile.cjs
188 lines (170 loc) · 5.82 KB
/
.pnpmfile.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Note if you change something here, you'll have to make a package.json mismatch pnpm-lock.yaml to
// get it re-run. An easy way to do that is to just edit pnpm-lock.yaml to change the version number
// of husky near the top.
/**
* Fix package dependencies.
*
* We could generally do the same with pnpm.overrides in packages.json, but this allows for comments.
*
* @param {object} pkg - Dependency package.json contents.
* @returns {object} Modified pkg.
*/
function fixDeps( pkg ) {
// Depends on punycode but doesn't declare it.
// https://github.com/markdown-it/markdown-it/issues/230
// https://github.com/markdown-it/markdown-it/issues/945
if ( pkg.name === 'markdown-it' && ! pkg.dependencies.punycode ) {
pkg.dependencies.punycode = '*';
}
// Missing dep or peer dep on react.
// https://github.com/WordPress/gutenberg/issues/55171
if (
pkg.name === '@wordpress/icons' &&
! pkg.dependencies?.react &&
! pkg.peerDependencies?.react
) {
pkg.peerDependencies.react = '^18';
}
// Unnecessary dep, should be a dev dep.
// https://github.com/WordPress/gutenberg/pull/56091
if ( pkg.name === '@wordpress/components' && pkg.dependencies[ '@ariakit/test' ] ) {
delete pkg.dependencies[ '@ariakit/test' ];
}
// Turn @wordpress/eslint-plugin's eslint plugin deps into peer deps.
// https://github.com/WordPress/gutenberg/issues/39810
if ( pkg.name === '@wordpress/eslint-plugin' ) {
for ( const [ dep, ver ] of Object.entries( pkg.dependencies ) ) {
if (
dep.startsWith( 'eslint-plugin-' ) ||
dep.endsWith( '/eslint-plugin' ) ||
dep.startsWith( 'eslint-config-' ) ||
dep.endsWith( '/eslint-config' )
) {
delete pkg.dependencies[ dep ];
pkg.peerDependencies[ dep ] = ver.replace( /^\^?/, '>=' );
}
}
}
// Avoid annoying flip-flopping of sub-dep peer deps.
// https://github.com/localtunnel/localtunnel/issues/481
if ( pkg.name === 'localtunnel' ) {
for ( const [ dep, ver ] of Object.entries( pkg.dependencies ) ) {
if ( ver.match( /^\d+(\.\d+)+$/ ) ) {
pkg.dependencies[ dep ] = '^' + ver;
}
}
}
// Outdated dependency.
// No upstream bug link yet.
if ( pkg.name === 'rollup-plugin-postcss' && pkg.dependencies.cssnano === '^5.0.1' ) {
pkg.dependencies.cssnano = '^5.0.1 || ^6';
}
// Outdated dependency.
// No upstream bug link yet.
if ( pkg.name === 'svelte-navigator' && pkg.dependencies.svelte2tsx === '^0.1.151' ) {
pkg.dependencies.svelte2tsx = '^0.6.10';
}
// Missing dep or peer dep on @babel/runtime
// https://github.com/zillow/react-slider/issues/296
if (
pkg.name === 'react-slider' &&
! pkg.dependencies?.[ '@babel/runtime' ] &&
! pkg.peerDependencies?.[ '@babel/runtime' ]
) {
pkg.peerDependencies[ '@babel/runtime' ] = '^7';
}
// Typo in package.json caused a missing peer dep.
// Already fixed by https://github.com/yjs/y-webrtc/pull/48, not yet released.
// Already fixed by https://github.com/yjs/y-protocols/pull/12, not yet released.
if (
( pkg.name === 'y-webrtc' && pkg.version === '10.2.5' ) ||
( pkg.name === 'y-protocols' && pkg.version === '1.0.5' )
) {
pkg.peerDependencies.yjs = '^13.5.6';
}
return pkg;
}
/**
* Fix package peer dependencies.
*
* This can't be done with pnpm.overrides.
*
* @param {object} pkg - Dependency package.json contents.
* @returns {object} Modified pkg.
*/
function fixPeerDeps( pkg ) {
// Indirect deps that still depend on React <18.
const reactOldPkgs = new Set( [
// Still on 16.
'react-autosize-textarea', // @wordpress/block-editor <https://github.com/WordPress/gutenberg/issues/39619>
// Still on 17.
'reakit', // @wordpress/components <https://github.com/WordPress/gutenberg/issues/53278>
'reakit-system', // @wordpress/components → reakit
'reakit-utils', // @wordpress/components → reakit
'reakit-warning', // @wordpress/components → reakit
] );
if ( reactOldPkgs.has( pkg.name ) ) {
for ( const p of [ 'react', 'react-dom' ] ) {
if ( ! pkg.peerDependencies?.[ p ] ) {
continue;
}
if (
pkg.peerDependencies[ p ].match( /(?:^|\|\|\s*)(?:\^16|16\.x)/ ) &&
! pkg.peerDependencies[ p ].match( /(?:^|\|\|\s*)(?:\^17|17\.x)/ )
) {
pkg.peerDependencies[ p ] += ' || ^17';
}
if (
pkg.peerDependencies[ p ].match( /(?:^|\|\|\s*)(?:\^17|17\.x)/ ) &&
! pkg.peerDependencies[ p ].match( /(?:^|\|\|\s*)(?:\^18|18\.x)/ )
) {
pkg.peerDependencies[ p ] += ' || ^18';
}
}
}
return pkg;
}
/**
* Pnpm package hook.
*
* @see https://pnpm.io/pnpmfile#hooksreadpackagepkg-context-pkg--promisepkg
* @param {object} pkg - Dependency package.json contents.
* @param {object} context - Pnpm object of some sort.
* @returns {object} Modified pkg.
*/
function readPackage( pkg, context ) {
if ( pkg.name ) {
pkg = fixDeps( pkg, context );
pkg = fixPeerDeps( pkg, context );
}
return pkg;
}
/**
* Pnpm lockfile hook.
*
* @see https://pnpm.io/pnpmfile#hooksafterallresolvedlockfile-context-lockfile--promiselockfile
* @param {object} lockfile - Lockfile data.
* @returns {object} Modified lockfile.
*/
function afterAllResolved( lockfile ) {
// If there's only one "importer", it's probably pnpx rather than the monorepo. Don't interfere.
if ( Object.keys( lockfile.importers ).length === 1 ) {
return lockfile;
}
for ( const [ k, v ] of Object.entries( lockfile.packages ) ) {
// Forbid installing webpack without webpack-cli. It results in lots of spurious lockfile changes.
// https://github.com/pnpm/pnpm/issues/3935
if ( k.startsWith( '/webpack/' ) && ! v.dependencies[ 'webpack-cli' ] ) {
throw new Error(
"Something you've done is trying to add a dependency on webpack without webpack-cli.\nThis is not allowed, as it tends to result in pnpm lockfile flip-flopping.\nSee https://github.com/pnpm/pnpm/issues/3935 for the upstream bug report."
);
}
}
return lockfile;
}
module.exports = {
hooks: {
readPackage,
afterAllResolved,
},
};