rsnext/packages/next/build/webpack/require-hook.ts
Jiachi Liu b0f87fbc7c
Bundle ssr client layer excepts react externals (#41606)
Bundle the ssr client layer for RSC, this solves the problem when
there's an esm package is using on client components, but esm imports
the installed react instead of the built-in react version since esm
imports is not intercepted by require hook.

After bundling the ssr client layer and treating react as externals, now
react compiles as cjs externals and could be intercepted by require
hook, other code are bundled together which can also get optimized.

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

Co-authored-by: Shu Ding <g@shud.in>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2022-10-22 16:33:51 -07:00

75 lines
2.1 KiB
TypeScript

// sync injects a hook for webpack and webpack/... requires to use the internal ncc webpack version
// this is in order for userland plugins to attach to the same webpack instance as next.js
// the individual compiled modules are as defined for the compilation in bundles/webpack/packages/*
const hookPropertyMap = new Map()
let initialized = false
function setupResolve() {
if (initialized) {
return
}
initialized = true
const mod = require('module')
const resolveFilename = mod._resolveFilename
mod._resolveFilename = function (
request: string,
parent: any,
isMain: boolean,
options: any
) {
const hookResolved = hookPropertyMap.get(request)
if (hookResolved) request = hookResolved
return resolveFilename.call(mod, request, parent, isMain, options)
}
}
export function setRequireOverrides(aliases: [string, string][]) {
for (const [key, value] of aliases) {
hookPropertyMap.set(key, value)
}
}
export function loadRequireHook(aliases: [string, string][] = []) {
const defaultAliases = [
...aliases,
// Use `require.resolve` explicitly to make them statically analyzable
['styled-jsx', require.resolve('styled-jsx')],
['styled-jsx/style', require.resolve('styled-jsx/style')],
['styled-jsx/style', require.resolve('styled-jsx/style')],
] as [string, string][]
setRequireOverrides(defaultAliases)
setupResolve()
}
export function overrideBuiltInReactPackages() {
setRequireOverrides([
['react', require.resolve('next/dist/compiled/react')],
[
'react/jsx-runtime',
require.resolve('next/dist/compiled/react/jsx-runtime'),
],
[
'react/jsx-dev-runtime',
require.resolve('next/dist/compiled/react/jsx-dev-runtime'),
],
[
'react-dom',
require.resolve('next/dist/compiled/react-dom/server-rendering-stub'),
],
[
'react-dom/client',
require.resolve('next/dist/compiled/react-dom/client'),
],
[
'react-dom/server',
require.resolve('next/dist/compiled/react-dom/server'),
],
[
'react-dom/server.browser',
require.resolve('next/dist/compiled/react-dom/server.browser'),
],
])
}