Unverified Commit 0fc762c7 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

sync-react: Automatically update all references to the React peer dependency version (#69194)

parent e1b889fd
...@@ -11,6 +11,10 @@ import pkg from "../package.json"; ...@@ -11,6 +11,10 @@ import pkg from "../package.json";
import { GetTemplateFileArgs, InstallTemplateArgs } from "./types"; import { GetTemplateFileArgs, InstallTemplateArgs } from "./types";
// Do not rename or format. sync-react script relies on this line.
// prettier-ignore
const nextjsReactPeerVersion = "19.0.0-rc-1eaccd82-20240816";
/** /**
* Get the file path for a given file in a template, e.g. "next.config.js". * Get the file path for a given file in a template, e.g. "next.config.js".
*/ */
...@@ -184,8 +188,8 @@ export const installTemplate = async ({ ...@@ -184,8 +188,8 @@ export const installTemplate = async ({
* Default dependencies. * Default dependencies.
*/ */
dependencies: { dependencies: {
react: "19.0.0-rc-1eaccd82-20240816", react: nextjsReactPeerVersion,
"react-dom": "19.0.0-rc-1eaccd82-20240816", "react-dom": nextjsReactPeerVersion,
next: version, next: version,
}, },
devDependencies: {}, devDependencies: {},
......
...@@ -18,6 +18,10 @@ const exec = promisify(execOrig) ...@@ -18,6 +18,10 @@ const exec = promisify(execOrig)
const core = require('@actions/core') const core = require('@actions/core')
const { getTestFilter } = require('./test/get-test-filter') const { getTestFilter } = require('./test/get-test-filter')
// Do not rename or format. sync-react script relies on this line.
// prettier-ignore
const nextjsReactPeerVersion = "19.0.0-rc-1eaccd82-20240816";
let argv = require('yargs/yargs')(process.argv.slice(2)) let argv = require('yargs/yargs')(process.argv.slice(2))
.string('type') .string('type')
.string('test-pattern') .string('test-pattern')
...@@ -409,7 +413,7 @@ ${ENDGROUP}`) ...@@ -409,7 +413,7 @@ ${ENDGROUP}`)
// run `pnpm install` each time. // run `pnpm install` each time.
console.log(`${GROUP}Creating shared Next.js install`) console.log(`${GROUP}Creating shared Next.js install`)
const reactVersion = const reactVersion =
process.env.NEXT_TEST_REACT_VERSION || '19.0.0-rc-1eaccd82-20240816' process.env.NEXT_TEST_REACT_VERSION || nextjsReactPeerVersion
const { installDir, pkgPaths, tmpRepoDir } = await createNextInstall({ const { installDir, pkgPaths, tmpRepoDir } = await createNextInstall({
parentSpan: mockTrace(), parentSpan: mockTrace(),
dependencies: { dependencies: {
......
...@@ -7,6 +7,19 @@ const execa = require('execa') ...@@ -7,6 +7,19 @@ const execa = require('execa')
/** @type {any} */ /** @type {any} */
const fetch = require('node-fetch') const fetch = require('node-fetch')
const filesReferencingReactPeerDependencyVersion = [
'run-tests.js',
'packages/create-next-app/templates/index.ts',
'test/lib/next-modes/base.ts',
]
const appManifestsInstallingNextjsPeerDependencies = [
'examples/reproduction-template/package.json',
'test/.stats-app/package.json',
// TODO: These should use the usual test helpers that automatically install the right React version
'test/e2e/next-test/first-time-setup-js/package.json',
'test/e2e/next-test/first-time-setup-ts/package.json',
]
// Use this script to update Next's vendored copy of React and related packages: // Use this script to update Next's vendored copy of React and related packages:
// //
// Basic usage (defaults to most recent React canary version): // Basic usage (defaults to most recent React canary version):
...@@ -15,7 +28,9 @@ const fetch = require('node-fetch') ...@@ -15,7 +28,9 @@ const fetch = require('node-fetch')
// Update package.json but skip installing the dependencies automatically: // Update package.json but skip installing the dependencies automatically:
// pnpm run sync-react --no-install // pnpm run sync-react --no-install
async function sync(channel = 'next') { async function sync({ channel, useAsPeerDependency }) {
const errors = []
const noInstall = readBoolArg(process.argv, 'no-install') const noInstall = readBoolArg(process.argv, 'no-install')
const useExperimental = channel === 'experimental' const useExperimental = channel === 'experimental'
let newVersionStr = readStringArg(process.argv, 'version') let newVersionStr = readStringArg(process.argv, 'version')
...@@ -110,6 +125,58 @@ Or, run this command with no arguments to use the most recently published versio ...@@ -110,6 +125,58 @@ Or, run this command with no arguments to use the most recently published versio
) )
console.log('Successfully updated React dependencies in package.json.\n') console.log('Successfully updated React dependencies in package.json.\n')
if (useAsPeerDependency) {
for (const fileName of filesReferencingReactPeerDependencyVersion) {
const filePath = path.join(cwd, fileName)
const previousSource = await fsp.readFile(filePath, 'utf-8')
const updatedSource = previousSource.replace(
`const nextjsReactPeerVersion = "${baseVersionStr}";`,
`const nextjsReactPeerVersion = "${newVersionStr}";`
)
if (updatedSource === previousSource) {
errors.push(
new Error(
`${fileName}: Failed to update ${baseVersionStr} to ${newVersionStr}. Is this file still referencing the React peer dependency version?`
)
)
} else {
await fsp.writeFile(filePath, updatedSource)
}
}
const nextjsPackageJsonPath = path.join(
cwd,
'packages',
'next',
'package.json'
)
const nextjsPackageJson = JSON.parse(
await fsp.readFile(nextjsPackageJsonPath, 'utf-8')
)
nextjsPackageJson.peerDependencies.react = `${newVersionStr}`
nextjsPackageJson.peerDependencies['react-dom'] = `${newVersionStr}`
await fsp.writeFile(
nextjsPackageJsonPath,
JSON.stringify(nextjsPackageJson, null, 2) +
// Prettier would add a newline anyway so do it manually to skip the additional `pnpm prettier-write`
'\n'
)
for (const fileName of appManifestsInstallingNextjsPeerDependencies) {
const packageJsonPath = path.join(cwd, fileName)
const packageJson = await fsp.readFile(packageJsonPath, 'utf-8')
const manifest = JSON.parse(packageJson)
manifest.dependencies['react'] = newVersionStr
manifest.dependencies['react-dom'] = newVersionStr
await fsp.writeFile(
packageJsonPath,
JSON.stringify(manifest, null, 2) +
// Prettier would add a newline anyway so do it manually to skip the additional `pnpm prettier-write`
'\n'
)
}
}
// Install the updated dependencies and build the vendored React files. // Install the updated dependencies and build the vendored React files.
if (noInstall) { if (noInstall) {
console.log('Skipping install step because --no-install flag was passed.\n') console.log('Skipping install step because --no-install flag was passed.\n')
...@@ -179,6 +246,11 @@ Or run this command again without the --no-install flag to do both automatically ...@@ -179,6 +246,11 @@ Or run this command again without the --no-install flag to do both automatically
await fsp.writeFile(path.join(cwd, '.github/.react-version'), newVersionStr) await fsp.writeFile(path.join(cwd, '.github/.react-version'), newVersionStr)
if (errors.length) {
// eslint-disable-next-line no-undef -- Defined in Node.js
throw new AggregateError(errors)
}
console.log( console.log(
`Successfully updated React from ${baseSha} to ${newSha}.\n` + `Successfully updated React from ${baseSha} to ${newSha}.\n` +
`Don't forget to find & replace all references to the React version '${baseVersionStr}' with '${newVersionStr}':\n` + `Don't forget to find & replace all references to the React version '${baseVersionStr}' with '${newVersionStr}':\n` +
...@@ -255,8 +327,8 @@ async function getChangelogFromGitHub(baseSha, newSha) { ...@@ -255,8 +327,8 @@ async function getChangelogFromGitHub(baseSha, newSha) {
return changelog.length > 0 ? changelog.join('\n') : null return changelog.length > 0 ? changelog.join('\n') : null
} }
sync('experimental') sync({ channel: 'experimental', useAsPeerDependency: false })
.then(() => sync('rc')) .then(() => sync({ channel: 'rc', useAsPeerDependency: true }))
.catch((error) => { .catch((error) => {
console.error(error) console.error(error)
process.exit(1) process.exit(1)
......
...@@ -49,6 +49,10 @@ type OmitFirstArgument<F> = F extends ( ...@@ -49,6 +49,10 @@ type OmitFirstArgument<F> = F extends (
? (...args: P) => R ? (...args: P) => R
: never : never
// Do not rename or format. sync-react script relies on this line.
// prettier-ignore
const nextjsReactPeerVersion = "19.0.0-rc-1eaccd82-20240816";
export class NextInstance { export class NextInstance {
protected files: FileRef | { [filename: string]: string | FileRef } protected files: FileRef | { [filename: string]: string | FileRef }
protected nextConfig?: NextConfig protected nextConfig?: NextConfig
...@@ -163,7 +167,7 @@ export class NextInstance { ...@@ -163,7 +167,7 @@ export class NextInstance {
) )
const reactVersion = const reactVersion =
process.env.NEXT_TEST_REACT_VERSION || '19.0.0-rc-1eaccd82-20240816' process.env.NEXT_TEST_REACT_VERSION || nextjsReactPeerVersion
const finalDependencies = { const finalDependencies = {
react: reactVersion, react: reactVersion,
'react-dom': reactVersion, 'react-dom': reactVersion,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment