Unverified Commit 4adab6a6 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve server bundling strategy (#41584)

This PR adds a list of popular packages that should always opt-out
bundling in the server layer.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent bad909e4
...@@ -13,6 +13,7 @@ import { ...@@ -13,6 +13,7 @@ import {
WEBPACK_LAYERS, WEBPACK_LAYERS,
RSC_MOD_REF_PROXY_ALIAS, RSC_MOD_REF_PROXY_ALIAS,
} from '../lib/constants' } from '../lib/constants'
import { EXTERNAL_PACKAGES } from '../lib/server-external-packages'
import { fileExists } from '../lib/file-exists' import { fileExists } from '../lib/file-exists'
import { CustomRoutes } from '../lib/load-custom-routes.js' import { CustomRoutes } from '../lib/load-custom-routes.js'
import { import {
...@@ -1008,6 +1009,10 @@ export default async function getBaseWebpackConfig( ...@@ -1008,6 +1009,10 @@ export default async function getBaseWebpackConfig(
const crossOrigin = config.crossOrigin const crossOrigin = config.crossOrigin
const looseEsmExternals = config.experimental?.esmExternals === 'loose' const looseEsmExternals = config.experimental?.esmExternals === 'loose'
const optoutBundlingPackages = EXTERNAL_PACKAGES.concat(
...(config.experimental.serverComponentsExternalPackages || [])
)
async function handleExternals( async function handleExternals(
context: string, context: string,
request: string, request: string,
...@@ -1166,12 +1171,7 @@ export default async function getBaseWebpackConfig( ...@@ -1166,12 +1171,7 @@ export default async function getBaseWebpackConfig(
if (/node_modules[/\\].*\.[mc]?js$/.test(res)) { if (/node_modules[/\\].*\.[mc]?js$/.test(res)) {
if (layer === WEBPACK_LAYERS.server) { if (layer === WEBPACK_LAYERS.server) {
// All packages should be bundled for the server layer if they're not opted out. // All packages should be bundled for the server layer if they're not opted out.
if ( if (isResourceInPackages(res, optoutBundlingPackages)) {
isResourceInPackages(
res,
config.experimental.serverComponentsExternalPackages
)
) {
return `${externalType} ${request}` return `${externalType} ${request}`
} }
...@@ -1545,10 +1545,7 @@ export default async function getBaseWebpackConfig( ...@@ -1545,10 +1545,7 @@ export default async function getBaseWebpackConfig(
// bundling, don't resolve it. // bundling, don't resolve it.
if ( if (
!codeCondition.test.test(req) || !codeCondition.test.test(req) ||
isResourceInPackages( isResourceInPackages(req, optoutBundlingPackages)
req,
config.experimental.serverComponentsExternalPackages
)
) { ) {
return false return false
} }
...@@ -1612,10 +1609,7 @@ export default async function getBaseWebpackConfig( ...@@ -1612,10 +1609,7 @@ export default async function getBaseWebpackConfig(
// bundling, don't resolve it. // bundling, don't resolve it.
if ( if (
!codeCondition.test.test(req) || !codeCondition.test.test(req) ||
isResourceInPackages( isResourceInPackages(req, optoutBundlingPackages)
req,
config.experimental.serverComponentsExternalPackages
)
) { ) {
return false return false
} }
......
// 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',
]
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