rsnext/packages/next/build/webpack/loaders/error-loader.ts
2020-05-18 15:24:37 -04:00

26 lines
686 B
TypeScript

import chalk from 'next/dist/compiled/chalk'
import loaderUtils from 'loader-utils'
import path from 'path'
import { loader } from 'webpack'
const ErrorLoader: loader.Loader = function () {
const options = loaderUtils.getOptions(this) || {}
const { reason = 'An unknown error has occurred' } = options
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)}` : '')
)
this.emitError(err)
}
export default ErrorLoader