rsnext/packages/next/build/webpack/plugins/wellknown-errors-plugin/parseBabel.ts
Guy Bedford 8221c180a5
ncc 0.25.0 upgrade and fixes (#18873)
This upgrades to ncc@0.25.0 and fixes the previous bugs including:

* ncc not referenced correctly in build
* Babel type errors
* node-fetch, etag, chalk and raw-body dependencies not building with ncc - these have been "un-ncc'd" for now. As they are relatively small dependencies, this doesn't seem too much of an issue and we can follow up in the tracking ncc issue at https://github.com/vercel/ncc/issues/612.
* `yarn dev` issues

Took a lot of bisecting, but the overall diff isn't too bad here in the end.
2020-11-06 02:33:14 +00:00

40 lines
1.1 KiB
TypeScript

import Chalk from 'chalk'
import { SimpleWebpackError } from './simpleWebpackError'
const chalk = new Chalk.constructor({ enabled: true })
export function getBabelError(
fileName: string,
err: Error & {
code?: 'BABEL_PARSE_ERROR'
loc?: { line: number; column: number }
}
): SimpleWebpackError | false {
if (err.code !== 'BABEL_PARSE_ERROR') {
return false
}
// https://github.com/babel/babel/blob/34693d6024da3f026534dd8d569f97ac0109602e/packages/babel-core/src/parser/index.js
if (err.loc) {
const lineNumber = Math.max(1, err.loc.line)
const column = Math.max(1, err.loc.column)
let message = err.message
// Remove file information, which instead is provided by webpack.
.replace(/^.+?: /, '')
// Remove column information from message
.replace(
new RegExp(`[^\\S\\r\\n]*\\(${lineNumber}:${column}\\)[^\\S\\r\\n]*`),
''
)
return new SimpleWebpackError(
`${chalk.cyan(fileName)}:${chalk.yellow(
lineNumber.toString()
)}:${chalk.yellow(column.toString())}`,
chalk.red.bold('Syntax error').concat(`: ${message}`)
)
}
return false
}