2020-05-13 17:43:41 +02:00
|
|
|
import * as path from 'path'
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
import { compilation } from 'webpack'
|
|
|
|
import { getBabelError } from './parseBabel'
|
2020-05-13 23:18:45 +02:00
|
|
|
import { getCssError } from './parseCss'
|
2020-05-13 17:43:41 +02:00
|
|
|
import { SimpleWebpackError } from './simpleWebpackError'
|
|
|
|
|
|
|
|
function getFilename(compilation: compilation.Compilation, m: any): string {
|
2020-05-13 23:18:45 +02:00
|
|
|
let ctx: string | null =
|
|
|
|
compilation.compiler?.context ?? compilation.context ?? null
|
|
|
|
if (ctx !== null && typeof m.resource === 'string') {
|
|
|
|
const res = path.relative(ctx, m.resource).replace(/\\/g, path.posix.sep)
|
|
|
|
return res.startsWith('.') ? res : `.${path.posix.sep}${res}`
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:43:41 +02:00
|
|
|
const requestShortener = compilation.requestShortener
|
|
|
|
if (typeof m?.readableIdentifier === 'function') {
|
|
|
|
return m.readableIdentifier(requestShortener)
|
|
|
|
}
|
|
|
|
|
2020-05-13 23:18:45 +02:00
|
|
|
return m.request ?? m.userRequest ?? '<unknown>'
|
2020-05-13 17:43:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getModuleBuildError(
|
|
|
|
compilation: compilation.Compilation,
|
|
|
|
input: any
|
|
|
|
): SimpleWebpackError | false {
|
|
|
|
if (
|
|
|
|
!(
|
|
|
|
typeof input === 'object' &&
|
|
|
|
input?.name === 'ModuleBuildError' &&
|
|
|
|
Boolean(input.module) &&
|
|
|
|
input.error instanceof Error
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const err: Error = input.error
|
|
|
|
const sourceFilename = getFilename(compilation, input.module)
|
2020-05-13 23:18:45 +02:00
|
|
|
|
2020-05-13 17:43:41 +02:00
|
|
|
const babel = getBabelError(sourceFilename, err)
|
|
|
|
if (babel !== false) {
|
|
|
|
return babel
|
|
|
|
}
|
|
|
|
|
2020-05-13 23:18:45 +02:00
|
|
|
const css = getCssError(sourceFilename, err)
|
|
|
|
if (css !== false) {
|
|
|
|
return css
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:43:41 +02:00
|
|
|
return false
|
|
|
|
}
|