rsnext/packages/next/webpack.config.js
Josh Story 23955574ae
Update React from 60a927d04 to 2bc7d336a (#61522)
Updates React from 60a927d04 to 2bc7d336a

Also updates aliases for `react.shared-subset` to `react.react-server`

### React upstream changes

- https://github.com/facebook/react/pull/28250
- https://github.com/facebook/react/pull/28225
- https://github.com/facebook/react/pull/28123
- https://github.com/facebook/react/pull/28240
- https://github.com/facebook/react/pull/28239
- https://github.com/facebook/react/pull/28245
- https://github.com/facebook/react/pull/28244
- https://github.com/facebook/react/pull/28238
- https://github.com/facebook/react/pull/28235
- https://github.com/facebook/react/pull/28221
- https://github.com/facebook/react/pull/28215
- https://github.com/facebook/react/pull/28214
- https://github.com/facebook/react/pull/28213
- https://github.com/facebook/react/pull/28212
- https://github.com/facebook/react/pull/28211
- https://github.com/facebook/react/pull/28247
- https://github.com/facebook/react/pull/28210
- https://github.com/facebook/react/pull/28186
- https://github.com/facebook/react/pull/28232
- https://github.com/facebook/react/pull/28169
- https://github.com/facebook/react/pull/28177
- https://github.com/facebook/react/pull/28170
- https://github.com/facebook/react/pull/28168
- https://github.com/facebook/react/pull/28122
- https://github.com/facebook/react/pull/27982
- https://github.com/facebook/react/pull/28217
- https://github.com/facebook/react/pull/28223
- https://github.com/facebook/react/pull/28208
- https://github.com/facebook/react/pull/28209
- https://github.com/facebook/react/pull/28200
- https://github.com/facebook/react/pull/28199
- https://github.com/facebook/react/pull/28198
- https://github.com/facebook/react/pull/28197
- https://github.com/facebook/react/pull/28196
- https://github.com/facebook/react/pull/28194
- https://github.com/facebook/react/pull/28192
- https://github.com/facebook/react/pull/28191
- https://github.com/facebook/react/pull/28182
- https://github.com/facebook/react/pull/28181
- https://github.com/facebook/react/pull/28180
- https://github.com/facebook/react/pull/28178
- https://github.com/facebook/react/pull/28201
- https://github.com/facebook/react/pull/28176
- https://github.com/facebook/react/pull/28162
- https://github.com/facebook/react/pull/28131
- https://github.com/facebook/react/pull/28190
- https://github.com/facebook/react/pull/28172
- https://github.com/facebook/react/pull/28171
- https://github.com/facebook/react/pull/28173
- https://github.com/facebook/react/pull/28174
- https://github.com/facebook/react/pull/28175
- https://github.com/facebook/react/pull/28136
- https://github.com/facebook/react/pull/28135
- https://github.com/facebook/react/pull/28134
- https://github.com/facebook/react/pull/28133
- https://github.com/facebook/react/pull/28132
- https://github.com/facebook/react/pull/28130
- https://github.com/facebook/react/pull/28202
- https://github.com/facebook/react/pull/28102
- https://github.com/facebook/react/pull/28161
- https://github.com/facebook/react/pull/28193
- https://github.com/facebook/react/pull/28195
- https://github.com/facebook/react/pull/28189
- https://github.com/facebook/react/pull/28160
- https://github.com/facebook/react/pull/28096
- https://github.com/facebook/react/pull/28183
- https://github.com/facebook/react/pull/28125
- https://github.com/facebook/react/pull/28157
- https://github.com/facebook/react/pull/28115
- https://github.com/facebook/react/pull/28124
- https://github.com/facebook/react/pull/28163
- https://github.com/facebook/react/pull/28164
- https://github.com/facebook/react/pull/28150
- https://github.com/facebook/react/pull/28159
- https://github.com/facebook/react/pull/28069
- https://github.com/facebook/react/pull/28110
- https://github.com/facebook/react/pull/28148
- https://github.com/facebook/react/pull/28116
- https://github.com/facebook/react/pull/28099
- https://github.com/facebook/react/pull/28100
- https://github.com/facebook/react/pull/28147
- https://github.com/facebook/react/pull/28128
- https://github.com/facebook/react/pull/28126
- https://github.com/facebook/react/pull/28139
- https://github.com/facebook/react/pull/28140
- https://github.com/facebook/react/pull/28141
- https://github.com/facebook/react/pull/28142
- https://github.com/facebook/react/pull/28113
- https://github.com/facebook/react/pull/28129
- https://github.com/facebook/react/pull/28114
- https://github.com/facebook/react/pull/28053
- https://github.com/facebook/react/pull/28091
- https://github.com/facebook/react/pull/28087
- https://github.com/facebook/react/pull/28112
- https://github.com/facebook/react/pull/28086
- https://github.com/facebook/react/pull/28101
- https://github.com/facebook/react/pull/28106
- https://github.com/facebook/react/pull/28117
- https://github.com/facebook/react/pull/28118
- https://github.com/facebook/react/pull/28105
- https://github.com/facebook/react/pull/27883
- https://github.com/facebook/react/pull/28111
- https://github.com/facebook/react/pull/28095
- https://github.com/facebook/react/pull/28108
- https://github.com/facebook/react/pull/28090
- https://github.com/facebook/react/pull/28089
- https://github.com/facebook/react/pull/28076
- https://github.com/facebook/react/pull/28074
- https://github.com/facebook/react/pull/28103
- https://github.com/facebook/react/pull/28098
- https://github.com/facebook/react/pull/28097
- https://github.com/facebook/react/pull/28068
- https://github.com/facebook/react/pull/28093
- https://github.com/facebook/react/pull/28094
- https://github.com/facebook/react/pull/28073
- https://github.com/facebook/react/pull/28084
- https://github.com/facebook/react/pull/28063
- https://github.com/facebook/react/pull/28085
- https://github.com/facebook/react/pull/28083
- https://github.com/facebook/react/pull/28065
- https://github.com/facebook/react/pull/28061
- https://github.com/facebook/react/pull/28077
- https://github.com/facebook/react/pull/28075
- https://github.com/facebook/react/pull/28078
- https://github.com/facebook/react/pull/28050
- https://github.com/facebook/react/pull/28011
- https://github.com/facebook/react/pull/28055
- https://github.com/facebook/react/pull/28066
- https://github.com/facebook/react/pull/28067
- https://github.com/facebook/react/pull/28010
- https://github.com/facebook/react/pull/27993
- https://github.com/facebook/react/pull/28052
- https://github.com/facebook/react/pull/28060
- https://github.com/facebook/react/pull/28059
- https://github.com/facebook/react/pull/28034
- https://github.com/facebook/react/pull/28033
- https://github.com/facebook/react/pull/28004
- https://github.com/facebook/react/pull/28051
- https://github.com/facebook/react/pull/28012
- https://github.com/facebook/react/pull/28001
- https://github.com/facebook/react/pull/28002
- https://github.com/facebook/react/pull/27995
- https://github.com/facebook/react/pull/28006
- https://github.com/facebook/react/pull/28005
- https://github.com/facebook/react/pull/28007
- https://github.com/facebook/react/pull/28008
- https://github.com/facebook/react/pull/28009
- https://github.com/facebook/react/pull/28000
- https://github.com/facebook/react/pull/28003
- https://github.com/facebook/react/pull/27997
- https://github.com/facebook/react/pull/27240
- https://github.com/facebook/react/pull/27977
- https://github.com/facebook/react/pull/27940
- https://github.com/facebook/react/pull/27939
- https://github.com/facebook/react/pull/28090
- https://github.com/facebook/react/pull/28089
- https://github.com/facebook/react/pull/28076
- https://github.com/facebook/react/pull/28074
- https://github.com/facebook/react/pull/28103
- https://github.com/facebook/react/pull/28098
- https://github.com/facebook/react/pull/28097
- https://github.com/facebook/react/pull/28068
- https://github.com/facebook/react/pull/28093
- https://github.com/facebook/react/pull/28094
- https://github.com/facebook/react/pull/28073
- https://github.com/facebook/react/pull/28084
- https://github.com/facebook/react/pull/28063
- https://github.com/facebook/react/pull/28085
- https://github.com/facebook/react/pull/28083
- https://github.com/facebook/react/pull/28065
- https://github.com/facebook/react/pull/28061
- https://github.com/facebook/react/pull/28077
- https://github.com/facebook/react/pull/28075
- https://github.com/facebook/react/pull/28078
- https://github.com/facebook/react/pull/28050
- https://github.com/facebook/react/pull/28011
- https://github.com/facebook/react/pull/28055
- https://github.com/facebook/react/pull/28066
- https://github.com/facebook/react/pull/28067
- https://github.com/facebook/react/pull/28010
- https://github.com/facebook/react/pull/27993
- https://github.com/facebook/react/pull/28052
- https://github.com/facebook/react/pull/28060
- https://github.com/facebook/react/pull/28059
- https://github.com/facebook/react/pull/28034
- https://github.com/facebook/react/pull/28033
- https://github.com/facebook/react/pull/28004
- https://github.com/facebook/react/pull/28051
- https://github.com/facebook/react/pull/28012
- https://github.com/facebook/react/pull/28001
- https://github.com/facebook/react/pull/28002
- https://github.com/facebook/react/pull/27995
- https://github.com/facebook/react/pull/28006
- https://github.com/facebook/react/pull/28005
- https://github.com/facebook/react/pull/28007
- https://github.com/facebook/react/pull/28008
- https://github.com/facebook/react/pull/28009
- https://github.com/facebook/react/pull/28000
- https://github.com/facebook/react/pull/28003
- https://github.com/facebook/react/pull/27997
- https://github.com/facebook/react/pull/27240
- https://github.com/facebook/react/pull/27977
- https://github.com/facebook/react/pull/27940
- https://github.com/facebook/react/pull/27939

Closes NEXT-2331
2024-02-05 18:22:27 -08:00

292 lines
9.9 KiB
JavaScript

const webpack = require('webpack')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const pagesExternals = [
'react',
'react/package.json',
'react/jsx-runtime',
'react/jsx-dev-runtime',
'react-dom',
'react-dom/package.json',
'react-dom/client',
'react-dom/server',
'react-dom/server.browser',
'react-dom/server.edge',
'react-server-dom-webpack/client',
'react-server-dom-webpack/client.edge',
'react-server-dom-webpack/server.edge',
'react-server-dom-webpack/server.node',
]
const appExternals = [
// Externalize the react-dom/server legacy implementation outside of the runtime.
// If users are using them and imported from 'react-dom/server' they will get the external asset bundled.
'next/dist/compiled/react-dom/cjs/react-dom-server-legacy.browser.development.js',
'next/dist/compiled/react-dom/cjs/react-dom-server-legacy.browser.production.min.js',
'next/dist/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.development.js',
'next/dist/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.production.min.js',
]
function makeAppAliases(reactChannel = '') {
return {
react$: `next/dist/compiled/react${reactChannel}`,
'react/react.react-server$': `next/dist/compiled/react${reactChannel}/react.react-server`,
'react-dom/server-rendering-stub$': `next/dist/compiled/react-dom${reactChannel}/server-rendering-stub`,
'react-dom$': `next/dist/compiled/react-dom${reactChannel}/server-rendering-stub`,
'react/jsx-runtime$': `next/dist/compiled/react${reactChannel}/jsx-runtime`,
'react/jsx-dev-runtime$': `next/dist/compiled/react${reactChannel}/jsx-dev-runtime`,
'react-dom/client$': `next/dist/compiled/react-dom${reactChannel}/client`,
'react-dom/server$': `next/dist/compiled/react-dom${reactChannel}/server`,
'react-dom/static$': `next/dist/compiled/react-dom-experimental/static`,
'react-dom/static.edge$': `next/dist/compiled/react-dom-experimental/static.edge`,
'react-dom/static.browser$': `next/dist/compiled/react-dom-experimental/static.browser`,
// optimizations to ignore the legacy build of react-dom/server in `server.browser` build
'react-dom/server.edge$': `next/dist/build/webpack/alias/react-dom-server-edge${reactChannel}.js`,
// In Next.js runtime only use react-dom/server.edge
'react-dom/server.browser$': 'react-dom/server.edge',
// react-server-dom-webpack alias
'react-server-dom-turbopack/client$': `next/dist/compiled/react-server-dom-turbopack${reactChannel}/client`,
'react-server-dom-turbopack/client.edge$': `next/dist/compiled/react-server-dom-turbopack${reactChannel}/client.edge`,
'react-server-dom-turbopack/server.edge$': `next/dist/compiled/react-server-dom-turbopack${reactChannel}/server.edge`,
'react-server-dom-turbopack/server.node$': `next/dist/compiled/react-server-dom-turbopack${reactChannel}/server.node`,
'react-server-dom-webpack/client$': `next/dist/compiled/react-server-dom-webpack${reactChannel}/client`,
'react-server-dom-webpack/client.edge$': `next/dist/compiled/react-server-dom-webpack${reactChannel}/client.edge`,
'react-server-dom-webpack/server.edge$': `next/dist/compiled/react-server-dom-webpack${reactChannel}/server.edge`,
'react-server-dom-webpack/server.node$': `next/dist/compiled/react-server-dom-webpack${reactChannel}/server.node`,
}
}
const appAliases = makeAppAliases()
const appExperimentalAliases = makeAppAliases('-experimental')
const sharedExternals = [
'styled-jsx',
'styled-jsx/style',
'@opentelemetry/api',
'next/dist/compiled/@next/react-dev-overlay/dist/middleware',
'next/dist/compiled/@ampproject/toolbox-optimizer',
'next/dist/compiled/edge-runtime',
'next/dist/compiled/@edge-runtime/ponyfill',
'next/dist/compiled/undici',
'next/dist/compiled/raw-body',
'next/dist/server/capsize-font-metrics.json',
'critters',
'next/dist/compiled/node-html-parser',
'next/dist/compiled/compression',
'next/dist/compiled/jsonwebtoken',
'next/dist/compiled/@opentelemetry/api',
'next/dist/compiled/@mswjs/interceptors/ClientRequest',
'next/dist/compiled/ws',
]
const externalsMap = {
'./web/sandbox': 'next/dist/server/web/sandbox',
}
const externalsRegexMap = {
'(.*)trace/tracer$': 'next/dist/server/lib/trace/tracer',
}
const bundleTypes = {
app: {
'app-page': path.join(
__dirname,
'dist/esm/server/future/route-modules/app-page/module.js'
),
'app-route': path.join(
__dirname,
'dist/esm/server/future/route-modules/app-route/module.js'
),
},
pages: {
pages: path.join(
__dirname,
'dist/esm/server/future/route-modules/pages/module.js'
),
'pages-api': path.join(
__dirname,
'dist/esm/server/future/route-modules/pages-api/module.js'
),
},
server: {
server: path.join(__dirname, 'dist/esm/server/next-server.js'),
},
}
module.exports = ({ dev, turbo, bundleType, experimental }) => {
const externalHandler = ({ context, request, getResolve }, callback) => {
;(async () => {
if (request.endsWith('.external')) {
const resolve = getResolve()
const resolved = await resolve(context, request)
const relative = path.relative(
path.join(__dirname, '..'),
resolved.replace('esm' + path.sep, '')
)
callback(null, `commonjs ${relative}`)
} else {
const regexMatch = Object.keys(externalsRegexMap).find((regex) =>
new RegExp(regex).test(request)
)
if (regexMatch) {
return callback(null, 'commonjs ' + externalsRegexMap[regexMatch])
}
callback()
}
})()
}
/** @type {webpack.Configuration} */
return {
entry: bundleTypes[bundleType],
target: 'node',
mode: 'production',
output: {
path: path.join(__dirname, 'dist/compiled/next-server'),
filename: `[name]${turbo ? '-turbo' : ''}${
experimental ? '-experimental' : ''
}.runtime.${dev ? 'dev' : 'prod'}.js`,
libraryTarget: 'commonjs2',
},
devtool: 'source-map',
optimization: {
moduleIds: 'named',
minimize: true,
concatenateModules: true,
minimizer: [
new TerserPlugin({
minify: TerserPlugin.swcMinify,
terserOptions: {
compress: {
dead_code: true,
// Zero means no limit.
passes: 0,
},
format: {
preamble: '',
},
},
}),
],
},
plugins: [
new webpack.DefinePlugin({
'typeof window': JSON.stringify('undefined'),
'process.env.NEXT_MINIMAL': JSON.stringify('true'),
'this.serverOptions.experimentalTestProxy': JSON.stringify(false),
'this.minimalMode': JSON.stringify(true),
'this.renderOpts.dev': JSON.stringify(dev),
'process.env.NODE_ENV': JSON.stringify(
dev ? 'development' : 'production'
),
'process.env.__NEXT_EXPERIMENTAL_REACT': JSON.stringify(
experimental ? true : false
),
'process.env.NEXT_RUNTIME': JSON.stringify('nodejs'),
...(!dev ? { 'process.env.TURBOPACK': JSON.stringify(turbo) } : {}),
}),
!!process.env.ANALYZE &&
new BundleAnalyzerPlugin({
analyzerPort: calculateUniquePort(
dev,
turbo,
experimental,
bundleType
),
openAnalyzer: false,
...(process.env.CI
? {
analyzerMode: 'static',
reportFilename: path.join(
__dirname,
`dist/compiled/next-server/report.${dev ? 'dev' : 'prod'}-${
turbo ? 'turbo' : 'webpack'
}-${
experimental ? 'experimental' : 'stable'
}-${bundleType}.html`
),
}
: {}),
}),
].filter(Boolean),
stats: {
optimizationBailout: true,
},
resolve: {
alias:
bundleType === 'app'
? experimental
? appExperimentalAliases
: appAliases
: {},
},
module: {
rules: [
{
include: /[\\/]react-server\.node/,
layer: 'react-server',
},
{
include: /vendored[\\/]rsc[\\/]entrypoints/,
resolve: {
conditionNames: ['react-server', '...'],
alias: {
react$: `next/dist/compiled/react${
experimental ? '-experimental' : ''
}/react.react-server`,
'next/dist/compiled/react$': `next/dist/compiled/react${
experimental ? '-experimental' : ''
}/react.react-server`,
},
},
layer: 'react-server',
},
{
issuerLayer: 'react-server',
resolve: {
conditionNames: ['react-server', '...'],
alias: {
react$: `next/dist/compiled/react${
experimental ? '-experimental' : ''
}/react.react-server`,
'next/dist/compiled/react$': `next/dist/compiled/react${
experimental ? '-experimental' : ''
}/react.react-server`,
},
},
},
],
},
externals: [
...sharedExternals,
...(bundleType === 'pages' ? pagesExternals : appExternals),
externalsMap,
externalHandler,
],
experiments: {
layers: true,
},
}
}
function calculateUniquePort(dev, turbo, experimental, bundleType) {
const devOffset = dev ? 1000 : 0
const turboOffset = turbo ? 200 : 0
const experimentalOffset = experimental ? 40 : 0
let bundleTypeOffset
switch (bundleType) {
case 'app':
bundleTypeOffset = 1
break
case 'pages':
bundleTypeOffset = 2
break
default:
bundleTypeOffset = 3
}
return 8888 + devOffset + turboOffset + experimentalOffset + bundleTypeOffset
}