1 | ## Webpack Deadcode Plugin
|
2 |
|
3 | Webpack plugin to detect unused files and unused exports in used files
|
4 |
|
5 | [![npm][npm]][npm-url]
|
6 | [![node][node]][node-url]
|
7 | [![deps][deps]][deps-url]
|
8 | [![licenses][licenses]][licenses-url]
|
9 |
|
10 | ### Installation
|
11 |
|
12 | Via npm:
|
13 |
|
14 | ```bash
|
15 | $ npm install webpack-deadcode-plugin --save-dev
|
16 | ```
|
17 |
|
18 | Via yarn:
|
19 |
|
20 | ```bash
|
21 | $ yarn add -D webpack-deadcode-plugin
|
22 | ```
|
23 |
|
24 | ![output](https://i.imgur.com/3Ll49Pj.png)
|
25 |
|
26 | ### Usage
|
27 |
|
28 | The plugin will report unused files and unused exports into your terminal but those are not part of your webpack build process, therefore, it will not fail your build (warning you). Simple add into your webpack config as follows:
|
29 |
|
30 | ✍️ If you use `babel-loader`, you have to set `modules: false` to make it works
|
31 |
|
32 | ```bash
|
33 | # in .babelrc
|
34 | {
|
35 | "presets": [
|
36 | ["env", { modules: false }]
|
37 | ]
|
38 | }
|
39 |
|
40 | # or in webpack.config.js -> module/rules
|
41 | {
|
42 | loader: 'babel-loader',
|
43 | options: {
|
44 | presets: [
|
45 | ['env', { modules: false }]
|
46 | ]
|
47 | }
|
48 | }
|
49 | ```
|
50 |
|
51 | **Webpack 3**
|
52 |
|
53 | ```js
|
54 | const DeadCodePlugin = require('webpack-deadcode-plugin');
|
55 |
|
56 | const webpackConfig = {
|
57 | ...
|
58 | plugins: [
|
59 | new DeadCodePlugin({
|
60 | patterns: [
|
61 | 'src/**/*.(js|jsx|css)',
|
62 | ],
|
63 | exclude: [
|
64 | '**/*.(stories|spec).(js|jsx)',
|
65 | ],
|
66 | })
|
67 | ]
|
68 | }
|
69 | ```
|
70 |
|
71 | **Webpack 4**
|
72 |
|
73 | ```js
|
74 | const DeadCodePlugin = require('webpack-deadcode-plugin');
|
75 |
|
76 | const webpackConfig = {
|
77 | ...
|
78 | optimization: {
|
79 | usedExports: true,
|
80 | },
|
81 | plugins: [
|
82 | new DeadCodePlugin({
|
83 | patterns: [
|
84 | 'src/**/*.(js|jsx|css)',
|
85 | ],
|
86 | exclude: [
|
87 | '**/*.(stories|spec).(js|jsx)',
|
88 | ],
|
89 | })
|
90 | ]
|
91 | }
|
92 | ```
|
93 |
|
94 | #### Using non-existent css class names
|
95 |
|
96 | To detect using non-existent class names in your codebase, you have to use [`es6-css-loader`](https://github.com/MQuy/es6-css-loader) instead of `style-loader/mini-css-extract-plugin`. They are quite similiar in term of api except [`es6-css-loader`](https://github.com/MQuy/es6-css-loader) supports to detect non-existent css class names.
|
97 |
|
98 | ![non-existent css class names](https://i.imgur.com/amHZF5Q.png)
|
99 |
|
100 | You can check `samples` folder, how to to config `webpack-deadcode-plugin` and `es6-css-loader`.
|
101 |
|
102 | #### Typescript
|
103 |
|
104 | Using with typescript loader ([ts-loader](https://github.com/TypeStrong/ts-loader), [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader)), if you enable `transpileOnly/happyPackMode`, output might be not correct due to [this issue](https://github.com/TypeStrong/ts-loader/issues/783). In case of incorrect output, the workaround solution is disabling `transpileOnly`, it will slow down webpack compiling time.
|
105 |
|
106 | ✍ Under some circumstances and production mode, if your output displays incorrect unused files, we encourage switching to [`awesome-typescript-loader`](https://github.com/s-panferov/awesome-typescript-loader).
|
107 |
|
108 | ### Configuration
|
109 |
|
110 | ```js
|
111 | new DeadCodePlugin(options);
|
112 | ```
|
113 |
|
114 | #### options.patterns (default: `["**/*.*"]`)
|
115 |
|
116 | The array of patterns to look for unused files and unused export in used files. Directly pass to [`fast-glob`](https://github.com/mrmlnc/fast-glob)
|
117 |
|
118 | #### options.exclude (default: `[]`)
|
119 |
|
120 | The array of patterns to not look at.
|
121 |
|
122 | #### options.context
|
123 |
|
124 | Current working directory for patterns above. If you don't set it explicitly, your webpack context will be used.
|
125 |
|
126 | #### options.failOnHint (default: `false`)
|
127 |
|
128 | Deadcode does not interrupt the compilation by default. If you want to cancel the compilation, set it true, it throws a fatal error and stops the compilation.
|
129 |
|
130 | #### options.detectUnusedFiles (default: `true`)
|
131 |
|
132 | Whether to run unused files detection or not.
|
133 |
|
134 | #### options.detectUnusedExport (default: `true`)
|
135 |
|
136 | Whether to run unused export detection or not.
|
137 |
|
138 | #### options.log (default: `"all"`)
|
139 |
|
140 | `"all"`: show all messages.
|
141 | `"unused"`: only show messages when there are either unused files or unused export.
|
142 |
|
143 | [npm]: https://img.shields.io/npm/v/webpack-deadcode-plugin.svg
|
144 | [npm-url]: https://npmjs.com/package/webpack-deadcode-plugin
|
145 | [node]: https://img.shields.io/node/v/webpack-deadcode-plugin.svg
|
146 | [node-url]: https://nodejs.org
|
147 | [deps]: https://img.shields.io/david/MQuy/webpack-deadcode-plugin.svg
|
148 | [deps-url]: https://david-dm.org/MQuy/webpack-deadcode-plugin
|
149 | [licenses]: https://img.shields.io/github/license/MQuy/webpack-deadcode-plugin.svg
|
150 | [licenses-url]: https://github.com/MQuy/webpack-deadcode-plugin/blob/master/LICENSE
|