0.2.0 • Published 5 years ago

webpack-bundle-content-validator v0.2.0

Weekly downloads
8
License
MIT
Repository
-
Last release
5 years ago

Build Status Coverage Status Known Vulnerabilities NPM version

Webpack Bundle Content Validator

Webpack plugin and CLI tool for Webpack bundle content validation.

  • ✔️ check if your Webpack bundle contains mandatory dependencies and doesn't contain disallowed ones
  • ⚠️ print warning in console or fail whole build process when validation is not successful
  • 🤖 use as Webpack plugin or CLI tool

This plugin/tool is compatible with Webpack 4. Compatibility with older versions of Webpack was not tested yet, but there might be some issues with that due to the fact that structure of Webpack Stats object was changed in Webpack 4.

Note: This is early version of this project. Feedback and suggestions are appreciated!

Background

We're using internal package of vendor libraries in my project. We wanted to ensure that none of dependencies provided by vendor ends up in bundles representing our applications. Therefore, I wrote this tool to validate content of Webpack bundle against list of mandatory and disallowed dependencies, and open sourced it. We're using it as Webpack plugin during development and as CLI tool in CI/CD process.

Usage

You can download this project as a package from NPM:

npm install --save-dev webpack-bundle-content-validator

You can also download this project as a source code from GitHub and build it yourself (compiled project will be placed in lib directory):

npm run build

As Webpack plugin

In order to use it as Webpack plugin, import it in your Webpack configuration, and add it to plugins section of Webpack's configuration file.

const WebpackBundleContentValidatorPlugin = require('webpack-bundle-content-validator/lib/plugin');

module.exports = {
  // rest of your configuration
  plugins: [
    // rest of your plugins
    new WebpackBundleContentValidatorPlugin(/* options */),
  ]
}

Options

NameDescriptionDefault value
mandatoryDependenciesArray of names of dependencies without which validation will be unsuccessful.[]
disallowedDependenciesArray of names of dependencies with which validation will be unsuccessful.[]
failOnInvalidIf set to false, unsuccessful validation will print warning message in console, but bundle will be compiled. If set to true, unsuccessful validation will print error in console and exit process; bundle will not be compiled.false

Example

const WebpackBundleContentValidatorPlugin = require('webpack-bundle-content-validator/lib/plugin');

module.exports = {
  // rest of your configuration
  plugins: [
    // rest of your plugins
    new WebpackBundleContentValidatorPlugin({
      mandatoryDependencies: ['preact'],
      disallowedDependencies: ['react', 'react-dom'],
      failOnInvalid: true,
    }),
  ]
}

As CLI tool

In order to use it as CLI tool, you need to produce Webpack Stats object first. This can be done with following command:

webpack --json > stats.json

Make sure that the file contains valid JSON! If yes, you can pass it to Webpack Bundle Content Validator.

node ./node_modules/webpack-bundle-content-validator/lib/cli.js -s ./stats.json

Options

NameDescriptionDefault value
-s, --statsPath to JSON file with Webpack Stats object.stats.json
-m, --mandatoryArray of names of dependencies without which validation will be unsuccessful.[]
-d, --disallowedArray of names of dependencies with which validation will be unsuccessful.[]
-f, --failIf set to false, unsuccessful validation will print warning message in console, but bundle will be compiled. If set to true, unsuccessful validation will print error in console and exit process; bundle will not be compiled.false

Example

./node_modules/webpack-bundle-content-validator/lib/cli.js -s ./my-stats.json -m preact -d react,react-dom -f

TODO

  • install CLI tool globally and make it run with npx