IgnorePlugin.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. this.checkIgnore = this.checkIgnore.bind(this);
  28. }
  29. /**
  30. * Note that if "contextRegExp" is given, both the "resourceRegExp" and "contextRegExp" have to match.
  31. * @param {ResolveData} resolveData resolve data
  32. * @returns {false|undefined} returns false when the request should be ignored, otherwise undefined
  33. */
  34. checkIgnore(resolveData) {
  35. if (
  36. "checkResource" in this.options &&
  37. this.options.checkResource &&
  38. this.options.checkResource(resolveData.request, resolveData.context)
  39. ) {
  40. return false;
  41. }
  42. if (
  43. "resourceRegExp" in this.options &&
  44. this.options.resourceRegExp &&
  45. this.options.resourceRegExp.test(resolveData.request)
  46. ) {
  47. if ("contextRegExp" in this.options && this.options.contextRegExp) {
  48. // if "contextRegExp" is given,
  49. // both the "resourceRegExp" and "contextRegExp" have to match.
  50. if (this.options.contextRegExp.test(resolveData.context)) {
  51. return false;
  52. }
  53. } else {
  54. return false;
  55. }
  56. }
  57. }
  58. /**
  59. * Apply the plugin
  60. * @param {Compiler} compiler the compiler instance
  61. * @returns {void}
  62. */
  63. apply(compiler) {
  64. compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
  65. nmf.hooks.beforeResolve.tap("IgnorePlugin", resolveData => {
  66. const result = this.checkIgnore(resolveData);
  67. if (
  68. result === false &&
  69. resolveData.dependencies.length > 0 &&
  70. resolveData.dependencies[0] instanceof EntryDependency
  71. ) {
  72. resolveData.ignoredModule = new RawModule(
  73. "",
  74. "ignored-entry-module",
  75. "(ignored-entry-module)"
  76. );
  77. }
  78. return result;
  79. });
  80. });
  81. compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
  82. cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
  83. });
  84. }
  85. }
  86. module.exports = IgnorePlugin;