2019-12-27 20:56:33 +01:00
|
|
|
import chalk from 'chalk'
|
2019-10-04 17:55:18 +02:00
|
|
|
import loaderUtils from 'loader-utils'
|
2019-12-27 20:56:33 +01:00
|
|
|
import path from 'path'
|
2019-10-04 17:55:18 +02:00
|
|
|
import { loader } from 'webpack'
|
|
|
|
|
|
|
|
const ErrorLoader: loader.Loader = function() {
|
|
|
|
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
|