UNPKG

5.28 kBMarkdownView Raw
1# Clean plugin for webpack
2
3[![npm][npm-image]][npm-url]
4[![MIT License][mit-license-image]][mit-license-url]
5[![Linux Build Status][circleci-image]][circleci-url]
6[![Windows Build Status][appveyor-image]][appveyor-url]
7[![Coveralls Status][coveralls-image]][coveralls-url]
8
9[npm-url]: https://www.npmjs.com/package/clean-webpack-plugin
10[npm-image]: https://img.shields.io/npm/v/clean-webpack-plugin.svg?label=npm%20version
11[mit-license-url]: LICENSE
12[mit-license-image]: https://camo.githubusercontent.com/d59450139b6d354f15a2252a47b457bb2cc43828/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f7365727665726c6573732e737667
13[circleci-url]: https://circleci.com/gh/johnagan/clean-webpack-plugin/tree/master
14[circleci-image]: https://img.shields.io/circleci/project/github/johnagan/clean-webpack-plugin/master.svg?label=linux%20build
15[appveyor-url]: https://ci.appveyor.com/project/johnagan/clean-webpack-plugin/branch/master
16[appveyor-image]: https://img.shields.io/appveyor/ci/johnagan/clean-webpack-plugin/master.svg?label=windows%20build
17[coveralls-url]: https://codecov.io/gh/johnagan/clean-webpack-plugin/branch/master
18[coveralls-image]: https://img.shields.io/codecov/c/github/johnagan/clean-webpack-plugin/master.svg
19
20A webpack plugin to remove/clean your build folder(s).
21
22> NOTE: Node v8+ and webpack v3+ are supported and tested.
23
24## About
25
26By default, this plugin will remove all files inside webpack's `output.path` directory, as well as all unused webpack assets after every successful rebuild.
27
28> Coming from `v1`? Please read about [additional v2 information](https://github.com/johnagan/clean-webpack-plugin/issues/106).
29
30## Installation
31
32`npm install --save-dev clean-webpack-plugin`
33
34## Usage
35
36```js
37const { CleanWebpackPlugin } = require('clean-webpack-plugin');
38
39const webpackConfig = {
40 plugins: [
41 /**
42 * All files inside webpack's output.path directory will be removed once, but the
43 * directory itself will not be. If using webpack 4+'s default configuration,
44 * everything under <PROJECT_DIR>/dist/ will be removed.
45 * Use cleanOnceBeforeBuildPatterns to override this behavior.
46 *
47 * During rebuilds, all webpack assets that are not used anymore
48 * will be removed automatically.
49 *
50 * See `Options and Defaults` for information
51 */
52 new CleanWebpackPlugin(),
53 ],
54};
55
56module.exports = webpackConfig;
57```
58
59## Options and Defaults (Optional)
60
61```js
62new CleanWebpackPlugin({
63 // Simulate the removal of files
64 //
65 // default: false
66 dry: true,
67
68 // Write Logs to Console
69 // (Always enabled when dry is true)
70 //
71 // default: false
72 verbose: true,
73
74 // Automatically remove all unused webpack assets on rebuild
75 //
76 // default: true
77 cleanStaleWebpackAssets: false,
78
79 // Do not allow removal of current webpack assets
80 //
81 // default: true
82 protectWebpackAssets: false,
83
84 // **WARNING**
85 //
86 // Notes for the below options:
87 //
88 // They are unsafe...so test initially with dry: true.
89 //
90 // Relative to webpack's output.path directory.
91 // If outside of webpack's output.path directory,
92 // use full path. path.join(process.cwd(), 'build/**/*')
93 //
94 // These options extend del's pattern matching API.
95 // See https://github.com/sindresorhus/del#patterns
96 // for pattern matching documentation
97
98 // Removes files once prior to Webpack compilation
99 // Not included in rebuilds (watch mode)
100 //
101 // Use !negative patterns to exclude files
102 //
103 // default: ['**/*']
104 cleanOnceBeforeBuildPatterns: ['**/*', '!static-files*'],
105 cleanOnceBeforeBuildPatterns: [], // disables cleanOnceBeforeBuildPatterns
106
107 // Removes files after every build (including watch mode) that match this pattern.
108 // Used for files that are not created directly by Webpack.
109 //
110 // Use !negative patterns to exclude files
111 //
112 // default: []
113 cleanAfterEveryBuildPatterns: ['static*.*', '!static1.js'],
114
115 // Allow clean patterns outside of process.cwd()
116 //
117 // requires dry option to be explicitly set
118 //
119 // default: false
120 dangerouslyAllowCleanPatternsOutsideProject: true,
121});
122```
123
124## Example Webpack Config
125
126This is a modified version of [WebPack's Plugin documentation](https://webpack.js.org/concepts/plugins/) that includes the Clean Plugin.
127
128```js
129const { CleanWebpackPlugin } = require('clean-webpack-plugin'); // installed via npm
130const HtmlWebpackPlugin = require('html-webpack-plugin'); // installed via npm
131const webpack = require('webpack'); // to access built-in plugins
132const path = require('path');
133
134module.exports = {
135 entry: './path/to/my/entry/file.js',
136 output: {
137 /**
138 * With zero configuration,
139 * clean-webpack-plugin will remove files inside the directory below
140 */
141 path: path.resolve(process.cwd(), 'dist'),
142 },
143 module: {
144 rules: [
145 {
146 test: /\.(js|jsx)$/,
147 loader: 'babel-loader',
148 },
149 ],
150 },
151 plugins: [
152 new webpack.ProgressPlugin(),
153 new CleanWebpackPlugin(),
154 new HtmlWebpackPlugin({ template: './src/index.html' }),
155 ],
156};
157```