reportErrors Function

Generate a human-readable error report from an invalid config object.

mortal-webpack is designed not to throw exceptions. However, it is your responsibility to make sure errors do not get through without reporting them to the user.

It is recommended that you pass the output of your calls to compose through this function. If the config is valid, this function will do nothing, otherwise it will report the errors to the user. You can tweak the reporting behavior through a few options explained below.

Example usage:

// file: webpack.config.js
const { compose, reportErrors } = require('mortal-webpack')

module.exports = reportErrors({ exit: true })(compose([ ... ]))

Sample output:

Error                                                        | Location
-----                                                        | --------
Target requires NODE_ENV to be set to "development" while it | /webpack/targets/vendor-dll.js:5
is set to "test". Please re-run by sourcing                  |
NODE_ENV=development into the environment.                   |
-----                                                        |
You must pass a pattern (a string or a RegExp) to "compile". | /webpack/targets/vendor-dll.js:50

Signature

reportErrors(options: Object, output: Config|InvalidConfig) -> Config|InvalidConfig

Parameters (6)

options
Object

Reporting options

options.exit
Boolean

Exit the process with return code 1 on errors.

Defaults to: true

options.context
String

A path to a directory that would be stripped from the reported location. This is used to make the location strings shorter and more meaningful.

If the default is not working for you (e.g. the absolute path to the files is being reported) then you should specify this with the root of your project (which consequently would be equal to builders.context if you've called that, but we can't re-use it here.)

Defaults to: process.cwd()

options.traceFileBlacklist
Array.<FilePattern>

List of files to ignore when locating the file the error originated from.

This may be necessary to configure in order to get more accurate results on where the error originated from to help the user locate it quickly.

By default, this routine uses something that looks like an Error stack trace to find out which function / file the error originated from. It excludes any file that comes from "mortal-webpack" because they're not relevant. However, if you're defining macros and your build scripts call those APIs instead of builders directly, you should list the file paths of those macros here to also exclude them.

Defaults to: []

options.write
Function

Function that is responsible for reporting the errors to the user. The default is to log them using console.error().

Defaults to: console.error

output
Config|InvalidConfig

The output of compose. If it didn't fail, the object is returned as-is.

Return Value

Config|InvalidConfig