1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const validateOptions = require("schema-utils");
|
8 | const schema = require("../schemas/plugins/IgnorePlugin.json");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class IgnorePlugin {
|
14 | |
15 |
|
16 |
|
17 | constructor(options) {
|
18 |
|
19 | if (arguments.length > 1 || options instanceof RegExp) {
|
20 | options = {
|
21 | resourceRegExp: arguments[0],
|
22 | contextRegExp: arguments[1]
|
23 | };
|
24 | }
|
25 |
|
26 | validateOptions(schema, options, "IgnorePlugin");
|
27 | this.options = options;
|
28 |
|
29 |
|
30 | this.checkIgnore = this.checkIgnore.bind(this);
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | checkResource(resource) {
|
39 | if ("checkResource" in this.options && this.options.checkResource) {
|
40 | return this.options.checkResource(resource);
|
41 | }
|
42 | if ("resourceRegExp" in this.options && this.options.resourceRegExp) {
|
43 | return this.options.resourceRegExp.test(resource);
|
44 | }
|
45 | return false;
|
46 | }
|
47 |
|
48 | |
49 |
|
50 |
|
51 |
|
52 |
|
53 | checkContext(context) {
|
54 | if ("checkContext" in this.options && this.options.checkContext) {
|
55 | return this.options.checkContext(context);
|
56 | }
|
57 | if ("contextRegExp" in this.options && this.options.contextRegExp) {
|
58 | return this.options.contextRegExp.test(context);
|
59 | }
|
60 | return true;
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | checkResult(result) {
|
71 | if (!result) {
|
72 | return true;
|
73 | }
|
74 | return (
|
75 | this.checkResource(result.request) && this.checkContext(result.context)
|
76 | );
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 | checkIgnore(result) {
|
84 |
|
85 | if (this.checkResult(result)) {
|
86 | return null;
|
87 | }
|
88 | return result;
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 |
|
95 | apply(compiler) {
|
96 | compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
|
97 | nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
|
98 | });
|
99 | compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
|
100 | cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
|
101 | });
|
102 | }
|
103 | }
|
104 |
|
105 | module.exports = IgnorePlugin;
|