Unverified Commit 632725b0 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[sync-react] Allow syncing React with local build (#90277)

parent cde39614
...@@ -3,13 +3,22 @@ ...@@ -3,13 +3,22 @@
const path = require('path') const path = require('path')
const fsp = require('fs/promises') const fsp = require('fs/promises')
const process = require('process') const process = require('process')
const { pathToFileURL } = require('url')
const execa = require('execa') const execa = require('execa')
const { Octokit } = require('octokit') const { Octokit } = require('octokit')
const SemVer = require('semver') const SemVer = require('semver')
const yargs = require('yargs') const yargs = require('yargs')
/** @type {any} */ // Use this script to update Next's vendored copy of React and related packages:
const fetch = require('node-fetch') //
// Basic usage (defaults to most recent React canary version):
// pnpm run sync-react
//
// Update package.json but skip installing the dependencies automatically:
// pnpm run sync-react --no-install
//
// Sync from a local checkout of React (requires having React built first):
// pnpm run sync-react --version /path/to/react/checkout/
const repoOwner = 'vercel' const repoOwner = 'vercel'
const repoName = 'next.js' const repoName = 'next.js'
...@@ -45,6 +54,10 @@ const appManifestsInstallingNextjsPeerDependencies = [ ...@@ -45,6 +54,10 @@ const appManifestsInstallingNextjsPeerDependencies = [
] ]
async function getSchedulerVersion(reactVersion) { async function getSchedulerVersion(reactVersion) {
if (reactVersion.startsWith('file://')) {
return reactVersion
}
const url = `https://registry.npmjs.org/react-dom/${reactVersion}` const url = `https://registry.npmjs.org/react-dom/${reactVersion}`
const response = await fetch(url, { const response = await fetch(url, {
headers: { headers: {
...@@ -62,13 +75,18 @@ async function getSchedulerVersion(reactVersion) { ...@@ -62,13 +75,18 @@ async function getSchedulerVersion(reactVersion) {
return manifest.dependencies['scheduler'] return manifest.dependencies['scheduler']
} }
// Use this script to update Next's vendored copy of React and related packages: /**
// * @param {string} packageName
// Basic usage (defaults to most recent React canary version): * @param {string} versionStr An NPM version or a file URL to a React checkout
// pnpm run sync-react * @returns {string}
// */
// Update package.json but skip installing the dependencies automatically: function getPackageVersion(packageName, versionStr) {
// pnpm run sync-react --no-install if (versionStr.startsWith('file://')) {
return new URL(packageName, versionStr).href
}
return `npm:${packageName}@${versionStr}`
}
async function sync({ channel, newVersionStr, noInstall }) { async function sync({ channel, newVersionStr, noInstall }) {
const useExperimental = channel === 'experimental' const useExperimental = channel === 'experimental'
...@@ -88,32 +106,45 @@ async function sync({ channel, newVersionStr, noInstall }) { ...@@ -88,32 +106,45 @@ async function sync({ channel, newVersionStr, noInstall }) {
return return
} }
const baseSchedulerVersionStr = devDependencies[
useExperimental ? 'scheduler-experimental-builtin' : 'scheduler-builtin'
].replace(/^npm:scheduler@/, '')
const newSchedulerVersionStr = await getSchedulerVersion(newVersionStr) const newSchedulerVersionStr = await getSchedulerVersion(newVersionStr)
console.log(`Updating "scheduler@${channel}" to ${newSchedulerVersionStr}...`) console.log(`Updating "scheduler@${channel}" to ${newSchedulerVersionStr}...`)
for (const [dep, version] of Object.entries(devDependencies)) { for (const packageName of ['react', 'react-dom']) {
if (version.endsWith(baseVersionStr)) { devDependencies[
devDependencies[dep] = version.replace(baseVersionStr, newVersionStr) `${packageName}${useExperimental ? '-experimental' : ''}-builtin`
} else if (version.endsWith(baseSchedulerVersionStr)) { ] = getPackageVersion(packageName, newVersionStr)
devDependencies[dep] = version.replace(
baseSchedulerVersionStr, if (!useExperimental) {
newSchedulerVersionStr pnpmOverrides[packageName] = getPackageVersion(packageName, newVersionStr)
)
} }
} }
for (const [dep, version] of Object.entries(pnpmOverrides)) {
if (version.endsWith(baseVersionStr)) { for (const packageName of [
pnpmOverrides[dep] = version.replace(baseVersionStr, newVersionStr) 'react-server-dom-turbopack',
} else if (version.endsWith(baseSchedulerVersionStr)) { 'react-server-dom-webpack',
pnpmOverrides[dep] = version.replace( ]) {
baseSchedulerVersionStr, devDependencies[`${packageName}${useExperimental ? '-experimental' : ''}`] =
newSchedulerVersionStr getPackageVersion(packageName, newVersionStr)
)
}
} }
devDependencies[
`scheduler-${useExperimental ? 'experimental-' : ''}builtin`
] = getPackageVersion('scheduler', newSchedulerVersionStr)
if (!useExperimental) {
pnpmOverrides.scheduler = getPackageVersion(
'scheduler',
newSchedulerVersionStr
)
// TODO: Should be handled like the other React packages
devDependencies['react-is-builtin'] = newVersionStr.startsWith('file://')
? new URL('react-is', newVersionStr).href
: `npm:react-is@${newVersionStr}`
pnpmOverrides['react-is'] = newVersionStr.startsWith('file://')
? new URL('react-is', newVersionStr).href
: `npm:react-is@${newVersionStr}`
}
await fsp.writeFile( await fsp.writeFile(
path.join(cwd, 'package.json'), path.join(cwd, 'package.json'),
JSON.stringify(pkgJson, null, 2) + JSON.stringify(pkgJson, null, 2) +
...@@ -122,8 +153,25 @@ async function sync({ channel, newVersionStr, noInstall }) { ...@@ -122,8 +153,25 @@ async function sync({ channel, newVersionStr, noInstall }) {
) )
} }
function extractInfoFromReactVersion(reactVersion) { /**
const match = reactVersion.match( * @typedef {object} ReactVersionInfo
* @property {string} semverVersion - The semver version of React.
* @property {string} releaseLabel - The release label of React (e.g. "canary", "rc").
* @property {string} sha - The commit SHA of the React version.
* @property {string} dateString - The date string of the React version.
* @returns {ReactVersionInfo}
*/
function extractInfoFromReactVersion(versionStr) {
if (versionStr.startsWith('file://')) {
return {
dateString: new Date().toISOString().split('T')[0],
releaseLabel: 'local',
semverVersion: '0.0.0',
sha: 'local',
}
}
const match = versionStr.match(
/(?<semverVersion>.*)-(?<releaseLabel>.*)-(?<sha>.*)-(?<dateString>.*)$/ /(?<semverVersion>.*)-(?<releaseLabel>.*)-(?<sha>.*)-(?<dateString>.*)$/
) )
return match ? match.groups : null return match ? match.groups : null
...@@ -134,10 +182,10 @@ async function getChangelogFromGitHub(baseSha, newSha) { ...@@ -134,10 +182,10 @@ async function getChangelogFromGitHub(baseSha, newSha) {
let changelog = [] let changelog = []
for (let currentPage = 1; ; currentPage++) { for (let currentPage = 1; ; currentPage++) {
const url = `https://api.github.com/repos/facebook/react/compare/${baseSha}...${newSha}?per_page=${pageSize}&page=${currentPage}` const url = `https://api.github.com/repos/facebook/react/compare/${baseSha}...${newSha}?per_page=${pageSize}&page=${currentPage}`
const headers = {} const headers = new Headers()
// GITHUB_TOKEN is optional but helps in case of rate limiting during development. // GITHUB_TOKEN is optional but helps in case of rate limiting during development.
if (process.env.GITHUB_TOKEN) { if (process.env.GITHUB_TOKEN) {
headers.Authorization = `token ${process.env.GITHUB_TOKEN}` headers.set('Authorization', `token ${process.env.GITHUB_TOKEN}`)
} }
const response = await fetch(url, { const response = await fetch(url, {
headers, headers,
...@@ -229,7 +277,14 @@ async function main() { ...@@ -229,7 +277,14 @@ async function main() {
}) })
.options('install', { default: true, type: 'boolean' }) .options('install', { default: true, type: 'boolean' })
.options('version', { default: null, type: 'string' }).argv .options('version', { default: null, type: 'string' }).argv
const { actor, createPull, commit, install, version } = argv let { actor, createPull, commit, install, version } = argv
if (version !== null && version.startsWith('/')) {
version = pathToFileURL(version).href
// Ensure trailing slash so that the URL is treated as a directory.
if (!version.endsWith('/')) {
version += '/'
}
}
async function commitEverything(message) { async function commitEverything(message) {
await execa('git', ['add', '-A']) await execa('git', ['add', '-A'])
...@@ -281,7 +336,10 @@ Or, run this command with no arguments to use the most recently published versio ...@@ -281,7 +336,10 @@ Or, run this command with no arguments to use the most recently published versio
} }
const { sha: newSha, dateString: newDateString } = newVersionInfo const { sha: newSha, dateString: newDateString } = newVersionInfo
const branchName = `update/react/${newVersionStr}` const branchName = newVersionStr.startsWith('file://')
? // left to user to name their local sync branch
`update/react/local`
: `update/react/${newVersionStr}`
if (createPull) { if (createPull) {
const { exitCode, all, command } = await execa( const { exitCode, all, command } = await execa(
'git', 'git',
...@@ -319,8 +377,14 @@ Or, run this command with no arguments to use the most recently published versio ...@@ -319,8 +377,14 @@ Or, run this command with no arguments to use the most recently published versio
'' ''
) )
let experimentalNewVersionStr = `0.0.0-experimental-${newSha}-${newDateString}`
if (version !== null && version.startsWith('file://')) {
experimentalNewVersionStr = new URL('build/oss-experimental/', version).href
newVersionStr = new URL('build/oss-stable/', version).href
}
await sync({ await sync({
newVersionStr: `0.0.0-experimental-${newSha}-${newDateString}`, newVersionStr: experimentalNewVersionStr,
noInstall: !install, noInstall: !install,
channel: 'experimental', channel: 'experimental',
}) })
...@@ -470,23 +534,27 @@ Or, run this command with no arguments to use the most recently published versio ...@@ -470,23 +534,27 @@ Or, run this command with no arguments to use the most recently published versio
} }
let prDescription = '' let prDescription = ''
if (syncPagesRouterReact) { if (newVersionInfo.releaseLabel === 'local') {
prDescription += `**breaking change for canary users: Bumps peer dependency of React from \`${baseVersionStr}\` to \`${pagesRouterReactVersion}\`**\n\n` prDescription = "Can't generate a changelog for local builds"
} } else {
if (syncPagesRouterReact) {
prDescription += `**breaking change for canary users: Bumps peer dependency of React from \`${baseVersionStr}\` to \`${pagesRouterReactVersion}\`**\n\n`
}
// Fetch the changelog from GitHub and print it to the console. // Fetch the changelog from GitHub and print it to the console.
prDescription += `[diff facebook/react@${baseSha}...${newSha}](https://github.com/facebook/react/compare/${baseSha}...${newSha})\n\n` prDescription += `[diff facebook/react@${baseSha}...${newSha}](https://github.com/facebook/react/compare/${baseSha}...${newSha})\n\n`
try { try {
const changelog = await getChangelogFromGitHub(baseSha, newSha) const changelog = await getChangelogFromGitHub(baseSha, newSha)
if (changelog === null) { if (changelog === null) {
prDescription += `GitHub reported no changes between ${baseSha} and ${newSha}.` prDescription += `GitHub reported no changes between ${baseSha} and ${newSha}.`
} else { } else {
prDescription += `<details>\n<summary>React upstream changes</summary>\n\n${changelog}\n\n</details>` prDescription += `<details>\n<summary>React upstream changes</summary>\n\n${changelog}\n\n</details>`
}
} catch (error) {
console.error(error)
prDescription +=
'\nFailed to fetch changelog from GitHub. Changes were applied, anyway.\n'
} }
} catch (error) {
console.error(error)
prDescription +=
'\nFailed to fetch changelog from GitHub. Changes were applied, anyway.\n'
} }
if (!install) { if (!install) {
......
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