2020-05-22 07:25:02 +02:00
|
|
|
import { readFileSync } from 'fs'
|
2020-05-13 17:43:41 +02:00
|
|
|
import * as path from 'path'
|
|
|
|
import { getBabelError } from './parseBabel'
|
2020-05-13 23:18:45 +02:00
|
|
|
import { getCssError } from './parseCss'
|
2020-05-22 07:25:02 +02:00
|
|
|
import { getScssError } from './parseScss'
|
2020-08-05 21:11:35 +02:00
|
|
|
import { getNotFoundError } from './parseNotFoundError'
|
2020-05-13 17:43:41 +02:00
|
|
|
import { SimpleWebpackError } from './simpleWebpackError'
|
2021-09-16 18:06:57 +02:00
|
|
|
import isError from '../../../../lib/is-error'
|
2021-10-22 21:32:25 +02:00
|
|
|
import type webpack5 from 'webpack5'
|
2020-05-13 17:43:41 +02:00
|
|
|
|
2020-05-22 07:25:02 +02:00
|
|
|
function getFileData(
|
2021-10-22 21:32:25 +02:00
|
|
|
compilation: webpack5.Compilation,
|
2020-05-22 07:25:02 +02:00
|
|
|
m: any
|
|
|
|
): [string, string | null] {
|
|
|
|
let resolved: string
|
2021-10-22 21:32:25 +02:00
|
|
|
let ctx: string | null = compilation.compiler?.context ?? null
|
2020-05-13 23:18:45 +02:00
|
|
|
if (ctx !== null && typeof m.resource === 'string') {
|
|
|
|
const res = path.relative(ctx, m.resource).replace(/\\/g, path.posix.sep)
|
2020-05-22 07:25:02 +02:00
|
|
|
resolved = res.startsWith('.') ? res : `.${path.posix.sep}${res}`
|
|
|
|
} else {
|
|
|
|
const requestShortener = compilation.requestShortener
|
|
|
|
if (typeof m?.readableIdentifier === 'function') {
|
|
|
|
resolved = m.readableIdentifier(requestShortener)
|
|
|
|
} else {
|
|
|
|
resolved = m.request ?? m.userRequest
|
|
|
|
}
|
2020-05-13 23:18:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:25:02 +02:00
|
|
|
if (resolved) {
|
|
|
|
let content: string | null = null
|
|
|
|
try {
|
|
|
|
content = readFileSync(
|
|
|
|
ctx ? path.resolve(ctx, resolved) : resolved,
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
} catch {}
|
|
|
|
return [resolved, content]
|
2020-05-13 17:43:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:25:02 +02:00
|
|
|
return ['<unknown>', null]
|
2020-05-13 17:43:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 21:11:35 +02:00
|
|
|
export async function getModuleBuildError(
|
2021-10-22 21:32:25 +02:00
|
|
|
compilation: webpack5.Compilation,
|
2020-05-13 17:43:41 +02:00
|
|
|
input: any
|
2020-08-05 21:11:35 +02:00
|
|
|
): Promise<SimpleWebpackError | false> {
|
2020-05-13 17:43:41 +02:00
|
|
|
if (
|
|
|
|
!(
|
|
|
|
typeof input === 'object' &&
|
2020-08-05 21:11:35 +02:00
|
|
|
(input?.name === 'ModuleBuildError' ||
|
|
|
|
input?.name === 'ModuleNotFoundError') &&
|
2020-05-13 17:43:41 +02:00
|
|
|
Boolean(input.module) &&
|
2021-09-16 18:06:57 +02:00
|
|
|
isError(input.error)
|
2020-05-13 17:43:41 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const err: Error = input.error
|
2020-05-22 07:25:02 +02:00
|
|
|
const [sourceFilename, sourceContent] = getFileData(compilation, input.module)
|
2020-05-13 23:18:45 +02:00
|
|
|
|
2020-08-05 21:11:35 +02:00
|
|
|
const notFoundError = await getNotFoundError(
|
|
|
|
compilation,
|
|
|
|
input,
|
|
|
|
sourceFilename
|
|
|
|
)
|
|
|
|
if (notFoundError !== false) {
|
|
|
|
return notFoundError
|
|
|
|
}
|
|
|
|
|
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-22 07:25:02 +02:00
|
|
|
const scss = getScssError(sourceFilename, sourceContent, err)
|
|
|
|
if (scss !== false) {
|
|
|
|
return scss
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:43:41 +02:00
|
|
|
return false
|
|
|
|
}
|