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 |
|
39 |
|
40 | checkIgnore(result) {
|
41 | if (!result) return result;
|
42 |
|
43 | if (
|
44 | "checkResource" in this.options &&
|
45 | this.options.checkResource &&
|
46 | this.options.checkResource(result.request, result.context)
|
47 | ) {
|
48 |
|
49 | if ("checkContext" in this.options && this.options.checkContext) {
|
50 | if (this.options.checkContext(result.context)) {
|
51 | return null;
|
52 | }
|
53 | } else {
|
54 | return null;
|
55 | }
|
56 | }
|
57 |
|
58 | if (
|
59 | "resourceRegExp" in this.options &&
|
60 | this.options.resourceRegExp &&
|
61 | this.options.resourceRegExp.test(result.request)
|
62 | ) {
|
63 | if ("contextRegExp" in this.options && this.options.contextRegExp) {
|
64 |
|
65 |
|
66 | if (this.options.contextRegExp.test(result.context)) {
|
67 | return null;
|
68 | }
|
69 | } else {
|
70 | return null;
|
71 | }
|
72 | }
|
73 |
|
74 | return result;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | apply(compiler) {
|
82 | compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
|
83 | nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
|
84 | });
|
85 | compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
|
86 | cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
|
87 | });
|
88 | }
|
89 | }
|
90 |
|
91 | module.exports = IgnorePlugin;
|