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

38 lines
1.1 KiB
TypeScript

import Chalk from 'chalk'
import { SimpleWebpackError } from './simpleWebpackError'
const chalk = new Chalk.constructor({ enabled: true })
const regexCssError = /^(?:CssSyntaxError|SyntaxError)\n\n\((\d+):(\d*)\) (.*)$/s
export function getCssError(
fileName: string,
err: Error
): SimpleWebpackError | false {
if (
!(
(err.name === 'CssSyntaxError' || err.name === 'SyntaxError') &&
(err as any).stack === false &&
!(err instanceof SyntaxError)
)
) {
return false
}
// https://github.com/postcss/postcss-loader/blob/d6931da177ac79707bd758436e476036a55e4f59/src/Error.js
const res = regexCssError.exec(err.message)
if (res) {
const [, _lineNumer, _column, reason] = res
const lineNumber = Math.max(1, parseInt(_lineNumer, 10))
const column = Math.max(1, parseInt(_column, 10))
return new SimpleWebpackError(
`${chalk.cyan(fileName)}:${chalk.yellow(
lineNumber.toString()
)}:${chalk.yellow(column.toString())}`,
chalk.red.bold('Syntax error').concat(`: ${reason}`)
)
}
return false
}