IgnorePlugin.js 2.7 KB

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