From 925811b6131e52690cc66194459222901275872f Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 24 Oct 2022 15:24:19 -0700 Subject: [PATCH] bundle server layer but not client layer --- packages/next/build/webpack-config.ts | 43 ++++++++++--------- .../client/components/navigation/client.ts | 2 - .../client/components/navigation/index.ts | 1 + .../next/shared/lib/app-router-context.ts | 2 - 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 0fcb675480c6b..238f6a2844773 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -76,7 +76,8 @@ const babelIncludeRegexes: RegExp[] = [ /[\\/](strip-ansi|ansi-regex|styled-jsx)[\\/]/, ] -const reactPackagesRegex = /^(react(?:$|\/)|react-dom(?:$|\/)|scheduler$)/ +const reactPackagesRegex = /^(react(?:$|\/)|react-dom(?:$|\/))/ +// /^(react(?:$|\/)|react-dom(?:$|\/))/ const staticGenerationAsyncStorageRegex = /next[\\/]dist[\\/]client[\\/]components[\\/]static-generation-async-storage/ @@ -1060,9 +1061,6 @@ export default async function getBaseWebpackConfig( // absolute paths. (process.platform === 'win32' && path.win32.isAbsolute(request)) - const isRscLayer = - layer === WEBPACK_LAYERS.server || layer === WEBPACK_LAYERS.client - // make sure import "next" shows a warning when imported // in pages/components if (request === 'next') { @@ -1087,11 +1085,7 @@ export default async function getBaseWebpackConfig( if (/^(?:next$)/.test(request)) { return `commonjs ${request}` } - if (reactPackagesRegex.test(request)) { - // override react-dom to server-rendering-stub for server - if (request === 'react-dom' && hasAppDir && !isClient) { - request = 'react-dom/server-rendering-stub' - } + if (/^(react(?:$|\/)|react-dom(?:$|\/))/.test(request)) { return `commonjs ${hasAppDir ? 'next/dist/compiled/' : ''}${request}` } @@ -1118,10 +1112,12 @@ export default async function getBaseWebpackConfig( // we need to process shared `router/router` and `dynamic`, // so that the DefinePlugin can inject process.env values const isNextExternal = - !isRscLayer && - /next[/\\]dist[/\\](shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic))/.test( - localRes - ) + // Treat next internals as non-external for server layer + layer === WEBPACK_LAYERS.server + ? false + : /next[/\\]dist[/\\](shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic))/.test( + localRes + ) if (isNextExternal) { // Generate Next.js external import @@ -1631,10 +1627,8 @@ export default async function getBaseWebpackConfig( // react to the direct file path, not the package name. In that case the condition // will be ignored completely. react: 'next/dist/compiled/react', - 'react-dom$': isClient - ? 'next/dist/compiled/react-dom/index' - : 'next/dist/compiled/react-dom/server-rendering-stub', - 'react-dom/client$': 'next/dist/compiled/react-dom/client', + 'react-dom$': + 'next/dist/compiled/react-dom/server-rendering-stub', }, }, }, @@ -1733,14 +1727,23 @@ export default async function getBaseWebpackConfig( }, }, }, + { + issuerLayer: WEBPACK_LAYERS.client, + test: codeCondition.test, + resolve: { + alias: { + react: 'next/dist/compiled/react', + 'react-dom$': + 'next/dist/compiled/react-dom/server-rendering-stub', + }, + }, + }, { test: codeCondition.test, resolve: { alias: { react: 'next/dist/compiled/react', - 'react-dom$': isClient - ? 'next/dist/compiled/react-dom/index' - : 'next/dist/compiled/react-dom/server-rendering-stub', + 'react-dom$': 'next/dist/compiled/react-dom', 'react-dom/client$': 'next/dist/compiled/react-dom/client', }, diff --git a/packages/next/client/components/navigation/client.ts b/packages/next/client/components/navigation/client.ts index cc5666e1b0ef5..fe677aa9de4d1 100644 --- a/packages/next/client/components/navigation/client.ts +++ b/packages/next/client/components/navigation/client.ts @@ -1,5 +1,3 @@ -'use client' - import { useContext, useMemo } from 'react' import { SearchParamsContext, diff --git a/packages/next/client/components/navigation/index.ts b/packages/next/client/components/navigation/index.ts index ad8894d297758..af0d328133746 100644 --- a/packages/next/client/components/navigation/index.ts +++ b/packages/next/client/components/navigation/index.ts @@ -1,3 +1,4 @@ +'use client' // useLayoutSegments() // Only the segments for the current place. ['children', 'dashboard', 'children', 'integrations'] -> /dashboard/integrations (/dashboard/layout.js would get ['children', 'dashboard', 'children', 'integrations']) import type { FlightRouterState } from '../../../server/app-render' diff --git a/packages/next/shared/lib/app-router-context.ts b/packages/next/shared/lib/app-router-context.ts index 42eb5c0931612..d0a295d764faa 100644 --- a/packages/next/shared/lib/app-router-context.ts +++ b/packages/next/shared/lib/app-router-context.ts @@ -1,5 +1,3 @@ -'use client' - import React from 'react' import type { FocusAndScrollRef } from '../../client/components/reducer' import type { FlightRouterState, FlightData } from '../../server/app-render'