Files
next.js/scripts/sync-react.js
T
Shu DingandGitHub 003b3af700 Update vendored React (#46826)
This PR updates vendored React to the latest `@next` version, as well as
corresponding changes to the manifest and module reference generation
code.

The new React version includes the following upstream changes:

- 49f741046 Fix: Infinite act loop caused by wrong shouldYield (#26317)
(Andrew Clark)
-  106ea1c58 Support Iterables in Flight (#26313) (Sebastian Markbåge)
- f905da227 [Flight] Send server reference error chunks to the client
(#26293) (Hendrik Liebau)
- e0241b660 Simplify Webpack References by encoding file path + export
name as single id (#26300) (Sebastian Markbåge)
- 25685d8a9 Codemod tests to waitFor pattern (9/?) (#26309) (Andrew
Clark)
- 64dde7082 Codemod tests to waitFor pattern (8/?) (#26308) (Andrew
Clark)
- 3cb5afb82 Codemod tests to waitFor pattern (7/?) (#26307) (Andrew
Clark)
- e98695db9 Codemod tests to waitFor pattern (6/?) (#26305) (Andrew
Clark)
- 9a52cc8bc Convert ReactLazy-test to waitFor pattern (#26304) (Andrew
Clark)
- 03462cfc7 [Fizz] External runtime: fix bug in processing existing
elements (#26303) (mofeiZ)
- faacefb4d Codemod tests to waitFor pattern (4/?) (#26302) (Andrew
Clark)
- 06460b6fb Remove unnecessary (and incorrect) code for compatibility
with Paper in the Fabric version of GlobalResponderHandler (#26290)
(Rubén Norte)
- e64a8f403 Codemod tests to waitFor pattern (3/?) (#26299) (Andrew
Clark)
- ce8a72fd4 Codemod tests to waitFor pattern (2/?) (#26296) (Andrew
Clark)
- 1f1f8eb55 [Float][Fizz][Fiber]: Refactor <style> Resource
implementation to group on flush (#26280) (Josh Story)
- 5c633a48f Add back accidentally deleted test comments (#26294) (Andrew
Clark)
- b073104c2 [DevTools] improve troubleshooting in README (#26235)
(Mengdi Chen)
- fcf218791 [DevTools] Remove renderer.js from extension build (#26234)
(Mengdi Chen)
- b72ed698f Fixed incorrect value returned as public instance from
reconciler (#26283) (Rubén Norte)
- 25a8b9735 Codemod tests to waitFor pattern (1/?) (#26288) (Andrew
Clark)
- e52446733 New internal testing helpers: waitFor, waitForAll,
waitForPaint (#26285) (Andrew Clark)
- d49e0e0be Removed unused imperative events implementation from React
Native renderer (#26282) (Rubén Norte)

---------
2023-03-06 16:51:10 -08:00

205 lines
5.8 KiB
JavaScript

// @ts-check
const path = require('path')
const { readJson, writeJson } = require('fs-extra')
const execa = require('execa')
/** @type {any} */
const fetch = require('node-fetch')
// Use this script to update Next's vendored copy of React and related packages:
//
// Basic usage (defaults to most recent React canary version):
// pnpm run sync-react
//
// Specify a canary version:
// pnpm run sync-react --version 18.3.0-next-41110021f-20230301
//
// Update package.json but skip installing the dependencies automatically:
// pnpm run sync-react --no-install
async function main() {
const noInstall = readBoolArg(process.argv, 'no-install')
let newVersionStr = readStringArg(process.argv, 'version')
if (newVersionStr === null) {
const { stdout, stderr } = await execa('npm', [
'view',
'react@next',
'version',
])
if (stderr) {
console.error(stderr)
throw new Error('Failed to read latest React canary version from npm.')
}
newVersionStr = stdout.trim()
}
const newVersionInfo = extractInfoFromReactCanaryVersion(newVersionStr)
if (!newVersionInfo) {
throw new Error(
`New react version does not match expected format: ${newVersionStr}
Choose a React canary version from npm: https://www.npmjs.com/package/react?activeTab=versions
Or, run this command with no arguments to use the most recently published version.
`
)
}
const cwd = process.cwd()
const pkgJson = await readJson(path.join(cwd, 'package.json'))
const devDependencies = pkgJson.devDependencies
const baseVersionStr = devDependencies['react-builtin'].replace(
/^npm:react@/,
''
)
const baseVersionInfo = extractInfoFromReactCanaryVersion(baseVersionStr)
if (!baseVersionInfo) {
throw new Error(
'Base react version does not match expected format: ' + baseVersionStr
)
}
const { sha: newSha, dateString: newDateString } = newVersionInfo
const { sha: baseSha, dateString: baseDateString } = baseVersionInfo
console.log(`Updating React to ${newSha}...\n`)
if (newSha === baseSha) {
console.log('Already up to date.')
return
}
for (const [dep, version] of Object.entries(devDependencies)) {
if (version.endsWith(`${baseSha}-${baseDateString}`)) {
devDependencies[dep] = version.replace(
`${baseSha}-${baseDateString}`,
`${newSha}-${newDateString}`
)
}
}
await writeJson(path.join(cwd, 'package.json'), pkgJson, { spaces: 2 })
console.log('Successfully updated React dependencies in package.json.\n')
// Install the updated dependencies and build the vendored React files.
if (noInstall) {
console.log('Skipping install step because --no-install flag was passed.\n')
} else {
console.log('Installing dependencies...\n')
const installSubprocess = execa('pnpm', ['install'])
if (installSubprocess.stdout) {
installSubprocess.stdout.pipe(process.stdout)
}
try {
await installSubprocess
} catch (error) {
console.error(error)
throw new Error('Failed to install updated dependencies.')
}
console.log('Building vendored React files...\n')
const nccSubprocess = execa('pnpm', ['taskr', 'ncc'], {
cwd: path.join(cwd, 'packages', 'next'),
})
if (nccSubprocess.stdout) {
nccSubprocess.stdout.pipe(process.stdout)
}
try {
await nccSubprocess
} catch (error) {
console.error(error)
throw new Error('Failed to run ncc.')
}
// Print extra newline after ncc output
console.log()
}
console.log(`Successfully updated React from ${baseSha} to ${newSha}.\n`)
// Fetch the changelog from GitHub and print it to the console.
try {
const changelog = await getChangelogFromGitHub(baseSha, newSha)
if (changelog === null) {
console.log(
`GitHub reported no changes between ${baseSha} and ${newSha}.`
)
} else {
console.log('Includes the following upstream changes:\n\n' + changelog)
}
} catch (error) {
console.error(error)
console.log(
'\nFailed to fetch changelog from GitHub. Changes were applied, anyway.\n'
)
}
if (noInstall) {
console.log(
`
To finish upgrading, complete the following steps:
- Install the updated dependencies: pnpm install
- Build the vendored React files: (inside packages/next dir) pnpm taskr ncc
Or run this command again without the --no-install flag to do both automatically.
`
)
}
}
function readBoolArg(argv, argName) {
return argv.indexOf('--' + argName) !== -1
}
function readStringArg(argv, argName) {
const argIndex = argv.indexOf('--' + argName)
return argIndex === -1 ? null : argv[argIndex + 1]
}
function extractInfoFromReactCanaryVersion(reactCanaryVersion) {
const match = reactCanaryVersion.match(
/(?<semverVersion>.*)-(?<releaseChannel>.*)-(?<sha>.*)-(?<dateString>.*)$/
)
return match ? match.groups : null
}
async function getChangelogFromGitHub(baseSha, newSha) {
const pageSize = 50
let changelog = []
for (let currentPage = 0; ; currentPage++) {
const response = await fetch(
`https://api.github.com/repos/facebook/react/compare/${baseSha}...${newSha}?per_page=${pageSize}&page=${currentPage}`
)
if (!response.ok) {
throw new Error('Failed to fetch commit log from GitHub.')
}
const data = await response.json()
const { commits } = data
for (const { commit, sha } of commits) {
changelog.push(
`- ${sha.slice(0, 9)} ${commit.message.split('\n')[0]} (${
commit.author.name
})`
)
}
if (commits.length !== pageSize) {
// If the number of commits is less than the page size, we've reached
// the end. Otherwise we'll keep fetching until we run out.
break
}
}
changelog.reverse()
return changelog.length > 0 ? changelog.join('\n') : null
}
main().catch((error) => {
console.error(error)
process.exit(1)
})