From 9d114d5319151c2420ae225357a64fba92729c9e Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Thu, 14 Sep 2023 10:56:20 +0200 Subject: [PATCH] toast --- packages/next/src/build/index.ts | 16 +- packages/next/src/build/webpack-config.ts | 233 +++++++++++------- .../router-reducer/fetch-server-response.ts | 10 +- .../reducers/server-action-reducer.ts | 15 +- ...t-dom-server-legacy.browser.development.js | 2 +- ...om-server-legacy.browser.production.min.js | 2 +- ...eact-dom-server-legacy.node.development.js | 2 +- ...t-dom-server-legacy.node.production.min.js | 2 +- ...t-dom-server-rendering-stub.development.js | 2 +- ...om-server-rendering-stub.production.min.js | 2 +- .../react-dom-server.browser.development.js | 2 +- ...react-dom-server.browser.production.min.js | 2 +- .../cjs/react-dom-server.edge.development.js | 2 +- .../react-dom-server.edge.production.min.js | 2 +- .../cjs/react-dom-server.node.development.js | 2 +- .../react-dom-server.node.production.min.js | 2 +- .../react-dom-static.browser.development.js | 2 +- ...react-dom-static.browser.production.min.js | 2 +- .../cjs/react-dom-static.edge.development.js | 2 +- .../react-dom-static.edge.production.min.js | 2 +- .../cjs/react-dom-static.node.development.js | 2 +- .../react-dom-static.node.production.min.js | 2 +- .../react-dom-unstable_testing.development.js | 4 +- ...act-dom-unstable_testing.production.min.js | 2 +- .../cjs/react-dom.development.js | 4 +- .../cjs/react-dom.production.min.js | 2 +- .../cjs/react-dom.profiling.min.js | 2 +- ...t-dom-server-legacy.browser.development.js | 2 +- ...om-server-legacy.browser.production.min.js | 2 +- ...eact-dom-server-legacy.node.development.js | 2 +- ...t-dom-server-legacy.node.production.min.js | 2 +- .../react-dom-server.browser.development.js | 2 +- ...react-dom-server.browser.production.min.js | 2 +- .../cjs/react-dom-server.edge.development.js | 2 +- .../react-dom-server.edge.production.min.js | 2 +- .../cjs/react-dom-server.node.development.js | 2 +- .../react-dom-server.node.production.min.js | 2 +- .../react-dom/cjs/react-dom.development.js | 4 +- .../react-dom/cjs/react-dom.production.min.js | 2 +- .../react-dom/cjs/react-dom.profiling.min.js | 2 +- .../cjs/react-jsx-dev-runtime.development.js | 2 +- .../cjs/react-jsx-runtime.development.js | 2 +- .../cjs/react-jsx-runtime.production.min.js | 2 +- .../cjs/react-jsx-runtime.profiling.min.js | 2 +- .../cjs/react-jsx-dev-runtime.development.js | 2 +- .../cjs/react-jsx-runtime.development.js | 2 +- .../cjs/react-jsx-runtime.production.min.js | 2 +- .../cjs/react-jsx-runtime.profiling.min.js | 2 +- packages/next/src/lib/constants.ts | 5 +- packages/next/src/server/esm-loader.mts | 4 +- .../future/route-modules/app-page/module.ts | 13 +- .../app-page/vendored/rsc/entrypoints.ts | 29 +++ .../app-page/vendored/rsc/react-dom.ts | 1 + .../react-server-dom-webpack-server-edge.ts | 3 + .../react-server-dom-webpack-server-node.ts | 3 + .../app-page/vendored/rsc/react.ts | 1 + .../app-page/vendored/shared/entrypoints.ts | 4 + .../vendored/shared/react-jsx-dev-runtime.ts | 3 + .../vendored/shared/react-jsx-runtime.ts | 3 + .../app-page/vendored/ssr/entrypoints.ts | 23 ++ .../vendored/ssr/react-dom-server-edge.ts | 3 + .../app-page/vendored/ssr/react-dom.ts | 1 + .../react-server-dom-webpack-client-edge.ts | 3 + .../app-page/vendored/ssr/react.ts | 1 + packages/next/src/server/import-overrides.ts | 122 ++++----- packages/next/src/server/require-hook.js | 4 +- packages/next/taskfile.js | 161 +++++++++--- packages/next/types/misc.d.ts | 5 + packages/next/webpack.config.js | 157 ++++++++++-- 69 files changed, 638 insertions(+), 279 deletions(-) create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/rsc/entrypoints.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-dom.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-edge.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-node.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/rsc/react.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/shared/entrypoints.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-dev-runtime.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-runtime.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/ssr/entrypoints.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom-server-edge.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-server-dom-webpack-client-edge.ts create mode 100644 packages/next/src/server/future/route-modules/app-page/vendored/ssr/react.ts diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index 16944bde6fc18..b1cb3fffed59a 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -146,9 +146,9 @@ import { generateInterceptionRoutesRewrites } from '../lib/generate-interception import { buildDataRoute } from '../server/lib/router-utils/build-data-route' import { - baseOverrides, + // baseOverrides, defaultOverrides, - experimentalOverrides, + // experimentalOverrides, } from '../server/import-overrides' import { initialize as initializeIncrementalCache } from '../server/lib/incremental-cache-server' import { nodeFs } from '../server/lib/node-fs-methods' @@ -2107,12 +2107,12 @@ export default async function build( ) const sharedEntriesSet = [ - ...Object.values(baseOverrides).map((override) => - require.resolve(override) - ), - ...Object.values(experimentalOverrides).map((override) => - require.resolve(override) - ), + // ...Object.values(baseOverrides).map((override) => + // require.resolve(override) + // ), + // ...Object.values(experimentalOverrides).map((override) => + // require.resolve(override) + // ), ...(config.experimental.turbotrace ? [] : Object.keys(defaultOverrides).map((value) => diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index a9273b46bdc2f..7123a3c94d834 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -17,6 +17,7 @@ import { RSC_ACTION_CLIENT_WRAPPER_ALIAS, RSC_ACTION_VALIDATE_ALIAS, WEBPACK_RESOURCE_QUERIES, + WebpackLayerName, } from '../lib/constants' import { CustomRoutes } from '../lib/load-custom-routes.js' import { isEdgeRuntime } from '../lib/is-edge-runtime' @@ -85,7 +86,7 @@ const NEXT_PROJECT_ROOT_DIST_CLIENT = path.join( 'client' ) -const isWebpackServerLayer = (layer: string | null) => +const isWebpackServerLayer = (layer: WebpackLayerName | null) => Boolean(layer && WEBPACK_LAYERS.GROUP.server.includes(layer)) if (parseInt(React.version) < 18) { @@ -109,6 +110,11 @@ const optionalEsmPart = `((${pathSeparators}esm)?${pathSeparators})` const sharedRuntimeFileEnd = '(\\.shared-runtime(\\.js)?)$' const externalFileEnd = '(\\.external(\\.js)?)$' const nextDist = `next${pathSeparators}dist` +const genericRuntimeFileEnd = '(\\.[\\w-]+\\.runtime\\.[\\w-]+(\\.js)?)$' + +const genericRuntimePattern = new RegExp( + `${nextDist}.*${genericRuntimeFileEnd}` +) const sharedRuntimePattern = new RegExp( `${nextDist}${optionalEsmPart}.*${sharedRuntimeFileEnd}` @@ -386,39 +392,62 @@ function getReactProfilingInProduction() { function createRSCAliases( bundledReactChannel: string, opts: { + layer: WebpackLayerName & ('rsc' | 'ssr' | 'app-pages-browser') reactProductionProfiling: boolean - reactSharedSubset: boolean - reactDomServerRenderingStub: boolean + // reactSharedSubset: boolean + // reactDomServerRenderingStub: boolean reactServerCondition?: boolean } ) { - const alias: Record = { - react$: `next/dist/compiled/react${bundledReactChannel}`, - 'react-dom$': `next/dist/compiled/react-dom${bundledReactChannel}`, - 'react/jsx-runtime$': `next/dist/compiled/react${bundledReactChannel}/jsx-runtime`, - 'react/jsx-dev-runtime$': `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime`, - 'react-dom/client$': `next/dist/compiled/react-dom${bundledReactChannel}/client`, - 'react-dom/server$': `next/dist/compiled/react-dom${bundledReactChannel}/server`, - 'react-dom/server.edge$': `next/dist/compiled/react-dom${bundledReactChannel}/server.edge`, - 'react-dom/server.browser$': `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`, - 'react-server-dom-webpack/client$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client`, - 'react-server-dom-webpack/client.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`, - 'react-server-dom-webpack/server.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`, - 'react-server-dom-webpack/server.node$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`, - } + const alias: Record = + opts.layer === 'app-pages-browser' + ? { + react$: `next/dist/compiled/react${bundledReactChannel}`, + 'react-dom$': `next/dist/compiled/react-dom${bundledReactChannel}`, + 'react/jsx-runtime$': `next/dist/compiled/react${bundledReactChannel}/jsx-runtime`, + 'react/jsx-dev-runtime$': `next/dist/compiled/react${bundledReactChannel}/jsx-dev-runtime`, + 'react-dom/client$': `next/dist/compiled/react-dom${bundledReactChannel}/client`, + 'react-dom/server$': `next/dist/compiled/react-dom${bundledReactChannel}/server`, + 'react-dom/server.edge$': `next/dist/compiled/react-dom${bundledReactChannel}/server.edge`, + 'react-dom/server.browser$': `next/dist/compiled/react-dom${bundledReactChannel}/server.browser`, + 'react-server-dom-webpack/client$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client`, + 'react-server-dom-webpack/client.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/client.edge`, + 'react-server-dom-webpack/server.edge$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.edge`, + 'react-server-dom-webpack/server.node$': `next/dist/compiled/react-server-dom-webpack${bundledReactChannel}/server.node`, + } + : { + react$: `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react`, + 'react-dom$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-dom`, + 'react/jsx-runtime$': `next/dist/server/future/route-modules/app-page/vendored/shared/react-jsx-runtime`, + 'react/jsx-dev-runtime$': `next/dist/server/future/route-modules/app-page/vendored/shared/react-jsx-dev-runtime`, + 'react-dom/client$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-dom-client`, + 'react-dom/server$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-dom-server`, + 'react-dom/server.edge$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-dom-server-edge`, + 'react-dom/server.browser$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-dom-server-browser`, + 'react-server-dom-webpack/client$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-server-dom-webpack-client`, + 'react-server-dom-webpack/client.edge$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-server-dom-webpack-client-edge`, + 'react-server-dom-webpack/server.edge$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-server-dom-webpack-server-edge`, + 'react-server-dom-webpack/server.node$': `next/dist/server/future/route-modules/app-page/vendored/${opts.layer}/react-server-dom-webpack-server-node`, + } - if (opts.reactSharedSubset) { - alias[ - 'react$' - ] = `next/dist/compiled/react${bundledReactChannel}/react.shared-subset` - } - // Use server rendering stub for RSC - // x-ref: https://github.com/facebook/react/pull/25436 - if (opts.reactDomServerRenderingStub) { - alias[ - 'react-dom$' - ] = `next/dist/compiled/react-dom${bundledReactChannel}/server-rendering-stub` - } + // if (opts.reactSharedSubset) { + // // alias[ + // // 'react$' + // // ] = `next/dist/compiled/react${bundledReactChannel}/react.shared-subset` + // alias[ + // 'react$' + // ] = `next/dist/server/future/route-modules/app-page/vendored/react-shared-subset` + // } + // // Use server rendering stub for RSC + // // x-ref: https://github.com/facebook/react/pull/25436 + // if (opts.reactDomServerRenderingStub) { + // // alias[ + // // 'react-dom$' + // // ] = `next/dist/compiled/react-dom${bundledReactChannel}/server-rendering-stub` + // alias[ + // 'react-dom$' + // ] = `next/dist/server/future/route-modules/app-page/vendored/react-server-rendering-stub` + // } // Alias `server-only` and `client-only` modules to their server/client only, vendored versions. // These aliases are necessary if the user doesn't have those two packages installed manually. @@ -1130,9 +1159,11 @@ export default async function getBaseWebpackConfig( ...(hasAppDir ? createRSCAliases(bundledReactChannel, { - reactSharedSubset: false, - reactDomServerRenderingStub: false, + // reactSharedSubset: false, + // reactDomServerRenderingStub: false, reactProductionProfiling, + // browser: true, + layer: 'app-pages-browser', }) : {}), @@ -1313,7 +1344,7 @@ export default async function getBaseWebpackConfig( context: string, request: string, dependencyType: string, - layer: string | null, + layer: WebpackLayerName | null, getResolve: ( options: any ) => ( @@ -1338,35 +1369,37 @@ export default async function getBaseWebpackConfig( return `commonjs next/dist/lib/import-next-warning` } - const isAppLayer = [ - WEBPACK_LAYERS.reactServerComponents, - WEBPACK_LAYERS.serverSideRendering, - WEBPACK_LAYERS.appPagesBrowser, - WEBPACK_LAYERS.actionBrowser, - WEBPACK_LAYERS.appRouteHandler, - ].includes(layer!) - - if ( - request === 'react/jsx-dev-runtime' || - request === 'react/jsx-runtime' - ) { - if (isAppLayer) { - return `commonjs next/dist/compiled/${request.replace( - 'react', - 'react' + bundledReactChannel - )}` - } - return - } + const isAppLayer = ( + [ + WEBPACK_LAYERS.reactServerComponents, + WEBPACK_LAYERS.serverSideRendering, + WEBPACK_LAYERS.appPagesBrowser, + WEBPACK_LAYERS.actionBrowser, + WEBPACK_LAYERS.appRouteHandler, + ] as WebpackLayerName[] + ).includes(layer!) + + // if ( + // request === 'react/jsx-dev-runtime' || + // request === 'react/jsx-runtime' + // ) { + // if (isAppLayer) { + // return `commonjs next/dist/compiled/${request.replace( + // 'react', + // 'react' + bundledReactChannel + // )}` + // } + // return + // } // Special internal modules that must be bundled for Server Components. - if (layer === WEBPACK_LAYERS.reactServerComponents) { - // React needs to be bundled for Server Components so the special - // `react-server` export condition can be used. - if (reactPackagesRegex.test(request)) { - return - } - } + // if (layer === WEBPACK_LAYERS.reactServerComponents) { + // // React needs to be bundled for Server Components so the special + // // `react-server` export condition can be used. + // if (reactPackagesRegex.test(request)) { + // return + // } + // } // Relative requires don't need custom resolution, because they // are relative to requests we've already resolved here. @@ -1377,31 +1410,32 @@ export default async function getBaseWebpackConfig( return `commonjs ${request}` } - if (reactPackagesRegex.test(request)) { + if (reactPackagesRegex.test(request) && !isAppLayer) { // override react-dom to server-rendering-stub for server - if ( - request === 'react-dom' && - (layer === WEBPACK_LAYERS.serverSideRendering || - layer === WEBPACK_LAYERS.reactServerComponents || - layer === WEBPACK_LAYERS.actionBrowser) - ) { - request = `next/dist/compiled/react-dom${bundledReactChannel}/server-rendering-stub` - } else if (isAppLayer) { - request = - 'next/dist/compiled/' + - request.replace( - /^(react-server-dom-webpack|react-dom|react)/, - (name) => { - return name + bundledReactChannel - } - ) - } + // if ( + // request === 'react-dom' && + // (layer === WEBPACK_LAYERS.serverSideRendering || + // layer === WEBPACK_LAYERS.reactServerComponents || + // layer === WEBPACK_LAYERS.actionBrowser) + // ) { + // request = `next/dist/compiled/react-dom${bundledReactChannel}/server-rendering-stub` + // } else if (isAppLayer) { + // request = + // 'next/dist/compiled/' + + // request.replace( + // /^(react-server-dom-webpack|react-dom|react)/, + // (name) => { + // return name + bundledReactChannel + // } + // ) + // } return `commonjs ${request}` } const notExternalModules = /^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|document|link|image|legacy\/image|constants|dynamic|script|navigation|headers|router)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-action-proxy$)/ if (notExternalModules.test(request)) { + console.log('notExternalModules', request) return } } @@ -1433,10 +1467,11 @@ export default async function getBaseWebpackConfig( const resolveNextExternal = (localRes: string) => { const isSharedRuntime = sharedRuntimePattern.test(localRes) const isExternal = externalPattern.test(localRes) + const isRenderRuntime = genericRuntimePattern.test(localRes) // if the file ends with .external, we need to make it a commonjs require in all cases // this is used mainly to share the async local storage across the routing, rendering and user layers. - if (isExternal) { + if (isExternal || isRenderRuntime) { // it's important we return the path that starts with `next/dist/` here instead of the absolute path // otherwise NFT will get tripped up return `commonjs ${localRes.replace(/.*?next[/\\]dist/, 'next/dist')}` @@ -1521,14 +1556,14 @@ export default async function getBaseWebpackConfig( // Treat react packages and next internals as external for SSR layer, // also map react to builtin ones with require-hook. if (layer === WEBPACK_LAYERS.serverSideRendering) { - if (reactPackagesRegex.test(request)) { - return `commonjs next/dist/compiled/${request.replace( - /^(react-server-dom-webpack|react-dom|react)/, - (name) => { - return name + bundledReactChannel - } - )}` - } + // if (reactPackagesRegex.test(request)) { + // return `commonjs next/dist/compiled/${request.replace( + // /^(react-server-dom-webpack|react-dom|react)/, + // (name) => { + // return name + bundledReactChannel + // } + // )}` + // } const isRelative = request.startsWith('.') const fullRequest = isRelative @@ -1729,7 +1764,7 @@ export default async function getBaseWebpackConfig( context, request, dependencyType, - contextInfo.issuerLayer, + contextInfo.issuerLayer as WebpackLayerName, (options) => { const resolveFunction = getResolve(options) return (resolveContext: string, requestToResolve: string) => @@ -2156,11 +2191,13 @@ export default async function getBaseWebpackConfig( // react to the direct file path, not the package name. In that case the condition // will be ignored completely. alias: createRSCAliases(bundledReactChannel, { - reactSharedSubset: true, - reactDomServerRenderingStub: true, + // reactSharedSubset: true, + // reactDomServerRenderingStub: true, reactServerCondition: true, // No server components profiling reactProductionProfiling, + // browser: false, + layer: WEBPACK_LAYERS.reactServerComponents, }), }, use: { @@ -2219,10 +2256,12 @@ export default async function getBaseWebpackConfig( // It needs `conditionNames` here to require the proper asset, // when react is acting as dependency of compiled/react-dom. alias: createRSCAliases(bundledReactChannel, { - reactSharedSubset: true, - reactDomServerRenderingStub: true, + // reactSharedSubset: true, + // reactDomServerRenderingStub: true, reactServerCondition: true, reactProductionProfiling, + // browser: false, + layer: WEBPACK_LAYERS.reactServerComponents, }), }, }, @@ -2231,10 +2270,12 @@ export default async function getBaseWebpackConfig( issuerLayer: WEBPACK_LAYERS.serverSideRendering, resolve: { alias: createRSCAliases(bundledReactChannel, { - reactSharedSubset: false, - reactDomServerRenderingStub: true, + // reactSharedSubset: false, + // reactDomServerRenderingStub: true, reactServerCondition: false, reactProductionProfiling, + // browser: true, + layer: WEBPACK_LAYERS.serverSideRendering, }), }, }, @@ -2246,10 +2287,12 @@ export default async function getBaseWebpackConfig( resolve: { alias: createRSCAliases(bundledReactChannel, { // Only alias server rendering stub in client SSR layer. - reactSharedSubset: false, - reactDomServerRenderingStub: false, + // reactSharedSubset: false, + // reactDomServerRenderingStub: false, reactServerCondition: false, reactProductionProfiling, + // browser: isClient, + layer: WEBPACK_LAYERS.appPagesBrowser, }), }, }, diff --git a/packages/next/src/client/components/router-reducer/fetch-server-response.ts b/packages/next/src/client/components/router-reducer/fetch-server-response.ts index 689d27df7798f..e6c3eb037b143 100644 --- a/packages/next/src/client/components/router-reducer/fetch-server-response.ts +++ b/packages/next/src/client/components/router-reducer/fetch-server-response.ts @@ -2,7 +2,15 @@ // @ts-ignore // eslint-disable-next-line import/no-extraneous-dependencies -import { createFromFetch } from 'react-server-dom-webpack/client' +// import { createFromFetch } from 'react-server-dom-webpack/client' +const { createFromFetch } = ( + typeof window === 'undefined' + ? // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client.edge') + : // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client') +) as typeof import('react-server-dom-webpack/client') + import type { FlightRouterState, FlightData, diff --git a/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts b/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts index 0c6caaba746ca..3cd195b17c04f 100644 --- a/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts +++ b/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts @@ -12,10 +12,17 @@ import { } from '../../app-router-headers' import { createRecordFromThenable } from '../create-record-from-thenable' import { readRecordValue } from '../read-record-value' -// eslint-disable-next-line import/no-extraneous-dependencies -import { createFromFetch } from 'react-server-dom-webpack/client' -// eslint-disable-next-line import/no-extraneous-dependencies -import { encodeReply } from 'react-server-dom-webpack/client' +// // eslint-disable-next-line import/no-extraneous-dependencies +// import { createFromFetch } from 'react-server-dom-webpack/client' +// // eslint-disable-next-line import/no-extraneous-dependencies +// import { encodeReply } from 'react-server-dom-webpack/client' +const { createFromFetch, encodeReply } = ( + typeof window === 'undefined' + ? // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client.edge') + : // eslint-disable-next-line import/no-extraneous-dependencies + require('react-server-dom-webpack/client') +) as typeof import('react-server-dom-webpack/client') import { ReadonlyReducerState, diff --git a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.development.js b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.development.js index 15a3a5bd195b2..933a1cf4b1b58 100644 --- a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.development.js +++ b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react-experimental"); +var React = require('react'); var ReactDOM = require('react-dom'); var ReactVersion = '18.3.0-experimental-dd480ef92-20230822'; diff --git a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.production.min.js b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.production.min.js index 1fd88592e6fc2..5429882ac9a40 100644 --- a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.production.min.js +++ b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom-server-legacy.browser.production.min.js @@ -7,7 +7,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var ca=require("next/dist/compiled/react-experimental"),da=require("react-dom");function l(a){for(var b="https://reactjs.org/docs/error-decoder.html?invariant="+a,c=1;cja||(a.current=ia[ja],ia[ja]=null,ja--)}function E(a,b){ja++;ia[ja]=a.current;a.current=b} var la=Symbol.for("react.element"),ma=Symbol.for("react.portal"),na=Symbol.for("react.fragment"),oa=Symbol.for("react.strict_mode"),pa=Symbol.for("react.profiler"),qa=Symbol.for("react.provider"),ra=Symbol.for("react.context"),sa=Symbol.for("react.server_context"),ta=Symbol.for("react.forward_ref"),ua=Symbol.for("react.suspense"),va=Symbol.for("react.suspense_list"),wa=Symbol.for("react.memo"),xa=Symbol.for("react.lazy"),ya=Symbol.for("react.scope");Symbol.for("react.debug_trace_mode"); var za=Symbol.for("react.offscreen"),Aa=Symbol.for("react.legacy_hidden"),Ba=Symbol.for("react.cache");Symbol.for("react.tracing_marker");var Ca=Symbol.for("react.default_value"),Da=Symbol.for("react.memo_cache_sentinel"),Ea=Symbol.for("react.postpone"),Fa=Symbol.iterator;function Ga(a){if(null===a||"object"!==typeof a)return null;a=Fa&&a[Fa]||a["@@iterator"];return"function"===typeof a?a:null} diff --git a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.development.js b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.development.js index 3516e84e55d88..98b075d7cd6aa 100644 --- a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.development.js +++ b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.development.js @@ -23,8 +23,8 @@ if ( ) { __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error()); } - var React = require("next/dist/compiled/react-experimental"); -var Scheduler = require("next/dist/compiled/scheduler-experimental"); + var React = require('react'); +var Scheduler = require('scheduler'); var Internals = { usingClientEntryPoint: false, diff --git a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.production.min.js b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.production.min.js index 212d918338910..d51fe46f4353a 100644 --- a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.production.min.js +++ b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.production.min.js @@ -10,7 +10,7 @@ /* Modernizr 3.0.0pre (Custom Build) | MIT */ -'use strict';var aa=require("next/dist/compiled/react-experimental"),ba=require("next/dist/compiled/scheduler-experimental"),ca={usingClientEntryPoint:!1,Events:null,Dispatcher:{current:null}};function t(a){for(var b="https://reactjs.org/docs/error-decoder.html?invariant="+a,c=1;cja||(a.current=ia[ja],ia[ja]=null,ja--)}function E(a,b){ja++;ia[ja]=a.current;a.current=b} var la=Symbol.for("react.element"),ma=Symbol.for("react.portal"),na=Symbol.for("react.fragment"),oa=Symbol.for("react.strict_mode"),pa=Symbol.for("react.profiler"),qa=Symbol.for("react.provider"),ra=Symbol.for("react.context"),sa=Symbol.for("react.server_context"),ta=Symbol.for("react.forward_ref"),ua=Symbol.for("react.suspense"),va=Symbol.for("react.suspense_list"),wa=Symbol.for("react.memo"),xa=Symbol.for("react.lazy"),ya=Symbol.for("react.scope");Symbol.for("react.debug_trace_mode"); var za=Symbol.for("react.offscreen"),Aa=Symbol.for("react.legacy_hidden"),Ba=Symbol.for("react.cache");Symbol.for("react.tracing_marker");var Ca=Symbol.for("react.default_value"),Da=Symbol.for("react.memo_cache_sentinel"),Ea=Symbol.for("react.postpone"),Fa=Symbol.iterator;function Ga(a){if(null===a||"object"!==typeof a)return null;a=Fa&&a[Fa]||a["@@iterator"];return"function"===typeof a?a:null} diff --git a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.profiling.min.js b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.profiling.min.js index 33c62fb078628..5ea39f478ef4e 100644 --- a/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.profiling.min.js +++ b/packages/next/src/compiled/react-dom-experimental/cjs/react-dom.profiling.min.js @@ -21,7 +21,7 @@ if ( /* Modernizr 3.0.0pre (Custom Build) | MIT */ -'use strict';var aa=require("next/dist/compiled/react-experimental"),ba=require("next/dist/compiled/scheduler-experimental"),ca={usingClientEntryPoint:!1,Events:null,Dispatcher:{current:null}};function t(a){for(var b="https://reactjs.org/docs/error-decoder.html?invariant="+a,c=1;cja||(a.current=ha[ja],ha[ja]=null,ja--)}function E(a,b){ja++;ha[ja]=a.current;a.current=b} var la=Symbol.for("react.element"),ma=Symbol.for("react.portal"),na=Symbol.for("react.fragment"),oa=Symbol.for("react.strict_mode"),pa=Symbol.for("react.profiler"),qa=Symbol.for("react.provider"),ra=Symbol.for("react.context"),sa=Symbol.for("react.server_context"),ta=Symbol.for("react.forward_ref"),ua=Symbol.for("react.suspense"),va=Symbol.for("react.suspense_list"),wa=Symbol.for("react.memo"),xa=Symbol.for("react.lazy"),ya=Symbol.for("react.scope");Symbol.for("react.debug_trace_mode"); var za=Symbol.for("react.offscreen"),Aa=Symbol.for("react.legacy_hidden"),Ba=Symbol.for("react.cache");Symbol.for("react.tracing_marker");var Ca=Symbol.for("react.default_value"),Da=Symbol.for("react.memo_cache_sentinel"),Ea=Symbol.for("react.postpone"),Fa=Symbol.iterator;function Ga(a){if(null===a||"object"!==typeof a)return null;a=Fa&&a[Fa]||a["@@iterator"];return"function"===typeof a?a:null} diff --git a/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.development.js b/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.development.js index a7e8b9b1e59f8..11b447300603c 100644 --- a/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.development.js +++ b/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react"); +var React = require('react'); var ReactDOM = require('react-dom'); var ReactVersion = '18.3.0-canary-dd480ef92-20230822'; diff --git a/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.production.min.js b/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.production.min.js index 4721283164864..14f4bfcfd2881 100644 --- a/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +++ b/packages/next/src/compiled/react-dom/cjs/react-dom-server-legacy.browser.production.min.js @@ -7,7 +7,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var aa=require("next/dist/compiled/react"),la=require("react-dom");function l(a){for(var b="https://reactjs.org/docs/error-decoder.html?invariant="+a,c=1;cha||(a.current=fa[ha],fa[ha]=null,ha--)}function E(a,b){ha++;fa[ha]=a.current;a.current=b} var ja=Symbol.for("react.element"),ka=Symbol.for("react.portal"),la=Symbol.for("react.fragment"),ma=Symbol.for("react.strict_mode"),na=Symbol.for("react.profiler"),oa=Symbol.for("react.provider"),pa=Symbol.for("react.context"),qa=Symbol.for("react.server_context"),ra=Symbol.for("react.forward_ref"),sa=Symbol.for("react.suspense"),ta=Symbol.for("react.suspense_list"),ua=Symbol.for("react.memo"),va=Symbol.for("react.lazy"),wa=Symbol.for("react.scope");Symbol.for("react.debug_trace_mode"); var xa=Symbol.for("react.offscreen"),ya=Symbol.for("react.legacy_hidden"),za=Symbol.for("react.cache");Symbol.for("react.tracing_marker");var Aa=Symbol.for("react.default_value"),Ba=Symbol.iterator;function Ca(a){if(null===a||"object"!==typeof a)return null;a=Ba&&a[Ba]||a["@@iterator"];return"function"===typeof a?a:null}var Da=ia(null),Ea=ia(null),Fa=ia(null); diff --git a/packages/next/src/compiled/react-dom/cjs/react-dom.profiling.min.js b/packages/next/src/compiled/react-dom/cjs/react-dom.profiling.min.js index 6e1719b94551e..dacda5f5473d5 100644 --- a/packages/next/src/compiled/react-dom/cjs/react-dom.profiling.min.js +++ b/packages/next/src/compiled/react-dom/cjs/react-dom.profiling.min.js @@ -21,7 +21,7 @@ if ( /* Modernizr 3.0.0pre (Custom Build) | MIT */ -'use strict';var aa=require("next/dist/compiled/scheduler"),ba=require("next/dist/compiled/react"),ca={usingClientEntryPoint:!1,Events:null,Dispatcher:{current:null}};function r(a){for(var b="https://reactjs.org/docs/error-decoder.html?invariant="+a,c=1;cha||(a.current=fa[ha],fa[ha]=null,ha--)}function E(a,b){ha++;fa[ha]=a.current;a.current=b} var ja=Symbol.for("react.element"),ka=Symbol.for("react.portal"),la=Symbol.for("react.fragment"),ma=Symbol.for("react.strict_mode"),na=Symbol.for("react.profiler"),oa=Symbol.for("react.provider"),pa=Symbol.for("react.context"),qa=Symbol.for("react.server_context"),ra=Symbol.for("react.forward_ref"),sa=Symbol.for("react.suspense"),ta=Symbol.for("react.suspense_list"),ua=Symbol.for("react.memo"),va=Symbol.for("react.lazy"),wa=Symbol.for("react.scope");Symbol.for("react.debug_trace_mode"); var xa=Symbol.for("react.offscreen"),ya=Symbol.for("react.legacy_hidden"),za=Symbol.for("react.cache");Symbol.for("react.tracing_marker");var Aa=Symbol.for("react.default_value"),Ba=Symbol.iterator;function Ca(a){if(null===a||"object"!==typeof a)return null;a=Ba&&a[Ba]||a["@@iterator"];return"function"===typeof a?a:null}var Da=ia(null),Ea=ia(null),Fa=ia(null); diff --git a/packages/next/src/compiled/react-experimental/cjs/react-jsx-dev-runtime.development.js b/packages/next/src/compiled/react-experimental/cjs/react-jsx-dev-runtime.development.js index b7fbf53834137..8fcab4f2602e1 100644 --- a/packages/next/src/compiled/react-experimental/cjs/react-jsx-dev-runtime.development.js +++ b/packages/next/src/compiled/react-experimental/cjs/react-jsx-dev-runtime.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react-experimental"); +var React = require('react'); // ATTENTION // When adding new symbols to this file, diff --git a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.development.js b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.development.js index cbc97be6bbc73..d5d0bb7693f96 100644 --- a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.development.js +++ b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react-experimental"); +var React = require('react'); // ATTENTION // When adding new symbols to this file, diff --git a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.production.min.js b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.production.min.js index 789512436d200..3ecb2350b3e55 100644 --- a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.production.min.js +++ b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.production.min.js @@ -7,5 +7,5 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var f=require("next/dist/compiled/react-experimental"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; +'use strict';var f=require("react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; function q(c,a,g){var b,d={},e=null,h=null;void 0!==g&&(e=""+g);void 0!==a.key&&(e=""+a.key);void 0!==a.ref&&(h=a.ref);for(b in a)m.call(a,b)&&!p.hasOwnProperty(b)&&(d[b]=a[b]);if(c&&c.defaultProps)for(b in a=c.defaultProps,a)void 0===d[b]&&(d[b]=a[b]);return{$$typeof:k,type:c,key:e,ref:h,props:d,_owner:n.current}}exports.Fragment=l;exports.jsx=q;exports.jsxs=q; diff --git a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.profiling.min.js b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.profiling.min.js index 5aeb73baa6743..b875a84b3d090 100644 --- a/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.profiling.min.js +++ b/packages/next/src/compiled/react-experimental/cjs/react-jsx-runtime.profiling.min.js @@ -7,5 +7,5 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var f=require("next/dist/compiled/react-experimental"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; +'use strict';var f=require("react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; function q(c,a,g){var b,d={},e=null,h=null;void 0!==g&&(e=""+g);void 0!==a.key&&(e=""+a.key);void 0!==a.ref&&(h=a.ref);for(b in a)m.call(a,b)&&!p.hasOwnProperty(b)&&(d[b]=a[b]);if(c&&c.defaultProps)for(b in a=c.defaultProps,a)void 0===d[b]&&(d[b]=a[b]);return{$$typeof:k,type:c,key:e,ref:h,props:d,_owner:n.current}}exports.Fragment=l;exports.jsx=q;exports.jsxs=q; diff --git a/packages/next/src/compiled/react/cjs/react-jsx-dev-runtime.development.js b/packages/next/src/compiled/react/cjs/react-jsx-dev-runtime.development.js index 9d4d9fe8f9768..05e5bedbfad47 100644 --- a/packages/next/src/compiled/react/cjs/react-jsx-dev-runtime.development.js +++ b/packages/next/src/compiled/react/cjs/react-jsx-dev-runtime.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react"); +var React = require('react'); // ATTENTION // When adding new symbols to this file, diff --git a/packages/next/src/compiled/react/cjs/react-jsx-runtime.development.js b/packages/next/src/compiled/react/cjs/react-jsx-runtime.development.js index 9c7bd16ccdb47..ba3757094e39a 100644 --- a/packages/next/src/compiled/react/cjs/react-jsx-runtime.development.js +++ b/packages/next/src/compiled/react/cjs/react-jsx-runtime.development.js @@ -14,7 +14,7 @@ if (process.env.NODE_ENV !== "production") { (function() { 'use strict'; -var React = require("next/dist/compiled/react"); +var React = require('react'); // ATTENTION // When adding new symbols to this file, diff --git a/packages/next/src/compiled/react/cjs/react-jsx-runtime.production.min.js b/packages/next/src/compiled/react/cjs/react-jsx-runtime.production.min.js index 2c15ab3ac1155..3ecb2350b3e55 100644 --- a/packages/next/src/compiled/react/cjs/react-jsx-runtime.production.min.js +++ b/packages/next/src/compiled/react/cjs/react-jsx-runtime.production.min.js @@ -7,5 +7,5 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var f=require("next/dist/compiled/react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; +'use strict';var f=require("react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; function q(c,a,g){var b,d={},e=null,h=null;void 0!==g&&(e=""+g);void 0!==a.key&&(e=""+a.key);void 0!==a.ref&&(h=a.ref);for(b in a)m.call(a,b)&&!p.hasOwnProperty(b)&&(d[b]=a[b]);if(c&&c.defaultProps)for(b in a=c.defaultProps,a)void 0===d[b]&&(d[b]=a[b]);return{$$typeof:k,type:c,key:e,ref:h,props:d,_owner:n.current}}exports.Fragment=l;exports.jsx=q;exports.jsxs=q; diff --git a/packages/next/src/compiled/react/cjs/react-jsx-runtime.profiling.min.js b/packages/next/src/compiled/react/cjs/react-jsx-runtime.profiling.min.js index 5936004d767cc..b875a84b3d090 100644 --- a/packages/next/src/compiled/react/cjs/react-jsx-runtime.profiling.min.js +++ b/packages/next/src/compiled/react/cjs/react-jsx-runtime.profiling.min.js @@ -7,5 +7,5 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ -'use strict';var f=require("next/dist/compiled/react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; +'use strict';var f=require("react"),k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnProperty,n=f.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,p={key:!0,ref:!0,__self:!0,__source:!0}; function q(c,a,g){var b,d={},e=null,h=null;void 0!==g&&(e=""+g);void 0!==a.key&&(e=""+a.key);void 0!==a.ref&&(h=a.ref);for(b in a)m.call(a,b)&&!p.hasOwnProperty(b)&&(d[b]=a[b]);if(c&&c.defaultProps)for(b in a=c.defaultProps,a)void 0===d[b]&&(d[b]=a[b]);return{$$typeof:k,type:c,key:e,ref:h,props:d,_owner:n.current}}exports.Fragment=l;exports.jsx=q;exports.jsxs=q; diff --git a/packages/next/src/lib/constants.ts b/packages/next/src/lib/constants.ts index 65370408b0bca..92702f6231c18 100644 --- a/packages/next/src/lib/constants.ts +++ b/packages/next/src/lib/constants.ts @@ -138,7 +138,10 @@ const WEBPACK_LAYERS_NAMES = { * The layer for the server bundle for App Route handlers. */ appRouteHandler: 'app-route-handler', -} +} as const + +export type WebpackLayerName = + (typeof WEBPACK_LAYERS_NAMES)[keyof typeof WEBPACK_LAYERS_NAMES] export const WEBPACK_LAYERS = { ...WEBPACK_LAYERS_NAMES, diff --git a/packages/next/src/server/esm-loader.mts b/packages/next/src/server/esm-loader.mts index 313bde8b39aae..946e73a7075f0 100644 --- a/packages/next/src/server/esm-loader.mts +++ b/packages/next/src/server/esm-loader.mts @@ -3,12 +3,12 @@ import module from 'module' const require = module.createRequire(import.meta.url) export function resolve(specifier: string, context: any, nextResolve: any) { - const { overrideReact, hookPropertyMap } = require(process.env.NEXT_YARN_PNP + const { hookPropertyMap } = require(process.env.NEXT_YARN_PNP ? './import-overrides' : 'next/dist/server/import-overrides') as typeof import('./import-overrides') // In case the environment variable is set after the module is loaded. - overrideReact() + // overrideReact()s const hookResolved = hookPropertyMap.get(specifier) if (hookResolved) { diff --git a/packages/next/src/server/future/route-modules/app-page/module.ts b/packages/next/src/server/future/route-modules/app-page/module.ts index daa0291a1c8b4..fac86c7f73fde 100644 --- a/packages/next/src/server/future/route-modules/app-page/module.ts +++ b/packages/next/src/server/future/route-modules/app-page/module.ts @@ -12,6 +12,9 @@ import { type RouteModuleHandleContext, } from '../route-module' import * as sharedModules from './shared-modules' +import * as vendoredReactRSC from './vendored/rsc/entrypoints' +import * as vendoredReactSSR from './vendored/ssr/entrypoints' +import * as vendoredReactShared from './vendored/shared/entrypoints' type AppPageUserlandModule = { /** @@ -36,6 +39,8 @@ export class AppPageRouteModule extends RouteModule< AppPageUserlandModule > { static readonly sharedModules = sharedModules + // static readonly vendoredReact = vendoredReact + // static readonly vendoredReactSSR = vendoredReactSSR public render( req: IncomingMessage, @@ -52,6 +57,12 @@ export class AppPageRouteModule extends RouteModule< } } -export { renderToHTMLOrFlight } +const vendored = { + 'react-rsc': vendoredReactRSC, + 'react-ssr': vendoredReactSSR, + 'react-shared': vendoredReactShared, +} + +export { renderToHTMLOrFlight, vendored } export default AppPageRouteModule diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/rsc/entrypoints.ts b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/entrypoints.ts new file mode 100644 index 0000000000000..f0c93e41a5b53 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/entrypoints.ts @@ -0,0 +1,29 @@ +// module.exports = { +// React: require('react'), +// ReactDOM: require('react-dom/server-rendering-stub'), + +// // TODO: move higher +// ReactJsxRuntime: require('react/jsx-runtime'), +// ReactJsxDevRuntime: require('react/jsx-dev-runtime'), + +// // eslint-disable-next-line import/no-extraneous-dependencies +// ReactServerDOMWebpackServerNode: require('react-server-dom-webpack/server.node'), +// // eslint-disable-next-line import/no-extraneous-dependencies +// ReactServerDOMWebpackServerEdge: require('react-server-dom-webpack/server.edge'), +// } + +import * as React from 'react' + +import * as ReactDOM from 'react-dom/server-rendering-stub' + +// eslint-disable-next-line import/no-extraneous-dependencies +import * as ReactServerDOMWebpackServerNode from 'react-server-dom-webpack/server.node' +// eslint-disable-next-line import/no-extraneous-dependencies +import * as ReactServerDOMWebpackServerEdge from 'react-server-dom-webpack/server.edge' + +export { + React, + ReactDOM, + ReactServerDOMWebpackServerNode, + ReactServerDOMWebpackServerEdge, +} diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-dom.ts b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-dom.ts new file mode 100644 index 0000000000000..5d369d4e33771 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-dom.ts @@ -0,0 +1 @@ +module.exports = require('../../module.compiled').vendored['react-rsc'].ReactDOM diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-edge.ts b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-edge.ts new file mode 100644 index 0000000000000..0ae9701b1e912 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-edge.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-rsc' +].ReactServerDOMWebpackServerEdge diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-node.ts b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-node.ts new file mode 100644 index 0000000000000..fbb7bd17d4ffc --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react-server-dom-webpack-server-node.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-rsc' +].ReactServerDOMWebpackServerNode diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react.ts b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react.ts new file mode 100644 index 0000000000000..41b44328775ab --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/rsc/react.ts @@ -0,0 +1 @@ +module.exports = require('../../module.compiled').vendored['react-rsc'].React diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/shared/entrypoints.ts b/packages/next/src/server/future/route-modules/app-page/vendored/shared/entrypoints.ts new file mode 100644 index 0000000000000..b5a1bf1a3fd02 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/shared/entrypoints.ts @@ -0,0 +1,4 @@ +import * as ReactJsxDevRuntime from 'react/jsx-dev-runtime' +import * as ReactJsxRuntime from 'react/jsx-runtime' + +export { ReactJsxDevRuntime, ReactJsxRuntime } diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-dev-runtime.ts b/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-dev-runtime.ts new file mode 100644 index 0000000000000..9623bb4a90ae0 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-dev-runtime.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-shared' +].ReactJsxDevRuntime diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-runtime.ts b/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-runtime.ts new file mode 100644 index 0000000000000..b7d24f304f96b --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/shared/react-jsx-runtime.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-shared' +].ReactJsxRuntime diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/ssr/entrypoints.ts b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/entrypoints.ts new file mode 100644 index 0000000000000..1b5bdbb161887 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/entrypoints.ts @@ -0,0 +1,23 @@ +// module.exports = { +// React: require('react'), +// ReactDOM: require('react-dom/server-rendering-stub'), + +// // todo move higher +// ReactJsxRuntime: require('react/jsx-runtime'), +// ReactJsxDevRuntime: require('react/jsx-dev-runtime'), + +// // ReactDOMServerEdge: require('react-dom/cjs/react-dom-server.browser.production.min'), +// ReactDOMServerEdge: require('react-dom/cjs/react-dom-server.node.production.min'), +// // eslint-disable-next-line import/no-extraneous-dependencies +// ReactServerDOMWebpackClientEdge: require('react-server-dom-webpack/client.edge'), +// } + +import * as React from 'react' +import * as ReactDOM from 'react-dom/server-rendering-stub' + +// eslint-disable-next-line import/no-extraneous-dependencies +import * as ReactDOMServerEdge from 'react-dom/server.edge' +// eslint-disable-next-line import/no-extraneous-dependencies +import * as ReactServerDOMWebpackClientEdge from 'react-server-dom-webpack/client.edge' + +export { React, ReactDOM, ReactDOMServerEdge, ReactServerDOMWebpackClientEdge } diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom-server-edge.ts b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom-server-edge.ts new file mode 100644 index 0000000000000..0908aefd95554 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom-server-edge.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-ssr' +].ReactDOMServerEdge diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom.ts b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom.ts new file mode 100644 index 0000000000000..378577fa80ec2 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-dom.ts @@ -0,0 +1 @@ +module.exports = require('../../module.compiled').vendored['react-ssr'].ReactDOM diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-server-dom-webpack-client-edge.ts b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-server-dom-webpack-client-edge.ts new file mode 100644 index 0000000000000..5398d7eecc700 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react-server-dom-webpack-client-edge.ts @@ -0,0 +1,3 @@ +module.exports = require('../../module.compiled').vendored[ + 'react-ssr' +].ReactServerDOMWebpackClientEdge diff --git a/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react.ts b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react.ts new file mode 100644 index 0000000000000..c2947601471d3 --- /dev/null +++ b/packages/next/src/server/future/route-modules/app-page/vendored/ssr/react.ts @@ -0,0 +1 @@ +module.exports = require('../../module.compiled').vendored['react-ssr'].React diff --git a/packages/next/src/server/import-overrides.ts b/packages/next/src/server/import-overrides.ts index db203e6d04644..2b60f820834c0 100644 --- a/packages/next/src/server/import-overrides.ts +++ b/packages/next/src/server/import-overrides.ts @@ -23,54 +23,54 @@ export const defaultOverrides = { : resolve('styled-jsx/style', nextPaths), } -export const baseOverrides = { - react: 'next/dist/compiled/react', - 'react/package.json': 'next/dist/compiled/react/package.json', - 'react/jsx-runtime': 'next/dist/compiled/react/jsx-runtime', - 'react/jsx-dev-runtime': 'next/dist/compiled/react/jsx-dev-runtime', - 'react-dom': 'next/dist/compiled/react-dom/server-rendering-stub', - 'react-dom/package.json': 'next/dist/compiled/react-dom/package.json', - 'react-dom/client': 'next/dist/compiled/react-dom/client', - 'react-dom/server': 'next/dist/compiled/react-dom/server', - 'react-dom/server.browser': 'next/dist/compiled/react-dom/server.browser', - 'react-dom/server.edge': 'next/dist/compiled/react-dom/server.edge', - 'react-server-dom-webpack/client': - 'next/dist/compiled/react-server-dom-webpack/client', - 'react-server-dom-webpack/client.edge': - 'next/dist/compiled/react-server-dom-webpack/client.edge', - 'react-server-dom-webpack/server.edge': - 'next/dist/compiled/react-server-dom-webpack/server.edge', - 'react-server-dom-webpack/server.node': - 'next/dist/compiled/react-server-dom-webpack/server.node', -} +// export const baseOverrides = { +// react: 'next/dist/compiled/react', +// 'react/package.json': 'next/dist/compiled/react/package.json', +// 'react/jsx-runtime': 'next/dist/compiled/react/jsx-runtime', +// 'react/jsx-dev-runtime': 'next/dist/compiled/react/jsx-dev-runtime', +// 'react-dom': 'next/dist/compiled/react-dom/server-rendering-stub', +// 'react-dom/package.json': 'next/dist/compiled/react-dom/package.json', +// 'react-dom/client': 'next/dist/compiled/react-dom/client', +// 'react-dom/server': 'next/dist/compiled/react-dom/server', +// 'react-dom/server.browser': 'next/dist/compiled/react-dom/server.browser', +// 'react-dom/server.edge': 'next/dist/compiled/react-dom/server.edge', +// 'react-server-dom-webpack/client': +// 'next/dist/compiled/react-server-dom-webpack/client', +// 'react-server-dom-webpack/client.edge': +// 'next/dist/compiled/react-server-dom-webpack/client.edge', +// 'react-server-dom-webpack/server.edge': +// 'next/dist/compiled/react-server-dom-webpack/server.edge', +// 'react-server-dom-webpack/server.node': +// 'next/dist/compiled/react-server-dom-webpack/server.node', +// } -export const experimentalOverrides = { - react: 'next/dist/compiled/react-experimental', - 'react/jsx-runtime': 'next/dist/compiled/react-experimental/jsx-runtime', - 'react/jsx-dev-runtime': - 'next/dist/compiled/react-experimental/jsx-dev-runtime', - 'react-dom': - 'next/dist/compiled/react-dom-experimental/server-rendering-stub', - 'react/package.json': 'next/dist/compiled/react-experimental/package.json', - 'react-dom/package.json': - 'next/dist/compiled/react-dom-experimental/package.json', - 'react-dom/client': 'next/dist/compiled/react-dom-experimental/client', - 'react-dom/server': 'next/dist/compiled/react-dom-experimental/server', - 'react-dom/server.browser': - 'next/dist/compiled/react-dom-experimental/server.browser', - 'react-dom/server.edge': - 'next/dist/compiled/react-dom-experimental/server.edge', - 'react-server-dom-webpack/client': - 'next/dist/compiled/react-server-dom-webpack-experimental/client', - 'react-server-dom-webpack/client.edge': - 'next/dist/compiled/react-server-dom-webpack-experimental/client.edge', - 'react-server-dom-webpack/server.edge': - 'next/dist/compiled/react-server-dom-webpack-experimental/server.edge', - 'react-server-dom-webpack/server.node': - 'next/dist/compiled/react-server-dom-webpack-experimental/server.node', -} +// export const experimentalOverrides = { +// react: 'next/dist/compiled/react-experimental', +// 'react/jsx-runtime': 'next/dist/compiled/react-experimental/jsx-runtime', +// 'react/jsx-dev-runtime': +// 'next/dist/compiled/react-experimental/jsx-dev-runtime', +// 'react-dom': +// 'next/dist/compiled/react-dom-experimental/server-rendering-stub', +// 'react/package.json': 'next/dist/compiled/react-experimental/package.json', +// 'react-dom/package.json': +// 'next/dist/compiled/react-dom-experimental/package.json', +// 'react-dom/client': 'next/dist/compiled/react-dom-experimental/client', +// 'react-dom/server': 'next/dist/compiled/react-dom-experimental/server', +// 'react-dom/server.browser': +// 'next/dist/compiled/react-dom-experimental/server.browser', +// 'react-dom/server.edge': +// 'next/dist/compiled/react-dom-experimental/server.edge', +// 'react-server-dom-webpack/client': +// 'next/dist/compiled/react-server-dom-webpack-experimental/client', +// 'react-server-dom-webpack/client.edge': +// 'next/dist/compiled/react-server-dom-webpack-experimental/client.edge', +// 'react-server-dom-webpack/server.edge': +// 'next/dist/compiled/react-server-dom-webpack-experimental/server.edge', +// 'react-server-dom-webpack/server.node': +// 'next/dist/compiled/react-server-dom-webpack-experimental/server.node', +// } -let aliasedPrebundledReact = false +// let aliasedPrebundledReact = false const toResolveMap = (map: Record): [string, string][] => Object.entries(map).map(([key, value]) => [key, resolve(value, nextPaths)]) @@ -83,19 +83,19 @@ export function addHookAliases(aliases: [string, string][] = []) { addHookAliases(toResolveMap(defaultOverrides)) -// Override built-in React packages if necessary -export function overrideReact() { - if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT && !aliasedPrebundledReact) { - aliasedPrebundledReact = true +// // Override built-in React packages if necessary +// export function overrideReact() { +// if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT && !aliasedPrebundledReact) { +// aliasedPrebundledReact = true - // Require these modules with static paths to make sure they are tracked by - // NFT when building the app in standalone mode, as we are now conditionally - // aliasing them it's tricky to track them in build time. - if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT === 'experimental') { - addHookAliases(toResolveMap(experimentalOverrides)) - } else { - addHookAliases(toResolveMap(baseOverrides)) - } - } -} -overrideReact() +// // Require these modules with static paths to make sure they are tracked by +// // NFT when building the app in standalone mode, as we are now conditionally +// // aliasing them it's tricky to track them in build time. +// if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT === 'experimental') { +// addHookAliases(toResolveMap(experimentalOverrides)) +// } else { +// addHookAliases(toResolveMap(baseOverrides)) +// } +// } +// } +// overrideReact() diff --git a/packages/next/src/server/require-hook.js b/packages/next/src/server/require-hook.js index ce9e30e6e9ffe..9b473529c9c6d 100644 --- a/packages/next/src/server/require-hook.js +++ b/packages/next/src/server/require-hook.js @@ -8,7 +8,7 @@ const mod = require('module') const originalRequire = mod.prototype.require const resolveFilename = mod._resolveFilename -const { overrideReact, hookPropertyMap } = require('./import-overrides') +const { hookPropertyMap } = require('./import-overrides') mod._resolveFilename = function ( originalResolveFilename, @@ -19,7 +19,7 @@ mod._resolveFilename = function ( options ) { // In case the environment variable is set after the module is loaded. - overrideReact() + // overrideReact() const hookResolved = requestMap.get(request) if (hookResolved) request = hookResolved diff --git a/packages/next/taskfile.js b/packages/next/taskfile.js index 43da4071afb25..08ce0cc5660cb 100644 --- a/packages/next/taskfile.js +++ b/packages/next/taskfile.js @@ -1738,14 +1738,14 @@ export async function copy_vendor_react(task_) { yield task .source(join(reactDir, 'cjs/**/*.js')) // eslint-disable-next-line require-yield - .run({ every: true }, function* (file) { - const source = file.data.toString() - // We replace the module/chunk loading code with our own implementation in Next.js. - file.data = source.replace( - /require\(["']react["']\)/g, - `require("next/dist/compiled/react${packageSuffix}")` - ) - }) + // .run({ every: true }, function* (file) { + // const source = file.data.toString() + // // We replace the module/chunk loading code with our own implementation in Next.js. + // file.data = source.replace( + // /require\(["']react["']\)/g, + // `require("next/dist/compiled/react${packageSuffix}")` + // ) + // }) .target(`src/compiled/react${packageSuffix}/cjs`) yield task @@ -1763,22 +1763,22 @@ export async function copy_vendor_react(task_) { yield task .source(join(reactDomDir, 'cjs/**/*.js')) // eslint-disable-next-line require-yield - .run({ every: true }, function* (file) { - const source = file.data.toString() - // We replace the module/chunk loading code with our own implementation in Next.js. - file.data = source - .replace( - /require\(["']scheduler["']\)/g, - `require("next/dist/compiled/scheduler${packageSuffix}")` - ) - .replace( - /require\(["']react["']\)/g, - `require("next/dist/compiled/react${packageSuffix}")` - ) - - // Note that we don't replace `react-dom` with `next/dist/compiled/react-dom` - // as it mighe be aliased to the server rendering stub. - }) + // .run({ every: true }, function* (file) { + // const source = file.data.toString() + // // We replace the module/chunk loading code with our own implementation in Next.js. + // file.data = source + // .replace( + // /require\(["']scheduler["']\)/g, + // `require("next/dist/compiled/scheduler${packageSuffix}")` + // ) + // .replace( + // /require\(["']react["']\)/g, + // `require("next/dist/compiled/react${packageSuffix}")` + // ) + + // // Note that we don't replace `react-dom` with `next/dist/compiled/react-dom` + // // as it mighe be aliased to the server rendering stub. + // }) .target(`src/compiled/react-dom${packageSuffix}/cjs`) // Remove unused files @@ -2662,38 +2662,137 @@ export async function release(task) { await task.clear('dist').start('build') } -export async function next_bundle_prod(task, opts) { +export async function next_bundle_app_turbo(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + turbo: true, + bundleType: 'app', + }), + name: 'next-bundle-app-turbo', + }) +} + +export async function next_bundle_app_prod(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + dev: false, + bundleType: 'app', + }), + name: 'next-bundle-app-prod', + }) +} + +export async function next_bundle_app_dev(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + dev: true, + bundleType: 'app', + }), + name: 'next-bundle-app-dev', + }) +} + +export async function next_bundle_app_turbo_experimental(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + turbo: true, + bundleType: 'app', + experimental: true, + }), + name: 'next-bundle-app-turbo-experimental', + }) +} + +export async function next_bundle_app_prod_experimental(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + dev: false, + bundleType: 'app', + experimental: true, + }), + name: 'next-bundle-app-prod-experimental', + }) +} + +export async function next_bundle_app_dev_experimental(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + dev: true, + bundleType: 'app', + experimental: true, + }), + name: 'next-bundle-app-dev-experimental', + }) +} + +export async function next_bundle_pages_prod(task, opts) { await task.source('dist').webpack({ watch: opts.dev, config: require('./webpack.config')({ dev: false, + bundleType: 'pages', }), - name: 'next-bundle-prod', + name: 'next-bundle-pages-prod', }) } -export async function next_bundle_dev(task, opts) { +export async function next_bundle_pages_dev(task, opts) { await task.source('dist').webpack({ watch: opts.dev, config: require('./webpack.config')({ dev: true, + bundleType: 'pages', }), - name: 'next-bundle-dev', + name: 'next-bundle-pages-dev', }) } -export async function next_bundle_turbo_prod(task, opts) { +export async function next_bundle_pages_turbo(task, opts) { await task.source('dist').webpack({ watch: opts.dev, config: require('./webpack.config')({ turbo: true, + bundleType: 'pages', + }), + name: 'next-bundle-pages-turbo', + }) +} + +export async function next_bundle_server(task, opts) { + await task.source('dist').webpack({ + watch: opts.dev, + config: require('./webpack.config')({ + dev: false, + bundleType: 'server', }), - name: 'next-bundle-prod-turbo', + name: 'next-bundle-server', }) } + export async function next_bundle(task, opts) { await task.parallel( - ['next_bundle_prod', 'next_bundle_dev', 'next_bundle_turbo_prod'], + [ + // builds the app (route/page) bundles + 'next_bundle_app_turbo', + 'next_bundle_app_prod', + 'next_bundle_app_dev', + // builds the app (route/page) bundles with react experimental + 'next_bundle_app_turbo_experimental', + 'next_bundle_app_prod_experimental', + 'next_bundle_app_dev_experimental', + // builds the pages (page/api) bundles + 'next_bundle_pages_prod', + 'next_bundle_pages_dev', + 'next_bundle_pages_turbo', + // builds the minimal server + 'next_bundle_server', + ], opts ) } diff --git a/packages/next/types/misc.d.ts b/packages/next/types/misc.d.ts index b22d2bd2957ee..e98312232711d 100644 --- a/packages/next/types/misc.d.ts +++ b/packages/next/types/misc.d.ts @@ -20,8 +20,13 @@ declare module 'next/dist/compiled/react-dom/server' declare module 'next/dist/compiled/react-dom/server.edge' declare module 'next/dist/compiled/react-dom/server.browser' declare module 'next/dist/compiled/browserslist' + declare module 'react-server-dom-webpack/client' declare module 'react-server-dom-webpack/server.edge' +declare module 'react-server-dom-webpack/server.node' +declare module 'react-server-dom-webpack/client.edge' + +declare module 'react-dom/server-rendering-stub' declare module 'react-dom/server.browser' declare module 'react-dom/server.edge' diff --git a/packages/next/webpack.config.js b/packages/next/webpack.config.js index 83fe9d3009877..e00fe36188065 100644 --- a/packages/next/webpack.config.js +++ b/packages/next/webpack.config.js @@ -3,7 +3,7 @@ const path = require('path') const TerserPlugin = require('terser-webpack-plugin') const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer') -const minimalExternals = [ +const pagesExternals = [ 'react', 'react/package.json', 'react/jsx-runtime', @@ -18,6 +18,33 @@ const minimalExternals = [ 'react-server-dom-webpack/client.edge', 'react-server-dom-webpack/server.edge', 'react-server-dom-webpack/server.node', +] + +function makeAppAliases(reactChannel) { + const alias = { + react$: `next/dist/compiled/react${reactChannel}`, + 'react/shared-subset$': `next/dist/compiled/react${reactChannel}/react.shared-subset`, + '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/server.edge$': `next/dist/compiled/react-dom${reactChannel}/server.edge`, + 'react-dom/server.browser$': `next/dist/compiled/react-dom${reactChannel}/server.browser`, + '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`, + } + + return alias +} + +const appAliases = makeAppAliases('') +const appExperimentalAliases = makeAppAliases('-experimental') + +const sharedExternals = [ 'styled-jsx', 'styled-jsx/style', '@opentelemetry/api', @@ -44,7 +71,33 @@ const externalsRegexMap = { '(.*)trace/tracer$': 'next/dist/server/lib/trace/tracer', } -module.exports = ({ dev, turbo }) => { +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 ( @@ -72,32 +125,14 @@ module.exports = ({ dev, turbo }) => { /** @type {webpack.Configuration} */ return { - entry: { - server: path.join(__dirname, 'dist/esm/server/next-server.js'), - '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: 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' - ), - }, + entry: bundleTypes[bundleType], target: 'node', mode: 'production', output: { path: path.join(__dirname, 'dist/compiled/next-server'), - filename: `[name]${turbo ? '-turbo' : ''}.runtime.${ - dev ? 'dev' : 'prod' - }.js`, + filename: `[name]${turbo ? '-turbo' : ''}${ + experimental ? '-experimental' : '' + }.runtime.${dev ? 'dev' : 'prod'}.js`, libraryTarget: 'commonjs2', }, optimization: { @@ -123,6 +158,7 @@ module.exports = ({ dev, turbo }) => { }, plugins: [ new webpack.DefinePlugin({ + // TODO: inject typeof window === 'undefined' 'process.env.NEXT_MINIMAL': JSON.stringify('true'), 'this.serverOptions.experimentalTestProxy': JSON.stringify(false), 'this.minimalMode': JSON.stringify(true), @@ -135,12 +171,81 @@ module.exports = ({ dev, turbo }) => { }), !!process.env.ANALYZE && new BundleAnalyzerPlugin({ - analyzerPort: 8888 + (dev ? 0 : 1) + (turbo ? 1 : 0), + analyzerPort: calculateUniquePort( + dev, + turbo, + experimental, + bundleType + ), + openAnalyzer: false, }), ].filter(Boolean), stats: { optimizationBailout: true, }, - externals: [...minimalExternals, externalsMap, externalHandler], + resolve: { + alias: + bundleType === 'app' + ? experimental + ? appExperimentalAliases + : appAliases + : {}, + }, + module: { + rules: [ + { + include: /vendored\/rsc\/entrypoints/, + resolve: { + conditionNames: ['react-server', '...'], + alias: { + react$: `next/dist/compiled/react${ + experimental ? '-experimental' : '' + }/react.shared-subset`, + }, + }, + layer: 'react-server', + }, + { + issuerLayer: 'react-server', + resolve: { + conditionNames: ['react-server', '...'], + alias: { + react$: `next/dist/compiled/react${ + experimental ? '-experimental' : '' + }/react.shared-subset`, + }, + }, + }, + ], + }, + externals: [ + ...sharedExternals, + ...(bundleType === 'pages' ? pagesExternals : []), + 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 +}