2020-11-06 03:33:14 +01:00
|
|
|
import chalk from 'chalk'
|
2021-01-15 02:51:45 +01:00
|
|
|
import loaderUtils from 'next/dist/compiled/loader-utils'
|
2019-12-27 20:56:33 +01:00
|
|
|
import path from 'path'
|
2021-01-14 02:59:08 +01:00
|
|
|
import { webpack } from 'next/dist/compiled/webpack/webpack'
|
2019-10-04 17:55:18 +02:00
|
|
|
|
2021-01-14 02:59:08 +01:00
|
|
|
const ErrorLoader: webpack.loader.Loader = function () {
|
2019-10-04 17:55:18 +02:00
|
|
|
const options = loaderUtils.getOptions(this) || {}
|
|
|
|
|
|
|
|
const { reason = 'An unknown error has occurred' } = options
|
|
|
|
|
2019-12-27 20:56:33 +01:00
|
|
|
const resource = this._module?.issuer?.resource ?? null
|
|
|
|
const context = this.rootContext ?? this._compiler?.context
|
|
|
|
|
|
|
|
const issuer = resource
|
|
|
|
? context
|
|
|
|
? path.relative(context, resource)
|
|
|
|
: resource
|
|
|
|
: null
|
|
|
|
|
|
|
|
const err = new Error(
|
|
|
|
reason + (issuer ? `\nLocation: ${chalk.cyan(issuer)}` : '')
|
|
|
|
)
|
2019-10-04 17:55:18 +02:00
|
|
|
this.emitError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ErrorLoader
|