cleandir-webpack-plugin

cleandir-webpack-plugin

Usage no npm install needed!

<script type="module">
  import cleandirWebpackPlugin from 'https://cdn.skypack.dev/cleandir-webpack-plugin';
</script>

README

cleandir-webpack-plugin

This plugin allows you to delete files/directories before or after bundle compilation.
Comparing to most popular cleanup plugin, cleandir-webpack-plugin provides you ability to run plugin after files was written to output directory and ability to exclude files with glob patterns.

Installation

npm i --save cleandir-webpack-plugin

Example

// webpack.conf.js
const CleanDirWebpackPlugin = require("cleandir-webpack-plugin");

module.exports = {
    plugins: [
        new CleanDirWebpackPlugin("dist/"), // removing whole dist dir
        new CleanDirWebpackPlugin([
                                      "dist/css/*.js",          // removing js generated by sass plugin
                                      "dist/some/directory/**", // and something else generated by build
                                  ],
                                  {
                                      stage:   "after",
                                      exclude: "dist/some/directory/**.log",    // excepting the *.log files
                                      silent:  true,
                                  },
        ),
    ],
};

Usage

// webpack.conf.js
const CleanDirWebpackPlugin = require("cleandir-webpack-plugin");

module.exports = {
    plugins:[
            new CleanDirWebpackPlugin(paths [, options])
    ]
}

paths (required)

Paths can be a valuable string or an array of strings.
Each string should be a valid glob pattern (node-glob is used under the hood).

options (optional)

const options = {
    // The momen when cleanup will be triggered, before or after emitting assets to output dir.
    // This way you will be able to remove redundant files generated by webpack
    // 
    // Variants: "before", "after"
    // Default: "before"
    stage: "before",
    
    // Same as paths parameter, but files and direcroties matching given globs wont be deleted
    //
    // Default: []
    exclude: [],
    
    // If true writes result of each glob processing
    //
    // Default: true
    verbose: true,
    
    // If true, will not generate any console output (excepting error)
    //
    // Default: false
    silent: false,
    
    // If true, will only emulate deletion (will not remove files)
    //
    // Default: false
    dryRun: false,
    
    // Allow plugin to clean paths outside of given root path
    //
    // Default: false
    allowExternal: false,
    
    // Path that will be treated as relative root for globs
    //
    // Default: path.dirname(module.parent.filename)
    root: path.dirname(module.parent.filename),
}

Tests

npm i
npm run test

Coverage

npm i
npm run test:coverage