webpack-validator
Validate your webpack config with joi
Last updated 9 years ago by jonathanewerner .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install webpack-validator 
SYNC missed versions from official npm registry.

webpack-validator

Validate your webpack configs with joi

Note: webpack v2 has built-in validation for configuration (you don't have to do anything, it just works). Due to this, webpack-validator is unlikely to make significant changes. While pull requests will be reviewed and can be merged (for webpack v1 support), project maintainers are unlikely to put a lot of much effort into the maintenance of the project.

travis build codecov.io dependencies devDependencies version downloads MIT License PRs Welcome Commitizen friendly semantic-release

Writing webpack configs is brittle and error-prone. This package provides a joi object schema for webpack configs. This gets you a) static type safety, b) property spell checking and c) semantic validations such as "loader and loaders can not be used simultaneously" or "query can only be used with loader, not with loaders".

You're very welcome to give feedback & PR's.

Example

Take this simple webpack config. It has a tiny, hard to spot error. Can you find it?

var config = {
  module: {
    loaders: [
      { test: /\.js$/, loaders: 'babel-loader', exclude: /node_modules/ }
    ]
  },
  output: {
    library: 'Redux',
    libraryTarget: 'umd'
  },
  plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(env)
    })
  ]
};

webpack-validator makes it easy:

validation-example

Usage

There are two ways to use webpack-validator: a) "programmatically" by wrapping your webpack config object with a validation function or b) by using a command line interface.

For the first approach, add this in your webpack.config.js:

const validate = require('webpack-validator');

module.exports = validate({ /* ... your webpack config */ });

Now run webpack. Either everything is green and the build continues or joi will let you know what's wrong and the build won't continue.

If your webpack config is an array of configs instead of a single object, the above doesn't quite work. Add this in your webpack.config.js instead:

const validate = require('webpack-validator').validateRoot;

module.exports = validate({ /* ... your webpack config */ });

CLI

For CLI usage you probably want to install the tool globally (npm install -g webpack-validator) first. Then just run webpack-validator <your-config>.

Customizing

Schema

If you need to extend the schema, for example for custom top level properties or properties added by third party plugins like eslint-loader (which adds a toplevel eslint property), do it like this:

const validate = require('webpack-validator')
const Joi = require('webpack-validator').Joi

// This joi schema will be `Joi.concat`-ed with the internal schema
const yourSchemaExtension = Joi.object({
  // this would just allow the property and doesn't perform any additional validation
  eslint: Joi.any()
})

const config = { /* ... your webpack config */ }

module.exports = validate(config, { schemaExtension: yourSchemaExtension })

Rules

Some validations do more than just validating your data shape, they check for best practices and do "more" which you might want to opt out of / in to. This is an overview of the available rules (we just started with this, this list will grow :)):

  • no-root-files-node-modules-nameclash (default: true): this checks that files/folders that are found in directories specified via webpacks resolve.root option do not nameclash with node_modules packages. This prevents nasty path resolving bugs (for a motivating example, have a look at this redux issue).
  • loader-enforce-include-or-exclude (default: false): enforce that loader objects use include or/and exclude, throw when neither is supplied. Without supplying one of these conditions it is too easy to process too many files, for example your node_modules folder.
  • loader-prefer-include (default: false): enforce that loader objects use include and not exclude. exclude makes it easy to match too many files, which might inadvertently slow your build down.

You opt in/out of rules by using the rules option:

module.exports = validate(config, {
  rules: {
    'no-root-files-node-modules-nameclash': false,
  },
})

Note: This is not yet implemented via cli options, the default rules will apply in that case.

Quiet Mode

If you want to mute console output apart from errors, set --quiet (-q) or validate(config, { quiet: true }). This is particularly useful if you are using webpack --json as you'll want to avoid writing additional text to the JSON output.

Validate all package.json scripts

It is possible to use the CLI to validate all your package.json scripts related configurations at once by using --all-scripts (-a). The system will try to guess the convention you are using and then executes validation against each script target based on that.

Advanced Usage

If you need to access the validation results directly and want to control the side-effects (i.e. console.log output, process.exit(1) on fail behaviour) yourself, you can call the validation function like so: validate(config, { returnValidation: true }). This will make 1) the function return the validation results instead of your configuration and 2) not perform any side effects.

Support

Because this module uses the amazing Joi validation library, this module only supports Node >=4.0.0.

License

MIT

Current Tags

  • 1.0.0-beta.10                                ...           beta (9 years ago)
  • 3.0.1                                ...           latest (7 years ago)

49 Versions

  • 3.0.1                                ...           7 years ago
  • 3.0.0                                ...           8 years ago
  • 2.3.0                                ...           8 years ago
  • 2.2.12                                ...           8 years ago
  • 2.2.11                                ...           8 years ago
  • 2.2.10                                ...           8 years ago
  • 2.2.9                                ...           8 years ago
  • 2.2.8                                ...           8 years ago
  • 2.2.7                                ...           8 years ago
  • 2.2.6                                ...           8 years ago
  • 2.2.5                                ...           8 years ago
  • 2.2.4                                ...           8 years ago
  • 2.2.3                                ...           8 years ago
  • 2.2.2                                ...           8 years ago
  • 2.2.1                                ...           8 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.3                                ...           8 years ago
  • 2.1.2                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.2                                ...           8 years ago
  • 2.0.1                                ...           8 years ago
  • 2.0.0                                ...           8 years ago
  • 1.9.0                                ...           8 years ago
  • 1.8.2                                ...           8 years ago
  • 1.8.1                                ...           8 years ago
  • 1.8.0                                ...           9 years ago
  • 1.7.0                                ...           9 years ago
  • 1.6.0                                ...           9 years ago
  • 1.5.0                                ...           9 years ago
  • 1.4.0                                ...           9 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.1                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.2                                ...           9 years ago
  • 1.0.1                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
  • 1.0.0-beta.10                                ...           9 years ago
  • 1.0.0-beta.9                                ...           9 years ago
  • 1.0.0-beta.8                                ...           9 years ago
  • 1.0.0-beta.7                                ...           9 years ago
  • 1.0.0-beta.6                                ...           9 years ago
  • 1.0.0-beta.5                                ...           9 years ago
  • 1.0.0-beta.4                                ...           9 years ago
  • 1.0.0-beta.3                                ...           9 years ago
  • 1.0.0-beta.2                                ...           9 years ago
  • 1.0.0-beta.1                                ...           9 years ago
  • 1.0.0-beta.0                                ...           9 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE