405e42e41e
Adds handling for module not found errors exposed by webpack. This ensures you see the actual stack line and code instead of a short message where you don't know where to go. ### Previous <img width="794" alt="Screen Shot 2020-08-05 at 18 02 06" src="https://user-images.githubusercontent.com/6324199/89435935-d5542c00-d745-11ea-9ca7-c67f553364f9.png"> ### New <img width="769" alt="Screen Shot 2020-08-05 at 14 20 23" src="https://user-images.githubusercontent.com/6324199/89412212-f6595480-d726-11ea-81a3-398ab7036338.png"> Fixes #14711
88 lines
2.3 KiB
TypeScript
88 lines
2.3 KiB
TypeScript
import { readFileSync } from 'fs'
|
|
import * as path from 'path'
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
import { compilation as CompilationType } from 'webpack'
|
|
import { getBabelError } from './parseBabel'
|
|
import { getCssError } from './parseCss'
|
|
import { getScssError } from './parseScss'
|
|
import { getNotFoundError } from './parseNotFoundError'
|
|
import { SimpleWebpackError } from './simpleWebpackError'
|
|
|
|
function getFileData(
|
|
compilation: CompilationType.Compilation,
|
|
m: any
|
|
): [string, string | null] {
|
|
let resolved: string
|
|
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)
|
|
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
|
|
}
|
|
}
|
|
|
|
if (resolved) {
|
|
let content: string | null = null
|
|
try {
|
|
content = readFileSync(
|
|
ctx ? path.resolve(ctx, resolved) : resolved,
|
|
'utf8'
|
|
)
|
|
} catch {}
|
|
return [resolved, content]
|
|
}
|
|
|
|
return ['<unknown>', null]
|
|
}
|
|
|
|
export async function getModuleBuildError(
|
|
compilation: CompilationType.Compilation,
|
|
input: any
|
|
): Promise<SimpleWebpackError | false> {
|
|
if (
|
|
!(
|
|
typeof input === 'object' &&
|
|
(input?.name === 'ModuleBuildError' ||
|
|
input?.name === 'ModuleNotFoundError') &&
|
|
Boolean(input.module) &&
|
|
input.error instanceof Error
|
|
)
|
|
) {
|
|
return false
|
|
}
|
|
|
|
const err: Error = input.error
|
|
const [sourceFilename, sourceContent] = getFileData(compilation, input.module)
|
|
|
|
const notFoundError = await getNotFoundError(
|
|
compilation,
|
|
input,
|
|
sourceFilename
|
|
)
|
|
if (notFoundError !== false) {
|
|
return notFoundError
|
|
}
|
|
|
|
const babel = getBabelError(sourceFilename, err)
|
|
if (babel !== false) {
|
|
return babel
|
|
}
|
|
|
|
const css = getCssError(sourceFilename, err)
|
|
if (css !== false) {
|
|
return css
|
|
}
|
|
|
|
const scss = getScssError(sourceFilename, sourceContent, err)
|
|
if (scss !== false) {
|
|
return scss
|
|
}
|
|
|
|
return false
|
|
}
|