Revert part of #45971 (#46071)

It turns out that our module resolution is pretty complicated, and if a
package can't be resolved with a certain configuration, doesn't 100%
mean it can't be bundled.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/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`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
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`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
This commit is contained in:
Shu Ding 2023-02-18 02:41:59 +01:00 committed by GitHub
parent ce1e5de258
commit 4acdd3485d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 44 deletions

View file

@ -2,7 +2,6 @@ import React from 'react'
import ReactRefreshWebpackPlugin from 'next/dist/compiled/@next/react-refresh-utils/dist/ReactRefreshWebpackPlugin'
import chalk from 'next/dist/compiled/chalk'
import crypto from 'crypto'
import { builtinModules } from 'module'
import { webpack } from 'next/dist/compiled/webpack/webpack'
import path from 'path'
import { escapeStringRegexp } from '../shared/lib/escape-regexp'
@ -1247,39 +1246,6 @@ export default async function getBaseWebpackConfig(
// If the request cannot be resolved we need to have
// webpack "bundle" it so it surfaces the not found error.
if (!res) {
// We tried to bundle this request in the server layer but it failed.
// In this case we need to display a helpful error message for people to
// add the package to the `serverComponentsExternalPackages` config.
if (layer === WEBPACK_LAYERS.server && !request.startsWith('#')) {
if (/[/\\]node_modules[/\\]/.test(context)) {
// Built-in modules can be safely ignored.
if (!builtinModules.includes(request)) {
// Try to match the package name from the context path:
// - /node_modules/package-name/...
// - /node_modules/.pnpm/package-name@version/...
// - /node_modules/@scope/package-name/...
// - /node_modules/.pnpm/scope+package-name@version/...
const packageName =
context.match(
/[/\\]node_modules[/\\](\.pnpm[/\\])?([^/\\@]+)/
)?.[2] ||
context.match(
/[/\\]node_modules[/\\](@[^/\\]+[/\\][^/\\]+)[/\\]/
)?.[2] ||
context
.match(
/[/\\]node_modules[/\\]\.pnpm[/\\](@[^/\\@]+\+[^/\\@]+)/
)?.[1]
?.replace(/\+/g, '/')
throw new Error(
`Failed to bundle ${
packageName ? `package "${packageName}"` : context
} in Server Components because it uses "${request}". Please try adding it to the \`serverComponentsExternalPackages\` config: https://beta.nextjs.org/docs/api-reference/next.config.js#servercomponentsexternalpackages`
)
}
}
}
return
}

View file

@ -193,25 +193,18 @@ export default function formatWebpackMessages(json: any, verbose?: boolean) {
})
// Reorder errors to put the most relevant ones first.
let reactServerComponentsBundleError = -1
let reactServerComponentsError = -1
for (let i = 0; i < formattedErrors.length; i++) {
const error = formattedErrors[i]
if (error.includes('ReactServerComponentsError')) {
reactServerComponentsError = i
}
if (/Failed to bundle .+ in Server Components/.test(error)) {
reactServerComponentsBundleError = i
break
}
}
// Move the reactServerComponentsBundleError to the top, if it exists
// Otherwise, move the reactServerComponentsError to the top if it exists
if (reactServerComponentsBundleError !== -1) {
const error = formattedErrors.splice(reactServerComponentsBundleError, 1)
formattedErrors.unshift(error[0])
} else if (reactServerComponentsError !== -1) {
// Move the reactServerComponentsError to the top if it exists
if (reactServerComponentsError !== -1) {
const error = formattedErrors.splice(reactServerComponentsError, 1)
formattedErrors.unshift(error[0])
}