Skip to content

Commit

Permalink
Improve server bundling strategy (vercel#41584)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
shuding authored and Kikobeats committed Oct 24, 2022
1 parent da666e1 commit 0abbd5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
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',
]

0 comments on commit 0abbd5b

Please sign in to comment.