Unverified Commit fc0f94f8 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Revert "Update split chunk handling for edge/node" (#62313)

We have a reproduction of OOMs still occurring with this chunking so
going to revert while we investigate further

x-ref:
https://github.com/vercel/next.js/issues/51298#issuecomment-1953004425

Reverts vercel/next.js#62205

Closes NEXT-2548
parent 0b9d6355
...@@ -715,15 +715,11 @@ export default async function getBaseWebpackConfig( ...@@ -715,15 +715,11 @@ export default async function getBaseWebpackConfig(
// Packages which will be split into the 'framework' chunk. // Packages which will be split into the 'framework' chunk.
// Only top-level packages are included, e.g. nested copies like // Only top-level packages are included, e.g. nested copies like
// 'node_modules/meow/node_modules/object-assign' are not included. // 'node_modules/meow/node_modules/object-assign' are not included.
const nextFrameworkPaths: string[] = []
const topLevelFrameworkPaths: string[] = [] const topLevelFrameworkPaths: string[] = []
const visitedFrameworkPackages = new Set<string>() const visitedFrameworkPackages = new Set<string>()
// Adds package-paths of dependencies recursively // Adds package-paths of dependencies recursively
const addPackagePath = ( const addPackagePath = (packageName: string, relativeToPath: string) => {
packageName: string,
relativeToPath: string,
paths: string[]
) => {
try { try {
if (visitedFrameworkPackages.has(packageName)) { if (visitedFrameworkPackages.has(packageName)) {
return return
...@@ -742,11 +738,11 @@ export default async function getBaseWebpackConfig( ...@@ -742,11 +738,11 @@ export default async function getBaseWebpackConfig(
const directory = path.join(packageJsonPath, '../') const directory = path.join(packageJsonPath, '../')
// Returning from the function in case the directory has already been added and traversed // Returning from the function in case the directory has already been added and traversed
if (paths.includes(directory)) return if (topLevelFrameworkPaths.includes(directory)) return
paths.push(directory) topLevelFrameworkPaths.push(directory)
const dependencies = require(packageJsonPath).dependencies || {} const dependencies = require(packageJsonPath).dependencies || {}
for (const name of Object.keys(dependencies)) { for (const name of Object.keys(dependencies)) {
addPackagePath(name, directory, paths) addPackagePath(name, directory)
} }
} catch (_) { } catch (_) {
// don't error on failing to resolve framework packages // don't error on failing to resolve framework packages
...@@ -763,9 +759,8 @@ export default async function getBaseWebpackConfig( ...@@ -763,9 +759,8 @@ export default async function getBaseWebpackConfig(
] ]
: []), : []),
]) { ]) {
addPackagePath(packageName, dir, topLevelFrameworkPaths) addPackagePath(packageName, dir)
} }
addPackagePath('next', dir, nextFrameworkPaths)
const crossOrigin = config.crossOrigin const crossOrigin = config.crossOrigin
...@@ -918,7 +913,6 @@ export default async function getBaseWebpackConfig( ...@@ -918,7 +913,6 @@ export default async function getBaseWebpackConfig(
splitChunks: ((): splitChunks: (():
| Required<webpack.Configuration>['optimization']['splitChunks'] | Required<webpack.Configuration>['optimization']['splitChunks']
| false => { | false => {
// server chunking
if (dev) { if (dev) {
if (isNodeServer) { if (isNodeServer) {
/* /*
...@@ -969,6 +963,21 @@ export default async function getBaseWebpackConfig( ...@@ -969,6 +963,21 @@ export default async function getBaseWebpackConfig(
return false return false
} }
if (isNodeServer) {
return {
filename: '[name].js',
chunks: 'all',
minChunks: 2,
}
}
if (isEdgeServer) {
return {
filename: 'edge-chunks/[name].js',
minChunks: 2,
}
}
const frameworkCacheGroup = { const frameworkCacheGroup = {
chunks: 'all' as const, chunks: 'all' as const,
name: 'framework', name: 'framework',
...@@ -987,22 +996,6 @@ export default async function getBaseWebpackConfig( ...@@ -987,22 +996,6 @@ export default async function getBaseWebpackConfig(
// becoming a part of the commons chunk) // becoming a part of the commons chunk)
enforce: true, enforce: true,
} }
const nextRuntimeCacheGroup = {
chunks: 'all' as const,
name: 'next-runtime',
test(module: any) {
const resource = module.nameForCondition?.()
return resource
? nextFrameworkPaths.some((pkgPath) =>
resource.startsWith(pkgPath)
)
: false
},
priority: 30,
enforce: true,
}
const libCacheGroup = { const libCacheGroup = {
test(module: { test(module: {
size: Function size: Function
...@@ -1044,20 +1037,6 @@ export default async function getBaseWebpackConfig( ...@@ -1044,20 +1037,6 @@ export default async function getBaseWebpackConfig(
minChunks: 1, minChunks: 1,
reuseExistingChunk: true, reuseExistingChunk: true,
} }
if (isNodeServer || isEdgeServer) {
return {
filename: `${isEdgeServer ? 'edge-chunks/' : ''}[name].js`,
cacheGroups: {
nextRuntime: nextRuntimeCacheGroup,
framework: frameworkCacheGroup,
lib: libCacheGroup,
},
minChunks: 2,
}
}
// client chunking
const cssCacheGroup = { const cssCacheGroup = {
test: /\.(css|sass|scss)$/i, test: /\.(css|sass|scss)$/i,
chunks: 'all' as const, chunks: 'all' as const,
......
...@@ -98,6 +98,16 @@ export class TerserPlugin { ...@@ -98,6 +98,16 @@ export class TerserPlugin {
return false return false
} }
// don't minify _middleware as it can break in some cases
// and doesn't provide too much of a benefit as it's server-side
if (
name.match(
/(edge-runtime-webpack\.js|edge-chunks|middleware\.js$)/
)
) {
return false
}
const { info } = res const { info } = res
// Skip double minimize assets from child compilation // Skip double minimize assets from child compilation
......
...@@ -111,11 +111,11 @@ describe('Middleware Runtime trailing slash', () => { ...@@ -111,11 +111,11 @@ describe('Middleware Runtime trailing slash', () => {
) )
expect(manifest.middleware).toEqual({ expect(manifest.middleware).toEqual({
'/': { '/': {
files: expect.arrayContaining([ files: [
'prerender-manifest.js', 'prerender-manifest.js',
'server/edge-runtime-webpack.js', 'server/edge-runtime-webpack.js',
'server/middleware.js', 'server/middleware.js',
]), ],
name: 'middleware', name: 'middleware',
page: '/', page: '/',
matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }], matchers: [{ regexp: '^/.*$', originalSource: '/:path*' }],
......
...@@ -187,10 +187,10 @@ describe('Switchable runtime', () => { ...@@ -187,10 +187,10 @@ describe('Switchable runtime', () => {
expect(manifest).toMatchObject({ expect(manifest).toMatchObject({
functions: { functions: {
'/api/hello': { '/api/hello': {
files: expect.arrayContaining([ files: [
'server/edge-runtime-webpack.js', 'server/edge-runtime-webpack.js',
'server/pages/api/hello.js', 'server/pages/api/hello.js',
]), ],
name: 'pages/api/hello', name: 'pages/api/hello',
page: '/api/hello', page: '/api/hello',
matchers: [ matchers: [
...@@ -199,10 +199,10 @@ describe('Switchable runtime', () => { ...@@ -199,10 +199,10 @@ describe('Switchable runtime', () => {
wasm: [], wasm: [],
}, },
'/api/edge': { '/api/edge': {
files: expect.arrayContaining([ files: [
'server/edge-runtime-webpack.js', 'server/edge-runtime-webpack.js',
'server/pages/api/edge.js', 'server/pages/api/edge.js',
]), ],
name: 'pages/api/edge', name: 'pages/api/edge',
page: '/api/edge', page: '/api/edge',
matchers: [ matchers: [
...@@ -621,11 +621,11 @@ describe('Switchable runtime', () => { ...@@ -621,11 +621,11 @@ describe('Switchable runtime', () => {
expect(manifest).toMatchObject({ expect(manifest).toMatchObject({
functions: { functions: {
'/api/hello': { '/api/hello': {
files: expect.arrayContaining([ files: [
'prerender-manifest.js', 'prerender-manifest.js',
'server/edge-runtime-webpack.js', 'server/edge-runtime-webpack.js',
'server/pages/api/hello.js', 'server/pages/api/hello.js',
]), ],
name: 'pages/api/hello', name: 'pages/api/hello',
page: '/api/hello', page: '/api/hello',
matchers: [ matchers: [
...@@ -634,11 +634,11 @@ describe('Switchable runtime', () => { ...@@ -634,11 +634,11 @@ describe('Switchable runtime', () => {
wasm: [], wasm: [],
}, },
'/api/edge': { '/api/edge': {
files: expect.arrayContaining([ files: [
'prerender-manifest.js', 'prerender-manifest.js',
'server/edge-runtime-webpack.js', 'server/edge-runtime-webpack.js',
'server/pages/api/edge.js', 'server/pages/api/edge.js',
]), ],
name: 'pages/api/edge', name: 'pages/api/edge',
page: '/api/edge', page: '/api/edge',
matchers: [ matchers: [
......
...@@ -50,6 +50,11 @@ describe('build trace with extra entries', () => { ...@@ -50,6 +50,11 @@ describe('build trace with extra entries', () => {
expect(appTrace.files.some((file) => file.endsWith('hello.json'))).toBe( expect(appTrace.files.some((file) => file.endsWith('hello.json'))).toBe(
true true
) )
expect(
appTrace.files.filter(
(file) => file.includes('chunks') && file.endsWith('.js')
).length
).toBe(0)
expect( expect(
indexTrace.files.filter( indexTrace.files.filter(
......
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