IgnorePlugin.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const PLUGIN_NAME = "IgnorePlugin";
  21. class IgnorePlugin {
  22. /**
  23. * @param {IgnorePluginOptions} options IgnorePlugin options
  24. */
  25. constructor(options) {
  26. validate(options);
  27. this.options = options;
  28. this.checkIgnore = this.checkIgnore.bind(this);
  29. }
  30. /**
  31. * Note that if "contextRegExp" is given, both the "resourceRegExp" and "contextRegExp" have to match.
  32. * @param {ResolveData} 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. if (
  44. "resourceRegExp" in this.options &&
  45. this.options.resourceRegExp &&
  46. this.options.resourceRegExp.test(resolveData.request)
  47. ) {
  48. if ("contextRegExp" in this.options && this.options.contextRegExp) {
  49. // if "contextRegExp" is given,
  50. // both the "resourceRegExp" and "contextRegExp" have to match.
  51. if (this.options.contextRegExp.test(resolveData.context)) {
  52. return false;
  53. }
  54. } else {
  55. return false;
  56. }
  57. }
  58. }
  59. /**
  60. * Apply the plugin
  61. * @param {Compiler} compiler the compiler instance
  62. * @returns {void}
  63. */
  64. apply(compiler) {
  65. compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, nmf => {
  66. nmf.hooks.beforeResolve.tap(PLUGIN_NAME, resolveData => {
  67. const result = this.checkIgnore(resolveData);
  68. if (
  69. result === false &&
  70. resolveData.dependencies.length > 0 &&
  71. resolveData.dependencies[0] instanceof EntryDependency
  72. ) {
  73. resolveData.ignoredModule = new RawModule(
  74. "",
  75. "ignored-entry-module",
  76. "(ignored-entry-module)"
  77. );
  78. }
  79. return result;
  80. });
  81. });
  82. compiler.hooks.contextModuleFactory.tap(PLUGIN_NAME, cmf => {
  83. cmf.hooks.beforeResolve.tap(PLUGIN_NAME, this.checkIgnore);
  84. });
  85. }
  86. }
  87. module.exports = IgnorePlugin;