Unverified Commit bfaffbdd authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Alias react and react-dom by default (#38245)

Previously we use custom webpack alias for specific react versions for non server side node runtime aliases. This PR alias the entire folders of `react/` and `react-dom/` so that no more alias in next.config is required but only the nodejs require hook.

* Alias `react` and `react-dom` by default
* Use `react@experimental` to run server components integration test
* Drop with-react-17 test util, add `__NEXT_REACT_CHANNEL` as an env var for testing and development to specify the react channel is 17 or new experimental version
parent a1504f4c
...@@ -38,7 +38,8 @@ ...@@ -38,7 +38,8 @@
"lint-staged": "lint-staged", "lint-staged": "lint-staged",
"next-with-deps": "./scripts/next-with-deps.sh", "next-with-deps": "./scripts/next-with-deps.sh",
"next": "node --trace-deprecation --enable-source-maps packages/next/dist/bin/next", "next": "node --trace-deprecation --enable-source-maps packages/next/dist/bin/next",
"next-react-17": "node --trace-deprecation --enable-source-maps -r ./test/lib/react-17-require-hook.js packages/next/dist/bin/next", "next-react-17": "__NEXT_REACT_CHANNEL=17 node --trace-deprecation --enable-source-maps -r ./test/lib/react-channel-require-hook.js packages/next/dist/bin/next",
"next-react-exp": "__NEXT_REACT_CHANNEL=exp node --trace-deprecation --enable-source-maps -r ./test/lib/react-channel-require-hook.js packages/next/dist/bin/next",
"next-no-sourcemaps": "node --trace-deprecation packages/next/dist/bin/next", "next-no-sourcemaps": "node --trace-deprecation packages/next/dist/bin/next",
"clean-trace-jaeger": "rm -rf test/integration/basic/.next && TRACE_TARGET=JAEGER node --trace-deprecation --enable-source-maps packages/next/dist/bin/next build test/integration/basic", "clean-trace-jaeger": "rm -rf test/integration/basic/.next && TRACE_TARGET=JAEGER node --trace-deprecation --enable-source-maps packages/next/dist/bin/next build test/integration/basic",
"debug": "node --inspect packages/next/dist/bin/next", "debug": "node --inspect packages/next/dist/bin/next",
...@@ -175,6 +176,8 @@ ...@@ -175,6 +176,8 @@
"react-17": "npm:react@17.0.2", "react-17": "npm:react@17.0.2",
"react-dom": "18.2.0", "react-dom": "18.2.0",
"react-dom-17": "npm:react-dom@17.0.2", "react-dom-17": "npm:react-dom@17.0.2",
"react-dom-exp": "npm:react-dom@0.0.0-experimental-4cd788aef-20220630",
"react-exp": "npm:react@0.0.0-experimental-4cd788aef-20220630",
"react-ssr-prepass": "1.0.8", "react-ssr-prepass": "1.0.8",
"react-virtualized": "9.22.3", "react-virtualized": "9.22.3",
"relay-compiler": "13.0.2", "relay-compiler": "13.0.2",
......
...@@ -3,7 +3,7 @@ import chalk from 'next/dist/compiled/chalk' ...@@ -3,7 +3,7 @@ import chalk from 'next/dist/compiled/chalk'
import crypto from 'crypto' import crypto from 'crypto'
import { webpack } from 'next/dist/compiled/webpack/webpack' import { webpack } from 'next/dist/compiled/webpack/webpack'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack' import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import path, { join as pathJoin, relative as relativePath } from 'path' import path, { dirname, join as pathJoin, relative as relativePath } from 'path'
import { escapeStringRegexp } from '../shared/lib/escape-regexp' import { escapeStringRegexp } from '../shared/lib/escape-regexp'
import { import {
DOT_NEXT_ALIAS, DOT_NEXT_ALIAS,
...@@ -627,6 +627,9 @@ export default async function getBaseWebpackConfig( ...@@ -627,6 +627,9 @@ export default async function getBaseWebpackConfig(
] ]
} }
const reactDir = dirname(require.resolve('react/package.json'))
const reactDomDir = dirname(require.resolve('react-dom/package.json'))
const resolveConfig = { const resolveConfig = {
// Disable .mjs for node_modules bundling // Disable .mjs for node_modules bundling
extensions: isNodeServer extensions: isNodeServer
...@@ -653,6 +656,12 @@ export default async function getBaseWebpackConfig( ...@@ -653,6 +656,12 @@ export default async function getBaseWebpackConfig(
alias: { alias: {
next: NEXT_PROJECT_ROOT, next: NEXT_PROJECT_ROOT,
react: `${reactDir}`,
'react-dom$': `${reactDomDir}`,
'react-dom/server$': `${reactDomDir}/server`,
'react-dom/server.browser$': `${reactDomDir}/server.browser`,
'react-dom/client$': `${reactDomDir}/client`,
...customAppAliases, ...customAppAliases,
...customErrorAlias, ...customErrorAlias,
...customDocumentAliases, ...customDocumentAliases,
......
...@@ -140,6 +140,8 @@ importers: ...@@ -140,6 +140,8 @@ importers:
react-17: npm:react@17.0.2 react-17: npm:react@17.0.2
react-dom: 18.2.0 react-dom: 18.2.0
react-dom-17: npm:react-dom@17.0.2 react-dom-17: npm:react-dom@17.0.2
react-dom-exp: npm:react-dom@0.0.0-experimental-4cd788aef-20220630
react-exp: npm:react@0.0.0-experimental-4cd788aef-20220630
react-ssr-prepass: 1.0.8 react-ssr-prepass: 1.0.8
react-virtualized: 9.22.3 react-virtualized: 9.22.3
relay-compiler: 13.0.2 relay-compiler: 13.0.2
...@@ -292,6 +294,8 @@ importers: ...@@ -292,6 +294,8 @@ importers:
react-17: /react/17.0.2 react-17: /react/17.0.2
react-dom: 18.2.0_react@18.2.0 react-dom: 18.2.0_react@18.2.0
react-dom-17: /react-dom/17.0.2_react@18.2.0 react-dom-17: /react-dom/17.0.2_react@18.2.0
react-dom-exp: /react-dom/0.0.0-experimental-4cd788aef-20220630_react@18.2.0
react-exp: /react/0.0.0-experimental-4cd788aef-20220630
react-ssr-prepass: 1.0.8_qncsgtzehe3fgiqp6tr7lwq6fm react-ssr-prepass: 1.0.8_qncsgtzehe3fgiqp6tr7lwq6fm
react-virtualized: 9.22.3_biqbaboplfbrettd7655fr4n2y react-virtualized: 9.22.3_biqbaboplfbrettd7655fr4n2y
relay-compiler: 13.0.2 relay-compiler: 13.0.2
...@@ -18490,6 +18494,16 @@ packages: ...@@ -18490,6 +18494,16 @@ packages:
strip-json-comments: 2.0.1 strip-json-comments: 2.0.1
dev: true dev: true
/react-dom/0.0.0-experimental-4cd788aef-20220630_react@18.2.0:
resolution: {integrity: sha512-VN80AwDQ19WbcX70m/3/ho/eahBIMVsrxbP7C0KU/tNe6BRwc7+QzvdDxSb2CwLlk/MZrOansFpQYEefn90w9Q==}
peerDependencies:
react: 0.0.0-experimental-4cd788aef-20220630
dependencies:
loose-envify: 1.4.0
react: 18.2.0
scheduler: 0.0.0-experimental-4cd788aef-20220630
dev: true
/react-dom/17.0.2_react@18.2.0: /react-dom/17.0.2_react@18.2.0:
resolution: {integrity: sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==} resolution: {integrity: sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==}
peerDependencies: peerDependencies:
...@@ -18570,6 +18584,13 @@ packages: ...@@ -18570,6 +18584,13 @@ packages:
react-lifecycles-compat: 3.0.4 react-lifecycles-compat: 3.0.4
dev: true dev: true
/react/0.0.0-experimental-4cd788aef-20220630:
resolution: {integrity: sha512-mPez9MeF8whDoKAtz7bGvwOWn3THEzZvftQAGAlzrohcszPDOyq1GEc3sFLKDZTSgRzVzALXcfaVoJ0EqWcgnw==}
engines: {node: '>=0.10.0'}
dependencies:
loose-envify: 1.4.0
dev: true
/react/17.0.2: /react/17.0.2:
resolution: {integrity: sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==} resolution: {integrity: sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==}
engines: {node: '>=0.10.0'} engines: {node: '>=0.10.0'}
...@@ -19552,6 +19573,12 @@ packages: ...@@ -19552,6 +19573,12 @@ packages:
xmlchars: 2.2.0 xmlchars: 2.2.0
dev: true dev: true
/scheduler/0.0.0-experimental-4cd788aef-20220630:
resolution: {integrity: sha512-ywbmX5l/JpTWQbUy9IcHO0fzaHGL/IIgM5lj1IsJA0KYn3IZx6fJkXiVRFFY1GuXAsLsalniC+SM5NLjUED8fg==}
dependencies:
loose-envify: 1.4.0
dev: true
/scheduler/0.20.2: /scheduler/0.20.2:
resolution: {integrity: sha512-2eWfGgAqqWFGqtdMmcL5zCMK1U8KlXv8SQFGglL3CEtd0aDVDWgeF/YoCmvln55m5zSk3J/20hTaSBeSObsQDQ==} resolution: {integrity: sha512-2eWfGgAqqWFGqtdMmcL5zCMK1U8KlXv8SQFGglL3CEtd0aDVDWgeF/YoCmvln55m5zSk3J/20hTaSBeSObsQDQ==}
dependencies: dependencies:
......
const path = require('path')
module.exports = require(path.join(__dirname, '../../lib/with-react-17.js'))({})
...@@ -5,7 +5,7 @@ import webdriver from 'next-webdriver' ...@@ -5,7 +5,7 @@ import webdriver from 'next-webdriver'
import { nextBuild, nextStart, findPort, killApp, check } from 'next-test-utils' import { nextBuild, nextStart, findPort, killApp, check } from 'next-test-utils'
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const nodeArgs = ['-r', join(appDir, '../../lib/react-17-require-hook.js')] const nodeArgs = ['-r', join(appDir, '../../lib/react-channel-require-hook.js')]
let appPort let appPort
let app let app
...@@ -13,10 +13,12 @@ describe('Custom error page exception', () => { ...@@ -13,10 +13,12 @@ describe('Custom error page exception', () => {
beforeAll(async () => { beforeAll(async () => {
await nextBuild(appDir, undefined, { await nextBuild(appDir, undefined, {
nodeArgs, nodeArgs,
env: { __NEXT_REACT_CHANNEL: '17' },
}) })
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort, { app = await nextStart(appDir, appPort, {
nodeArgs, nodeArgs,
env: { __NEXT_REACT_CHANNEL: '17' },
}) })
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
......
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
} from 'next-test-utils' } from 'next-test-utils'
const appDir = __dirname const appDir = __dirname
const nodeArgs = ['-r', join(appDir, '../../lib/react-17-require-hook.js')] const nodeArgs = ['-r', join(appDir, '../../lib/react-channel-require-hook.js')]
const reactDomPackagePah = join(appDir, 'node_modules/react-dom') const reactDomPackagePah = join(appDir, 'node_modules/react-dom')
const nextConfig = new File(join(appDir, 'next.config.js')) const nextConfig = new File(join(appDir, 'next.config.js'))
const documentPage = new File(join(appDir, 'pages/_document.js')) const documentPage = new File(join(appDir, 'pages/_document.js'))
...@@ -38,11 +38,9 @@ Document.getInitialProps = (ctx) => { ...@@ -38,11 +38,9 @@ Document.getInitialProps = (ctx) => {
} }
` `
function writeNextConfig(config, reactVersion = 17) { function writeNextConfig(config) {
const content = ` const content = `
const path = require('path') module.exports = { experimental: ${JSON.stringify(config)} }
const withReact = ${reactVersion} === 18 ? v => v : require(path.join(__dirname, '../../lib/with-react-17.js'))
module.exports = withReact({ experimental: ${JSON.stringify(config)} })
` `
nextConfig.write(content) nextConfig.write(content)
} }
...@@ -52,7 +50,11 @@ describe('Invalid react 18 webpack config', () => { ...@@ -52,7 +50,11 @@ describe('Invalid react 18 webpack config', () => {
writeNextConfig({ writeNextConfig({
runtime: 'experimental-edge', runtime: 'experimental-edge',
}) })
const { stderr } = await nextBuild(appDir, [], { stderr: true, nodeArgs }) const { stderr } = await nextBuild(appDir, [], {
stderr: true,
nodeArgs,
env: { __NEXT_REACT_CHANNEL: '17' },
})
nextConfig.restore() nextConfig.restore()
expect(stderr).toContain( expect(stderr).toContain(
...@@ -79,6 +81,9 @@ describe('React 17 with React 18 config', () => { ...@@ -79,6 +81,9 @@ describe('React 17 with React 18 config', () => {
const { stderr, code } = await nextBuild(appDir, [], { const { stderr, code } = await nextBuild(appDir, [], {
stderr: true, stderr: true,
nodeArgs, nodeArgs,
env: {
__NEXT_REACT_CHANNEL: '17',
},
}) })
expect(stderr).toContain( expect(stderr).toContain(
'Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense' 'Invalid suspense option usage in next/dynamic. Read more: https://nextjs.org/docs/messages/invalid-dynamic-suspense'
...@@ -101,12 +106,9 @@ const documentSuite = { ...@@ -101,12 +106,9 @@ const documentSuite = {
} }
}, },
beforeAll: async () => { beforeAll: async () => {
writeNextConfig( writeNextConfig({
{ serverComponents: true,
serverComponents: true, })
},
18
)
documentPage.write(documentWithGip) documentPage.write(documentWithGip)
await fs.rename(indexPage, indexServerPage) await fs.rename(indexPage, indexServerPage)
}, },
...@@ -115,6 +117,9 @@ const documentSuite = { ...@@ -115,6 +117,9 @@ const documentSuite = {
nextConfig.restore() nextConfig.restore()
await fs.rename(indexServerPage, indexPage) await fs.rename(indexServerPage, indexPage)
}, },
env: {
__NEXT_REACT_CHANNEL: 'exp',
},
} }
runDevSuite('Invalid custom document with gip', appDir, documentSuite) runDevSuite('Invalid custom document with gip', appDir, documentSuite)
......
import Comp from '../components/styled-jsx.client'
export default function Page() {
return (
<div>
<Comp />
</div>
)
}
{
"/_app": "pages/_app.js",
"/_error": "pages/_error.js",
"/edge-rsc": "pages/edge-rsc.js",
"/static": "pages/static.js",
"/node-rsc": "pages/node-rsc.js",
"/node": "pages/node.js",
"/edge": "pages/edge.js"
}
...@@ -100,6 +100,9 @@ const nodejsRuntimeBasicSuite = { ...@@ -100,6 +100,9 @@ const nodejsRuntimeBasicSuite = {
afterAll: () => { afterAll: () => {
nextConfig.restore() nextConfig.restore()
}, },
env: {
__NEXT_REACT_CHANNEL: 'exp',
},
} }
runDevSuite('Node.js runtime', appDir, nodejsRuntimeBasicSuite) runDevSuite('Node.js runtime', appDir, nodejsRuntimeBasicSuite)
......
...@@ -767,6 +767,7 @@ function runSuite(suiteName, context, options) { ...@@ -767,6 +767,7 @@ function runSuite(suiteName, context, options) {
const { stdout, stderr, code } = await nextBuild(appDir, [], { const { stdout, stderr, code } = await nextBuild(appDir, [], {
stderr: true, stderr: true,
stdout: true, stdout: true,
env: options.env || {},
}) })
context.stdout = stdout context.stdout = stdout
context.stderr = stderr context.stderr = stderr
...@@ -774,12 +775,14 @@ function runSuite(suiteName, context, options) { ...@@ -774,12 +775,14 @@ function runSuite(suiteName, context, options) {
context.server = await nextStart(context.appDir, context.appPort, { context.server = await nextStart(context.appDir, context.appPort, {
onStderr, onStderr,
onStdout, onStdout,
env: options.env || {},
}) })
} else if (env === 'dev') { } else if (env === 'dev') {
context.appPort = await findPort() context.appPort = await findPort()
context.server = await launchApp(context.appDir, context.appPort, { context.server = await launchApp(context.appDir, context.appPort, {
onStderr, onStderr,
onStdout, onStdout,
env: options.env || {},
}) })
} }
}) })
...@@ -797,7 +800,7 @@ function runSuite(suiteName, context, options) { ...@@ -797,7 +800,7 @@ function runSuite(suiteName, context, options) {
* *
* @param {string} suiteName * @param {string} suiteName
* @param {string} appDir * @param {string} appDir
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options * @param {{beforeAll?: Function; afterAll?: Function; runTests: Function; env?: Record<string, string>}} options
*/ */
export function runDevSuite(suiteName, appDir, options) { export function runDevSuite(suiteName, appDir, options) {
return runSuite(suiteName, { appDir, env: 'dev' }, options) return runSuite(suiteName, { appDir, env: 'dev' }, options)
...@@ -807,7 +810,7 @@ export function runDevSuite(suiteName, appDir, options) { ...@@ -807,7 +810,7 @@ export function runDevSuite(suiteName, appDir, options) {
* *
* @param {string} suiteName * @param {string} suiteName
* @param {string} appDir * @param {string} appDir
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options * @param {{beforeAll?: Function; afterAll?: Function; runTests: Function; env?: Record<string, string>}} options
*/ */
export function runProdSuite(suiteName, appDir, options) { export function runProdSuite(suiteName, appDir, options) {
return runSuite(suiteName, { appDir, env: 'prod' }, options) return runSuite(suiteName, { appDir, env: 'prod' }, options)
......
const mod = require('module') const mod = require('module')
// The value will be '17' or 'exp' to alias the actual react channel
const reactVersion = process.env.__NEXT_REACT_CHANNEL
const reactDir = `react-${reactVersion}`
const reactDomDir = `react-dom-${reactVersion}`
const hookPropertyMap = new Map([ const hookPropertyMap = new Map([
['react', 'react-17'], ['react', reactDir],
['react/jsx-runtime', 'react-17/jsx-runtime'], ['react/package.json', `${reactDir}/package.json`],
['react/jsx-dev-runtime', 'react-17/jsx-dev-runtime'], ['react/jsx-runtime', `${reactDir}/jsx-runtime`],
['react-dom', 'react-dom-17'], ['react/jsx-dev-runtime', `${reactDir}/jsx-dev-runtime`],
['react-dom/server', 'react-dom-17/server'], ['react-dom', `${reactDomDir}`],
['react-dom/server.browser', 'react-dom-17/server.browser'], ['react-dom/package.json', `${reactDomDir}/package.json`],
['react-dom/client', `${reactDomDir}/client`],
['react-dom/server', `${reactDomDir}/server`],
['react-dom/server.browser', `${reactDomDir}/server.browser`],
]) ])
const resolveFilename = mod._resolveFilename const resolveFilename = mod._resolveFilename
......
module.exports = function withReact17(config) {
config.webpack = (webpackConfig) => {
const { alias } = webpackConfig.resolve
// Use react 18
alias['react'] = 'react-17'
alias['react-dom'] = 'react-dom-17'
alias['react-dom/client'] = 'react-dom-17/client'
alias['react-dom/server'] = 'react-dom-17/server'
alias['react-dom/server.browser'] = 'react-dom-17/server.browser'
return webpackConfig
}
return config
}
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