UNPKG

2.14 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { validate } = require("schema-utils");
9const schema = require("../schemas/plugins/IgnorePlugin.json");
10
11/** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
12/** @typedef {import("./Compiler")} Compiler */
13/** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
14
15class IgnorePlugin {
16 /**
17 * @param {IgnorePluginOptions} options IgnorePlugin options
18 */
19 constructor(options) {
20 validate(schema, options, {
21 name: "Ignore Plugin",
22 baseDataPath: "options"
23 });
24 this.options = options;
25
26 /** @private @type {Function} */
27 this.checkIgnore = this.checkIgnore.bind(this);
28 }
29
30 /**
31 * Note that if "contextRegExp" is given, both the "resourceRegExp"
32 * and "contextRegExp" have to match.
33 *
34 * @param {ResolveData} resolveData resolve data
35 * @returns {false|undefined} returns false when the request should be ignored, otherwise undefined
36 */
37 checkIgnore(resolveData) {
38 if (
39 "checkResource" in this.options &&
40 this.options.checkResource &&
41 this.options.checkResource(resolveData.request, resolveData.context)
42 ) {
43 return false;
44 }
45
46 if (
47 "resourceRegExp" in this.options &&
48 this.options.resourceRegExp &&
49 this.options.resourceRegExp.test(resolveData.request)
50 ) {
51 if ("contextRegExp" in this.options && this.options.contextRegExp) {
52 // if "contextRegExp" is given,
53 // both the "resourceRegExp" and "contextRegExp" have to match.
54 if (this.options.contextRegExp.test(resolveData.context)) {
55 return false;
56 }
57 } else {
58 return false;
59 }
60 }
61 }
62
63 /**
64 * Apply the plugin
65 * @param {Compiler} compiler the compiler instance
66 * @returns {void}
67 */
68 apply(compiler) {
69 compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
70 nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
71 });
72 compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
73 cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
74 });
75 }
76}
77
78module.exports = IgnorePlugin;