2021-10-24 23:04:26 +02:00
|
|
|
import type { webpack5 as webpack } from 'next/dist/compiled/webpack/webpack'
|
2020-05-13 17:43:41 +02:00
|
|
|
import { getModuleBuildError } from './webpackModuleError'
|
2022-05-02 17:21:40 +02:00
|
|
|
import { NextConfig } from '../../../../server/config-shared'
|
2020-05-13 17:43:41 +02:00
|
|
|
|
|
|
|
export class WellKnownErrorsPlugin {
|
2022-05-02 17:21:40 +02:00
|
|
|
config: NextConfig
|
|
|
|
|
|
|
|
constructor({ config }: { config: NextConfig }) {
|
|
|
|
this.config = config
|
|
|
|
}
|
|
|
|
|
2021-10-24 23:04:26 +02:00
|
|
|
apply(compiler: webpack.Compiler) {
|
2020-05-18 21:24:37 +02:00
|
|
|
compiler.hooks.compilation.tap('WellKnownErrorsPlugin', (compilation) => {
|
2020-08-05 21:11:35 +02:00
|
|
|
compilation.hooks.afterSeal.tapPromise(
|
|
|
|
'WellKnownErrorsPlugin',
|
|
|
|
async () => {
|
|
|
|
if (compilation.errors?.length) {
|
2021-10-22 21:32:25 +02:00
|
|
|
await Promise.all(
|
|
|
|
compilation.errors.map(async (err, i) => {
|
2020-08-05 21:11:35 +02:00
|
|
|
try {
|
|
|
|
const moduleError = await getModuleBuildError(
|
|
|
|
compilation,
|
2022-05-02 17:21:40 +02:00
|
|
|
err,
|
|
|
|
this.config
|
2020-08-05 21:11:35 +02:00
|
|
|
)
|
2021-10-22 21:32:25 +02:00
|
|
|
if (moduleError !== false) {
|
|
|
|
compilation.errors[i] = moduleError
|
|
|
|
}
|
2020-08-05 21:11:35 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2020-05-13 17:43:41 +02:00
|
|
|
}
|
2020-08-05 21:11:35 +02:00
|
|
|
)
|
2020-05-13 17:43:41 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|