rsnext/packages/next/build/webpack/plugins/wellknown-errors-plugin/parseRSC.ts
Jiachi Liu 8db5991714
Update rsc error message (#41351)
Follow up for #41333, updating the error message with `"use client"` directives in suggestion
2022-10-12 08:35:50 +00:00

90 lines
3.3 KiB
TypeScript

import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { relative } from 'path'
import { SimpleWebpackError } from './simpleWebpackError'
export function formatRSCErrorMessage(
message: string
): null | [string, string] {
if (message && /NEXT_RSC_ERR_/.test(message)) {
let formattedMessage = message
let formattedVerboseMessage = ''
// Comes from the "React Server Components" transform in SWC, always
// attach the module trace.
const NEXT_RSC_ERR_REACT_API = /.+NEXT_RSC_ERR_REACT_API: (.*?)\n/s
const NEXT_RSC_ERR_SERVER_IMPORT = /.+NEXT_RSC_ERR_SERVER_IMPORT: (.*?)\n/s
const NEXT_RSC_ERR_CLIENT_IMPORT = /.+NEXT_RSC_ERR_CLIENT_IMPORT: (.*?)\n/s
if (NEXT_RSC_ERR_REACT_API.test(message)) {
formattedMessage = message.replace(
NEXT_RSC_ERR_REACT_API,
`\n\nYou're importing a component that needs $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\n\n`
)
formattedVerboseMessage =
'\n\nMaybe one of these should be marked as a client entry with "use client":\n'
} else if (NEXT_RSC_ERR_SERVER_IMPORT.test(message)) {
formattedMessage = message.replace(
NEXT_RSC_ERR_SERVER_IMPORT,
`\n\nYou're importing a component that imports $1. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.\n\n`
)
formattedVerboseMessage =
'\n\nMaybe one of these should be marked as a client entry "use client":\n'
} else if (NEXT_RSC_ERR_CLIENT_IMPORT.test(message)) {
formattedMessage = message.replace(
NEXT_RSC_ERR_CLIENT_IMPORT,
`\n\nYou're importing a component that needs $1. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.\n\n`
)
formattedVerboseMessage =
'\n\nOne of these is marked as a client entry with "use client":\n'
}
return [formattedMessage, formattedVerboseMessage]
}
return null
}
// Check if the error is specifically related to React Server Components.
// If so, we'll format the error message to be more helpful.
export function getRscError(
fileName: string,
err: Error,
module: any,
compilation: webpack.Compilation,
compiler: webpack.Compiler
): SimpleWebpackError | false {
const formattedError = formatRSCErrorMessage(err.message)
if (!formattedError) return false
// Get the module trace:
// https://cs.github.com/webpack/webpack/blob/9fcaa243573005d6fdece9a3f8d89a0e8b399613/lib/stats/DefaultStatsFactoryPlugin.js#L414
const visitedModules = new Set()
const moduleTrace = []
let current = module
while (current) {
if (visitedModules.has(current)) break
visitedModules.add(current)
moduleTrace.push(current)
const origin = compilation.moduleGraph.getIssuer(current)
if (!origin) break
current = origin
}
const error = new SimpleWebpackError(
fileName,
formattedError[0] +
formattedError[1] +
moduleTrace
.map((m) =>
m.resource ? ' ' + relative(compiler.context, m.resource) : ''
)
.filter(Boolean)
.join('\n')
)
// Delete the stack because it's created here.
error.stack = ''
return error
}