rsnext/packages/next/build/webpack/plugins/wellknown-errors-plugin/webpackModuleError.ts
Naoyuki Kanezawa 9e53af8e30
Fix next node buildin module error message for edge runtime (#36434)
- improve the message for importing node builtin module on edge runtime
- fix to show the message on overlay of error browser with `next dev`
- fix https://github.com/vercel/next.js/issues/36237

The message is NOT shown when using edge runtime (not middleware) since I cannot find a way to detect a webpack compilation is for edge runtime.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have 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 helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
2022-05-02 15:21:40 +00:00

90 lines
2.3 KiB
TypeScript

import { readFileSync } from 'fs'
import * as path from 'path'
import type { webpack5 as webpack } from 'next/dist/compiled/webpack/webpack'
import { getBabelError } from './parseBabel'
import { getCssError } from './parseCss'
import { getScssError } from './parseScss'
import { getNotFoundError } from './parseNotFoundError'
import { SimpleWebpackError } from './simpleWebpackError'
import isError from '../../../../lib/is-error'
import { NextConfig } from '../../../../server/config-shared'
function getFileData(
compilation: webpack.Compilation,
m: any
): [string, string | null] {
let resolved: string
let ctx: string | null = compilation.compiler?.context ?? null
if (ctx !== null && typeof m.resource === 'string') {
const res = path.relative(ctx, m.resource).replace(/\\/g, path.posix.sep)
resolved = res.startsWith('.') ? res : `.${path.posix.sep}${res}`
} else {
const requestShortener = compilation.requestShortener
if (typeof m?.readableIdentifier === 'function') {
resolved = m.readableIdentifier(requestShortener)
} else {
resolved = m.request ?? m.userRequest
}
}
if (resolved) {
let content: string | null = null
try {
content = readFileSync(
ctx ? path.resolve(ctx, resolved) : resolved,
'utf8'
)
} catch {}
return [resolved, content]
}
return ['<unknown>', null]
}
export async function getModuleBuildError(
compilation: webpack.Compilation,
input: any,
config: NextConfig
): Promise<SimpleWebpackError | false> {
if (
!(
typeof input === 'object' &&
(input?.name === 'ModuleBuildError' ||
input?.name === 'ModuleNotFoundError') &&
Boolean(input.module) &&
isError(input.error)
)
) {
return false
}
const err: Error = input.error
const [sourceFilename, sourceContent] = getFileData(compilation, input.module)
const notFoundError = await getNotFoundError(
compilation,
input,
sourceFilename,
config
)
if (notFoundError !== false) {
return notFoundError
}
const babel = getBabelError(sourceFilename, err)
if (babel !== false) {
return babel
}
const css = getCssError(sourceFilename, err)
if (css !== false) {
return css
}
const scss = getScssError(sourceFilename, sourceContent, err)
if (scss !== false) {
return scss
}
return false
}