rsnext/packages/next/build/webpack/plugins/wellknown-errors-plugin/parseScss.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

45 lines
1.2 KiB
TypeScript

import { codeFrameColumns } from 'next/dist/compiled/babel/code-frame'
import Chalk from 'chalk'
import { SimpleWebpackError } from './simpleWebpackError'
const chalk = new Chalk.constructor({ enabled: true })
const regexScssError = /SassError: (.+)\n\s+on line (\d+) [\s\S]*?>> (.+)\n\s*(-+)\^$/m
export function getScssError(
fileName: string,
fileContent: string | null,
err: Error
): SimpleWebpackError | false {
if (err.name !== 'SassError') {
return false
}
const res = regexScssError.exec(err.message)
if (res) {
const [, reason, _lineNumer, backupFrame, columnString] = res
const lineNumber = Math.max(1, parseInt(_lineNumer, 10))
const column = columnString?.length ?? 1
let frame: string | undefined
if (fileContent) {
try {
frame = codeFrameColumns(
fileContent,
{ start: { line: lineNumber, column } },
{ forceColor: true }
) as string
} catch {}
}
return new SimpleWebpackError(
`${chalk.cyan(fileName)}:${chalk.yellow(
lineNumber.toString()
)}:${chalk.yellow(column.toString())}`,
chalk.red
.bold('Syntax error')
.concat(`: ${reason}\n\n${frame ?? backupFrame}`)
)
}
return false
}