UNPKG

2.25 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 createSchemaValidation = require("./util/create-schema-validation");
9
10/** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
11/** @typedef {import("./Compiler")} Compiler */
12/** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
13
14const validate = createSchemaValidation(
15 require("../schemas/plugins/IgnorePlugin.check.js"),
16 () => require("../schemas/plugins/IgnorePlugin.json"),
17 {
18 name: "Ignore Plugin",
19 baseDataPath: "options"
20 }
21);
22
23class IgnorePlugin {
24 /**
25 * @param {IgnorePluginOptions} options IgnorePlugin options
26 */
27 constructor(options) {
28 validate(options);
29 this.options = options;
30
31 /** @private @type {Function} */
32 this.checkIgnore = this.checkIgnore.bind(this);
33 }
34
35 /**
36 * Note that if "contextRegExp" is given, both the "resourceRegExp"
37 * and "contextRegExp" have to match.
38 *
39 * @param {ResolveData} resolveData resolve data
40 * @returns {false|undefined} returns false when the request should be ignored, otherwise undefined
41 */
42 checkIgnore(resolveData) {
43 if (
44 "checkResource" in this.options &&
45 this.options.checkResource &&
46 this.options.checkResource(resolveData.request, resolveData.context)
47 ) {
48 return false;
49 }
50
51 if (
52 "resourceRegExp" in this.options &&
53 this.options.resourceRegExp &&
54 this.options.resourceRegExp.test(resolveData.request)
55 ) {
56 if ("contextRegExp" in this.options && this.options.contextRegExp) {
57 // if "contextRegExp" is given,
58 // both the "resourceRegExp" and "contextRegExp" have to match.
59 if (this.options.contextRegExp.test(resolveData.context)) {
60 return false;
61 }
62 } else {
63 return false;
64 }
65 }
66 }
67
68 /**
69 * Apply the plugin
70 * @param {Compiler} compiler the compiler instance
71 * @returns {void}
72 */
73 apply(compiler) {
74 compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
75 nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
76 });
77 compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
78 cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
79 });
80 }
81}
82
83module.exports = IgnorePlugin;