rsnext/test/lib/react-channel-require-hook.js
Jiachi Liu bfaffbdd3f
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
2022-07-01 23:57:45 +00:00

26 lines
1,011 B
JavaScript

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([
['react', reactDir],
['react/package.json', `${reactDir}/package.json`],
['react/jsx-runtime', `${reactDir}/jsx-runtime`],
['react/jsx-dev-runtime', `${reactDir}/jsx-dev-runtime`],
['react-dom', `${reactDomDir}`],
['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
mod._resolveFilename = function (request, parent, isMain, options) {
const hookResolved = hookPropertyMap.get(request)
if (hookResolved) request = hookResolved
return resolveFilename.call(mod, request, parent, isMain, options)
}