Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve server bundling strategy #41584

Merged
merged 1 commit into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 8 additions & 14 deletions packages/next/build/webpack-config.ts
Expand Up @@ -13,6 +13,7 @@ import {
WEBPACK_LAYERS,
RSC_MOD_REF_PROXY_ALIAS,
} from '../lib/constants'
import { EXTERNAL_PACKAGES } from '../lib/server-external-packages'
import { fileExists } from '../lib/file-exists'
import { CustomRoutes } from '../lib/load-custom-routes.js'
import {
Expand Down Expand Up @@ -1008,6 +1009,10 @@ export default async function getBaseWebpackConfig(
const crossOrigin = config.crossOrigin
const looseEsmExternals = config.experimental?.esmExternals === 'loose'

const optoutBundlingPackages = EXTERNAL_PACKAGES.concat(
...(config.experimental.serverComponentsExternalPackages || [])
)

async function handleExternals(
context: string,
request: string,
Expand Down Expand Up @@ -1166,12 +1171,7 @@ export default async function getBaseWebpackConfig(
if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
if (layer === WEBPACK_LAYERS.server) {
// All packages should be bundled for the server layer if they're not opted out.
if (
isResourceInPackages(
res,
config.experimental.serverComponentsExternalPackages
)
) {
if (isResourceInPackages(res, optoutBundlingPackages)) {
return `${externalType} ${request}`
}

Expand Down Expand Up @@ -1545,10 +1545,7 @@ export default async function getBaseWebpackConfig(
// bundling, don't resolve it.
if (
!codeCondition.test.test(req) ||
isResourceInPackages(
req,
config.experimental.serverComponentsExternalPackages
)
isResourceInPackages(req, optoutBundlingPackages)
) {
return false
}
Expand Down Expand Up @@ -1612,10 +1609,7 @@ export default async function getBaseWebpackConfig(
// bundling, don't resolve it.
if (
!codeCondition.test.test(req) ||
isResourceInPackages(
req,
config.experimental.serverComponentsExternalPackages
)
isResourceInPackages(req, optoutBundlingPackages)
) {
return false
}
Expand Down
20 changes: 20 additions & 0 deletions packages/next/lib/server-external-packages.ts
@@ -0,0 +1,20 @@
// A list of popular packages that cannot be bundled on the server.
export const EXTERNAL_PACKAGES = [
'eslint',
'typescript',
'prettier',
'postcss',
'jest',
'autoprefixer',
'tailwindcss',
'sharp',
'express',
'ts-node',
'webpack',
'cypress',
'@sentry/nextjs',
'@sentry/node',
'next-seo',
'rimraf',
'next-mdx-remote',
]