[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const RawModule = require("./RawModule");
|
---|
| 9 | const EntryDependency = require("./dependencies/EntryDependency");
|
---|
| 10 | const createSchemaValidation = require("./util/create-schema-validation");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
|
---|
| 13 | /** @typedef {import("./Compiler")} Compiler */
|
---|
| 14 | /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
|
---|
| 15 |
|
---|
| 16 | const validate = createSchemaValidation(
|
---|
| 17 | require("../schemas/plugins/IgnorePlugin.check.js"),
|
---|
| 18 | () => require("../schemas/plugins/IgnorePlugin.json"),
|
---|
| 19 | {
|
---|
| 20 | name: "Ignore Plugin",
|
---|
| 21 | baseDataPath: "options"
|
---|
| 22 | }
|
---|
| 23 | );
|
---|
| 24 |
|
---|
| 25 | class IgnorePlugin {
|
---|
| 26 | /**
|
---|
| 27 | * @param {IgnorePluginOptions} options IgnorePlugin options
|
---|
| 28 | */
|
---|
| 29 | constructor(options) {
|
---|
| 30 | validate(options);
|
---|
| 31 | this.options = options;
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @private
|
---|
| 35 | * @type {Function}
|
---|
| 36 | */
|
---|
| 37 | this.checkIgnore = this.checkIgnore.bind(this);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Note that if "contextRegExp" is given, both the "resourceRegExp" and "contextRegExp" have to match.
|
---|
| 42 | * @param {ResolveData} resolveData resolve data
|
---|
| 43 | * @returns {false|undefined} returns false when the request should be ignored, otherwise undefined
|
---|
| 44 | */
|
---|
| 45 | checkIgnore(resolveData) {
|
---|
| 46 | if (
|
---|
| 47 | "checkResource" in this.options &&
|
---|
| 48 | this.options.checkResource &&
|
---|
| 49 | this.options.checkResource(resolveData.request, resolveData.context)
|
---|
| 50 | ) {
|
---|
| 51 | return false;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | if (
|
---|
| 55 | "resourceRegExp" in this.options &&
|
---|
| 56 | this.options.resourceRegExp &&
|
---|
| 57 | this.options.resourceRegExp.test(resolveData.request)
|
---|
| 58 | ) {
|
---|
| 59 | if ("contextRegExp" in this.options && this.options.contextRegExp) {
|
---|
| 60 | // if "contextRegExp" is given,
|
---|
| 61 | // both the "resourceRegExp" and "contextRegExp" have to match.
|
---|
| 62 | if (this.options.contextRegExp.test(resolveData.context)) {
|
---|
| 63 | return false;
|
---|
| 64 | }
|
---|
| 65 | } else {
|
---|
| 66 | return false;
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Apply the plugin
|
---|
| 73 | * @param {Compiler} compiler the compiler instance
|
---|
| 74 | * @returns {void}
|
---|
| 75 | */
|
---|
| 76 | apply(compiler) {
|
---|
| 77 | compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
|
---|
| 78 | nmf.hooks.beforeResolve.tap("IgnorePlugin", resolveData => {
|
---|
| 79 | const result = this.checkIgnore(resolveData);
|
---|
| 80 |
|
---|
| 81 | if (
|
---|
| 82 | result === false &&
|
---|
| 83 | resolveData.dependencies.length > 0 &&
|
---|
| 84 | resolveData.dependencies[0] instanceof EntryDependency
|
---|
| 85 | ) {
|
---|
| 86 | resolveData.ignoredModule = new RawModule(
|
---|
| 87 | "",
|
---|
| 88 | "ignored-entry-module",
|
---|
| 89 | "(ignored-entry-module)"
|
---|
| 90 | );
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return result;
|
---|
| 94 | });
|
---|
| 95 | });
|
---|
| 96 | compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
|
---|
| 97 | cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
|
---|
| 98 | });
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | module.exports = IgnorePlugin;
|
---|