1 | const path = require("path");
|
2 | const detectDeadcode = require("./detect");
|
3 |
|
4 | class WebpackDeadcodePlugin {
|
5 | constructor(options = {}) {
|
6 | this.options = options;
|
7 | }
|
8 |
|
9 | apply(compiler) {
|
10 | const options = Object.assign(
|
11 | {
|
12 | patterns: ["**/*.*"],
|
13 | exclude: [],
|
14 | context: compiler.context,
|
15 | failOnHint: false,
|
16 | detectUnusedFiles: true,
|
17 | detectUnusedExport: true,
|
18 | log: "all",
|
19 | },
|
20 | this.options
|
21 | );
|
22 |
|
23 | if (compiler.hooks) {
|
24 | compiler.hooks.afterEmit.tapAsync("WebpackDeadcodePlugin", this.handleAfterEmit.bind(this, options));
|
25 | } else {
|
26 | compiler.plugin(`after-emit`, this.handleAfterEmit.bind(this, options));
|
27 | }
|
28 | }
|
29 |
|
30 | handleAfterEmit(options, compilation, callback) {
|
31 | detectDeadcode(compilation, options);
|
32 | callback();
|
33 | }
|
34 | }
|
35 |
|
36 | module.exports = WebpackDeadcodePlugin;
|