|
3 | 3 | import { readFile, writeFile } from 'node:fs/promises'
|
4 | 4 |
|
5 | 5 | import fg from 'fast-glob'
|
6 |
| -import { gte, satisfies, valid } from 'semver' |
| 6 | +import { coerce, gt, gte, satisfies, valid } from 'semver' |
7 | 7 | import { execaCommand } from 'execa'
|
8 | 8 |
|
9 | 9 | const FUTURE_NEXT_PATCH_VERSION = '14.999.0'
|
@@ -105,8 +105,33 @@ export async function setNextVersionInFixture(
|
105 | 105 | const nextPeerDependencies = JSON.parse(stdout)
|
106 | 106 |
|
107 | 107 | if (updateReact && nextVersionRequiresReact19(checkVersion)) {
|
| 108 | + // canaries started reporting peerDependencies as `^18.2.0 || 19.0.0-rc-<hash>-<date>` |
| 109 | + // with https://github.com/vercel/next.js/pull/70219 which is valid range for package managers |
| 110 | + // but not for @nx/next which checks dependencies and tries to assure that at least React 18 is used |
| 111 | + // but the check doesn't handle the alternative in version selector which thinks it's not valid: |
| 112 | + // https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/next/src/executors/build/build.impl.ts#L48 |
| 113 | + // https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/devkit/src/utils/semver.ts#L17 |
| 114 | + // so to workaround this nx/next issue we modify next peerDeps to extract highest version alternative to use |
| 115 | + const highestNextReactPeerDependencySelector = ( |
| 116 | + nextPeerDependencies['react'] ?? '^18.2.0' |
| 117 | + ) |
| 118 | + .split('||') |
| 119 | + .map((alternative) => { |
| 120 | + const selector = alternative.trim() |
| 121 | + const coerced = coerce(selector)?.format() |
| 122 | + return { |
| 123 | + selector, |
| 124 | + coerced, |
| 125 | + validSelector: valid(selector), |
| 126 | + validCoerced: valid(coerced), |
| 127 | + } |
| 128 | + }) |
| 129 | + .sort((a, b) => { |
| 130 | + return gt(a.coerced, b.coerced) ? -1 : 1 |
| 131 | + })[0].selector |
| 132 | + |
108 | 133 | const reactVersion =
|
109 |
| - operation === 'update' ? nextPeerDependencies['react'] : REACT_18_VERSION |
| 134 | + operation === 'update' ? highestNextReactPeerDependencySelector : REACT_18_VERSION |
110 | 135 | packageJson.dependencies.react = reactVersion
|
111 | 136 | packageJson.dependencies['react-dom'] = reactVersion
|
112 | 137 | }
|
|
0 commit comments