File size: 3,096 Bytes
b91e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { readFileSync } from 'fs'
import * as path from 'path'
import type { webpack } from 'next/dist/compiled/webpack/webpack'

import { getBabelError } from './parseBabel'
import { getCssError } from './parseCss'
import { getScssError } from './parseScss'
import { getNotFoundError, getImageError } from './parseNotFoundError'
import type { SimpleWebpackError } from './simpleWebpackError'
import isError from '../../../../lib/is-error'
import { getNextFontError } from './parseNextFontError'
import { getNextAppLoaderError } from './parseNextAppLoaderError'
import { getNextInvalidImportError } from './parseNextInvalidImportError'

function getFileData(
  compilation: webpack.Compilation,
  m: any
): [string, string | null] {
  let resolved: string
  let ctx: string | null = compilation.compiler?.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(
  compiler: webpack.Compiler,
  compilation: webpack.Compilation,
  input: any
): Promise<SimpleWebpackError | false> {
  if (
    !(
      typeof input === 'object' &&
      (input?.name === 'ModuleBuildError' ||
        input?.name === 'ModuleNotFoundError') &&
      Boolean(input.module) &&
      isError(input.error)
    )
  ) {
    return false
  }

  const err: Error = input.error
  const [sourceFilename, sourceContent] = getFileData(compilation, input.module)

  const notFoundError = await getNotFoundError(
    compilation,
    input,
    sourceFilename,
    input.module
  )
  if (notFoundError !== false) {
    return notFoundError
  }

  const imageError = await getImageError(compilation, input, err)
  if (imageError !== false) {
    return imageError
  }

  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
  }

  const nextFont = getNextFontError(err, input.module)
  if (nextFont !== false) {
    return nextFont
  }

  const nextAppLoader = getNextAppLoaderError(err, input.module, compiler)
  if (nextAppLoader !== false) {
    return nextAppLoader
  }

  const invalidImportError = getNextInvalidImportError(
    err,
    input.module,
    compilation,
    compiler
  )
  if (invalidImportError !== false) {
    return invalidImportError
  }

  return false
}