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

Revert part of #45971 #46071

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1247,39 +1247,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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down