| 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 |
|
|---|
| 11 | /** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
|
|---|
| 12 | /** @typedef {import("./Compiler")} Compiler */
|
|---|
| 13 | /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */
|
|---|
| 14 | /** @typedef {import("./ContextModuleFactory").BeforeContextResolveData} BeforeContextResolveData */
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {(resource: string, context: string) => boolean} CheckResourceFn */
|
|---|
| 17 |
|
|---|
| 18 | const PLUGIN_NAME = "IgnorePlugin";
|
|---|
| 19 |
|
|---|
| 20 | class IgnorePlugin {
|
|---|
| 21 | /**
|
|---|
| 22 | * Creates an instance of IgnorePlugin.
|
|---|
| 23 | * @param {IgnorePluginOptions} options IgnorePlugin options
|
|---|
| 24 | */
|
|---|
| 25 | constructor(options) {
|
|---|
| 26 | this.options = options;
|
|---|
| 27 | this.checkIgnore = this.checkIgnore.bind(this);
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Note that if "contextRegExp" is given, both the "resourceRegExp" and "contextRegExp" have to match.
|
|---|
| 32 | * @param {ResolveData | BeforeContextResolveData} resolveData resolve data
|
|---|
| 33 | * @returns {false | undefined} returns false when the request should be ignored, otherwise undefined
|
|---|
| 34 | */
|
|---|
| 35 | checkIgnore(resolveData) {
|
|---|
| 36 | if (
|
|---|
| 37 | "checkResource" in this.options &&
|
|---|
| 38 | this.options.checkResource &&
|
|---|
| 39 | this.options.checkResource(resolveData.request, resolveData.context)
|
|---|
| 40 | ) {
|
|---|
| 41 | return false;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | if (
|
|---|
| 45 | "resourceRegExp" in this.options &&
|
|---|
| 46 | this.options.resourceRegExp &&
|
|---|
| 47 | this.options.resourceRegExp.test(resolveData.request)
|
|---|
| 48 | ) {
|
|---|
| 49 | if ("contextRegExp" in this.options && this.options.contextRegExp) {
|
|---|
| 50 | // if "contextRegExp" is given,
|
|---|
| 51 | // both the "resourceRegExp" and "contextRegExp" have to match.
|
|---|
| 52 | if (this.options.contextRegExp.test(resolveData.context)) {
|
|---|
| 53 | return false;
|
|---|
| 54 | }
|
|---|
| 55 | } else {
|
|---|
| 56 | return false;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 63 | * @param {Compiler} compiler the compiler instance
|
|---|
| 64 | * @returns {void}
|
|---|
| 65 | */
|
|---|
| 66 | apply(compiler) {
|
|---|
| 67 | compiler.hooks.validate.tap(PLUGIN_NAME, () => {
|
|---|
| 68 | compiler.validate(
|
|---|
| 69 | /** @type {EXPECTED_ANY} */
|
|---|
| 70 | (require("../schemas/plugins/IgnorePlugin.json")),
|
|---|
| 71 | this.options,
|
|---|
| 72 | {
|
|---|
| 73 | name: "Ignore Plugin",
|
|---|
| 74 | baseDataPath: "options"
|
|---|
| 75 | },
|
|---|
| 76 | (options) => require("../schemas/plugins/IgnorePlugin.check")(options)
|
|---|
| 77 | );
|
|---|
| 78 | });
|
|---|
| 79 |
|
|---|
| 80 | compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
|
|---|
| 81 | nmf.hooks.beforeResolve.tap(PLUGIN_NAME, (resolveData) => {
|
|---|
| 82 | const result = this.checkIgnore(resolveData);
|
|---|
| 83 |
|
|---|
| 84 | if (
|
|---|
| 85 | result === false &&
|
|---|
| 86 | resolveData.dependencies.length > 0 &&
|
|---|
| 87 | resolveData.dependencies[0] instanceof EntryDependency
|
|---|
| 88 | ) {
|
|---|
| 89 | const module = new RawModule(
|
|---|
| 90 | "",
|
|---|
| 91 | "ignored-entry-module",
|
|---|
| 92 | "(ignored-entry-module)"
|
|---|
| 93 | );
|
|---|
| 94 | module.factoryMeta = { sideEffectFree: true };
|
|---|
| 95 |
|
|---|
| 96 | resolveData.ignoredModule = module;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | return result;
|
|---|
| 100 | });
|
|---|
| 101 | });
|
|---|
| 102 | compiler.hooks.contextModuleFactory.tap(PLUGIN_NAME, (cmf) => {
|
|---|
| 103 | cmf.hooks.beforeResolve.tap(PLUGIN_NAME, this.checkIgnore);
|
|---|
| 104 | });
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | module.exports = IgnorePlugin;
|
|---|