Skip to content

Commit

Permalink
bundle server layer but not client layer
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 24, 2022
1 parent 57012e4 commit 925811b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
43 changes: 23 additions & 20 deletions packages/next/build/webpack-config.ts
Expand Up @@ -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/
Expand Down Expand Up @@ -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') {
Expand All @@ -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}`
}

Expand All @@ -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
Expand Down Expand Up @@ -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',
},
},
},
Expand Down Expand Up @@ -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',
},
Expand Down
2 changes: 0 additions & 2 deletions packages/next/client/components/navigation/client.ts
@@ -1,5 +1,3 @@
'use client'

import { useContext, useMemo } from 'react'
import {
SearchParamsContext,
Expand Down
1 change: 1 addition & 0 deletions 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'
Expand Down
2 changes: 0 additions & 2 deletions 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'
Expand Down

0 comments on commit 925811b

Please sign in to comment.