rsnext/packages/next/build/webpack/plugins/wellknown-errors-plugin/index.ts
Shu Ding 9baf651154
Improve error messages of server compilation (#41136)
We used to format RSC errors (which are from the SWC loader) before outputting them to the CLI. This PR moves that to a better place in `WellknownErrorPlugin`.

## 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)
2022-10-03 20:19:25 +00:00

31 lines
923 B
TypeScript

import type { webpack } from 'next/dist/compiled/webpack/webpack'
import { getModuleBuildError } from './webpackModuleError'
const NAME = 'WellKnownErrorsPlugin'
export class WellKnownErrorsPlugin {
apply(compiler: webpack.Compiler) {
compiler.hooks.compilation.tap(NAME, (compilation) => {
compilation.hooks.afterSeal.tapPromise(NAME, async () => {
if (compilation.errors?.length) {
await Promise.all(
compilation.errors.map(async (err, i) => {
try {
const moduleError = await getModuleBuildError(
compiler,
compilation,
err
)
if (moduleError !== false) {
compilation.errors[i] = moduleError
}
} catch (e) {
console.log(e)
}
})
)
}
})
})
}
}