Skip to content

kentcdodds/webpack-validator

 
 

Repository files navigation

webpack-validator

Validate your webpack configs with joi

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

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.

Alternatively just run node webpack.config.js to only validate your config and not run webpack.

Customizing

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 schema = require('webpack-validator').schema

// joi is installed as dependency of this package and will be available in node_modules
// if you use npm 3. Otherwise install it explicitly.
const Joi = require('joi')

const yourSchema = schema.concat(Joi.object({
  // this would just allow the property and doesn't perform any additional validation
  eslint: Joi.any()
}))

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

// Override default config by supplying your config as second parameter.
module.exports = validate(config, yourSchema)

License

MIT

About

Validates your webpack config with Joi

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • JavaScript 100.0%