Unverified Commit 7297942c authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Remove edge shared deps (#41413)

Since we have already bundled dependencies for server layer, so the
shared deps chunk group is not needed anymore.
Also changing to esm assets import with next internals for edge function
and middleware build.

The changes are originally from #41337, try to land them separately.
parent 889af9fd
...@@ -209,7 +209,10 @@ export function getEdgeServerEntry(opts: { ...@@ -209,7 +209,10 @@ export function getEdgeServerEntry(opts: {
return { return {
import: `next-edge-ssr-loader?${stringify(loaderParams)}!`, import: `next-edge-ssr-loader?${stringify(loaderParams)}!`,
layer: opts.isServerComponent ? WEBPACK_LAYERS.server : undefined, // The Edge bundle includes the server in its entrypoint, so it has to
// be in the SSR layer — we later convert the page request to the RSC layer
// via a webpack rule.
layer: undefined,
} }
} }
......
...@@ -86,9 +86,6 @@ const BABEL_CONFIG_FILES = [ ...@@ -86,9 +86,6 @@ const BABEL_CONFIG_FILES = [
'babel.config.cjs', 'babel.config.cjs',
] ]
const rscSharedRegex =
/(node_modules[\\/]react\/|[\\/]shared[\\/]lib[\\/](head-manager-context|router-context|server-inserted-html)\.js|node_modules[\\/]styled-jsx[\\/])/
// Support for NODE_PATH // Support for NODE_PATH
const nodePathList = (process.env.NODE_PATH || '') const nodePathList = (process.env.NODE_PATH || '')
.split(process.platform === 'win32' ? ';' : ':') .split(process.platform === 'win32' ? ';' : ':')
...@@ -1310,39 +1307,18 @@ export default async function getBaseWebpackConfig( ...@@ -1310,39 +1307,18 @@ export default async function getBaseWebpackConfig(
...(config.experimental.optimizeCss ? [] : ['critters']), ...(config.experimental.optimizeCss ? [] : ['critters']),
], ],
optimization: { optimization: {
// @ts-ignore: TODO remove ts-ignore when webpack 4 is removed
emitOnErrors: !dev, emitOnErrors: !dev,
checkWasmTypes: false, checkWasmTypes: false,
nodeEnv: false, nodeEnv: false,
splitChunks: ((): splitChunks: (():
| Required<webpack.Configuration>['optimization']['splitChunks'] | Required<webpack.Configuration>['optimization']['splitChunks']
| false => { | false => {
// For the edge runtime, we have to bundle all dependencies inside without dynamic `require`s.
// To make some dependencies like `react` to be shared between entrypoints, we use a special
// cache group here even under dev mode.
const edgeRSCCacheGroups = hasServerComponents
? {
rscDeps: {
enforce: true,
name: 'rsc-runtime-deps',
filename: 'rsc-runtime-deps.js',
test: rscSharedRegex,
},
}
: undefined
if (isEdgeServer && edgeRSCCacheGroups) {
return {
cacheGroups: edgeRSCCacheGroups,
}
}
if (dev) { if (dev) {
return false return false
} }
if (isNodeServer) { if (isNodeServer) {
return { return {
// @ts-ignore
filename: '[name].js', filename: '[name].js',
chunks: 'all', chunks: 'all',
minSize: 1000, minSize: 1000,
...@@ -1351,11 +1327,8 @@ export default async function getBaseWebpackConfig( ...@@ -1351,11 +1327,8 @@ export default async function getBaseWebpackConfig(
if (isEdgeServer) { if (isEdgeServer) {
return { return {
// @ts-ignore
filename: 'edge-chunks/[name].js', filename: 'edge-chunks/[name].js',
chunks: 'all',
minChunks: 2, minChunks: 2,
cacheGroups: edgeRSCCacheGroups,
} }
} }
...@@ -1471,7 +1444,6 @@ export default async function getBaseWebpackConfig( ...@@ -1471,7 +1444,6 @@ export default async function getBaseWebpackConfig(
}, },
context: dir, context: dir,
// Kept as function to be backwards compatible // Kept as function to be backwards compatible
// @ts-ignore TODO webpack 5 typings needed
entry: async () => { entry: async () => {
return { return {
...(clientEntries ? clientEntries : {}), ...(clientEntries ? clientEntries : {}),
...@@ -1599,6 +1571,17 @@ export default async function getBaseWebpackConfig( ...@@ -1599,6 +1571,17 @@ export default async function getBaseWebpackConfig(
} as any, } as any,
] ]
: []), : []),
...(hasAppDir && isEdgeServer
? [
// The Edge bundle includes the server in its entrypoint, so it has to
// be in the SSR layer — here we convert the actual page request to
// the RSC layer via a webpack rule.
{
resourceQuery: /__edge_ssr_entry__/,
layer: WEBPACK_LAYERS.server,
},
]
: []),
// Alias `next/dynamic` to React.lazy implementation for RSC // Alias `next/dynamic` to React.lazy implementation for RSC
...(hasServerComponents ...(hasServerComponents
? [ ? [
...@@ -1631,16 +1614,6 @@ export default async function getBaseWebpackConfig( ...@@ -1631,16 +1614,6 @@ export default async function getBaseWebpackConfig(
}, },
] ]
: []), : []),
...(hasServerComponents && isEdgeServer
? [
// Move shared dependencies from sc_server and sc_client into the
// same layer.
{
test: rscSharedRegex,
layer: WEBPACK_LAYERS.rscShared,
},
]
: []),
{ {
test: /\.(js|cjs|mjs)$/, test: /\.(js|cjs|mjs)$/,
issuerLayer: WEBPACK_LAYERS.api, issuerLayer: WEBPACK_LAYERS.api,
......
...@@ -18,7 +18,7 @@ export default function middlewareLoader(this: any) { ...@@ -18,7 +18,7 @@ export default function middlewareLoader(this: any) {
buildInfo.rootDir = rootDir buildInfo.rootDir = rootDir
return ` return `
import { adapter, enhanceGlobals } from 'next/dist/server/web/adapter' import { adapter, enhanceGlobals } from 'next/dist/esm/server/web/adapter'
enhanceGlobals() enhanceGlobals()
......
...@@ -85,7 +85,7 @@ export default async function edgeSSRLoader(this: any) { ...@@ -85,7 +85,7 @@ export default async function edgeSSRLoader(this: any) {
const pageModPath = `${appDirLoader}${stringifiedPagePath.substring( const pageModPath = `${appDirLoader}${stringifiedPagePath.substring(
1, 1,
stringifiedPagePath.length - 1 stringifiedPagePath.length - 1
)}` )}${isAppDir ? '?__edge_ssr_entry__' : ''}`
const transformed = ` const transformed = `
import { adapter, enhanceGlobals } from 'next/dist/esm/server/web/adapter' import { adapter, enhanceGlobals } from 'next/dist/esm/server/web/adapter'
......
...@@ -40,7 +40,7 @@ export default function middlewareLoader(this: any) { ...@@ -40,7 +40,7 @@ export default function middlewareLoader(this: any) {
buildInfo.rootDir = rootDir buildInfo.rootDir = rootDir
return ` return `
import { adapter, blockUnallowedResponse, enhanceGlobals } from 'next/dist/server/web/adapter' import { adapter, blockUnallowedResponse, enhanceGlobals } from 'next/dist/esm/server/web/adapter'
enhanceGlobals() enhanceGlobals()
......
...@@ -73,7 +73,6 @@ export const WEBPACK_LAYERS = { ...@@ -73,7 +73,6 @@ export const WEBPACK_LAYERS = {
server: 'sc_server', server: 'sc_server',
client: 'sc_client', client: 'sc_client',
api: 'api', api: 'api',
rscShared: 'rsc_shared_deps',
middleware: 'middleware', middleware: 'middleware',
edgeAsset: 'edge-asset', edgeAsset: 'edge-asset',
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment