Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const validateOptions = require("schema-utils");
  7. const schema = require("../schemas/plugins/IgnorePlugin.json");
  8. /** @typedef {import("./Compiler")} Compiler */
  9. class IgnorePlugin {
  10. /**
  11. * @param {object} options IgnorePlugin options
  12. * @param {RegExp} options.resourceRegExp - A RegExp to test the request against
  13. * @param {RegExp} options.contextRegExp - A RegExp to test the context (directory) against
  14. * @param {function(string): boolean=} options.checkResource - A filter function for resource
  15. * @param {function(string): boolean=} options.checkContext - A filter function for context
  16. */
  17. constructor(options) {
  18. // TODO webpack 5 remove this compat-layer
  19. if (arguments.length > 1 || options instanceof RegExp) {
  20. options = {
  21. resourceRegExp: arguments[0],
  22. contextRegExp: arguments[1]
  23. };
  24. }
  25. validateOptions(schema, options, "IgnorePlugin");
  26. this.options = options;
  27. /** @private @type {Function} */
  28. this.checkIgnore = this.checkIgnore.bind(this);
  29. }
  30. /**
  31. * @param {string} resource resource
  32. * @returns {boolean} returns true if a "resourceRegExp" exists
  33. * and the resource given matches the regexp.
  34. */
  35. checkResource(resource) {
  36. if (this.options.checkResource) {
  37. return this.options.checkResource(resource);
  38. }
  39. if (!this.options.resourceRegExp) {
  40. return false;
  41. }
  42. return this.options.resourceRegExp.test(resource);
  43. }
  44. /**
  45. * @param {string} context context
  46. * @returns {boolean} returns true if "contextRegExp" does not exist
  47. * or if context matches the given regexp.
  48. */
  49. checkContext(context) {
  50. if (this.options.checkContext) {
  51. return this.options.checkContext(context);
  52. }
  53. if (!this.options.contextRegExp) {
  54. return true;
  55. }
  56. return this.options.contextRegExp.test(context);
  57. }
  58. /**
  59. * Note that if "contextRegExp" is given, both the "resourceRegExp"
  60. * and "contextRegExp" have to match.
  61. *
  62. * @param {TODO} result result
  63. * @returns {boolean} returns true if result should be ignored
  64. */
  65. checkResult(result) {
  66. if (!result) {
  67. return true;
  68. }
  69. return (
  70. this.checkResource(result.request) && this.checkContext(result.context)
  71. );
  72. }
  73. /**
  74. * @param {TODO} result result
  75. * @returns {TODO|null} returns result or null if result should be ignored
  76. */
  77. checkIgnore(result) {
  78. // check if result is ignored
  79. if (this.checkResult(result)) {
  80. return null;
  81. }
  82. return result;
  83. }
  84. /**
  85. * @param {Compiler} compiler Webpack Compiler
  86. * @returns {void}
  87. */
  88. apply(compiler) {
  89. compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
  90. nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
  91. });
  92. compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
  93. cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
  94. });
  95. }
  96. }
  97. module.exports = IgnorePlugin;