rsnext/packages/next/build/compiler.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

73 lines
2.2 KiB
TypeScript

import { webpack } from 'next/dist/compiled/webpack/webpack'
import type { webpack5 } from 'next/dist/compiled/webpack/webpack'
import { Span } from '../trace'
export type CompilerResult = {
errors: webpack5.StatsError[]
warnings: webpack5.StatsError[]
stats: webpack5.Stats | undefined
}
function generateStats(
result: CompilerResult,
stat: webpack5.Stats
): CompilerResult {
const { errors, warnings } = stat.toJson({
preset: 'errors-warnings',
moduleTrace: true,
})
if (errors && errors.length > 0) {
result.errors.push(...errors)
}
if (warnings && warnings.length > 0) {
result.warnings.push(...warnings)
}
return result
}
// Webpack 5 requires the compiler to be closed (to save caches)
// Webpack 4 does not have this close method so in order to be backwards compatible we check if it exists
function closeCompiler(compiler: webpack5.Compiler | webpack5.MultiCompiler) {
return new Promise<void>((resolve, reject) => {
// @ts-ignore Close only exists on the compiler in webpack 5
return compiler.close((err: any) => (err ? reject(err) : resolve()))
})
}
export function runCompiler(
config: webpack.Configuration,
{ runWebpackSpan }: { runWebpackSpan: Span }
): Promise<CompilerResult> {
return new Promise((resolve, reject) => {
const compiler = webpack(config) as unknown as webpack5.Compiler
compiler.run((err, stats) => {
const webpackCloseSpan = runWebpackSpan.traceChild('webpack-close', {
name: config.name,
})
webpackCloseSpan
.traceAsyncFn(() => closeCompiler(compiler))
.then(() => {
if (err) {
const reason = err.stack ?? err.toString()
if (reason) {
return resolve({
errors: [{ message: reason, details: (err as any).details }],
warnings: [],
stats,
})
}
return reject(err)
} else if (!stats) throw new Error('No Stats from webpack')
const result = webpackCloseSpan
.traceChild('webpack-generate-error-stats')
.traceFn(() =>
generateStats({ errors: [], warnings: [], stats }, stats)
)
return resolve(result)
})
})
})
}