Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IgnorePlugin.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
  9. /** @typedef {import("./Compiler")} Compiler */
  10. class IgnorePlugin {
  11. /**
  12. * @param {IgnorePluginOptions} options IgnorePlugin options
  13. */
  14. constructor(options) {
  15. // TODO webpack 5 remove this compat-layer
  16. if (arguments.length > 1 || options instanceof RegExp) {
  17. options = {
  18. resourceRegExp: arguments[0],
  19. contextRegExp: arguments[1]
  20. };
  21. }
  22. validateOptions(schema, options, "IgnorePlugin");
  23. this.options = options;
  24. /** @private @type {Function} */
  25. this.checkIgnore = this.checkIgnore.bind(this);
  26. }
  27. /**
  28. * Note that if "contextRegExp" is given, both the "resourceRegExp"
  29. * and "contextRegExp" have to match.
  30. *
  31. * @param {TODO} result result
  32. * @returns {TODO|null} returns result or null if result should be ignored
  33. */
  34. checkIgnore(result) {
  35. if (!result) return result;
  36. if (
  37. "checkResource" in this.options &&
  38. this.options.checkResource &&
  39. this.options.checkResource(result.request, result.context)
  40. ) {
  41. // TODO webpack 5 remove checkContext, as checkResource already gets context
  42. if ("checkContext" in this.options && this.options.checkContext) {
  43. if (this.options.checkContext(result.context)) {
  44. return null;
  45. }
  46. } else {
  47. return null;
  48. }
  49. }
  50. if (
  51. "resourceRegExp" in this.options &&
  52. this.options.resourceRegExp &&
  53. this.options.resourceRegExp.test(result.request)
  54. ) {
  55. if ("contextRegExp" in this.options && this.options.contextRegExp) {
  56. // if "contextRegExp" is given,
  57. // both the "resourceRegExp" and "contextRegExp" have to match.
  58. if (this.options.contextRegExp.test(result.context)) {
  59. return null;
  60. }
  61. } else {
  62. return null;
  63. }
  64. }
  65. return result;
  66. }
  67. /**
  68. * @param {Compiler} compiler Webpack Compiler
  69. * @returns {void}
  70. */
  71. apply(compiler) {
  72. compiler.hooks.normalModuleFactory.tap("IgnorePlugin", nmf => {
  73. nmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
  74. });
  75. compiler.hooks.contextModuleFactory.tap("IgnorePlugin", cmf => {
  76. cmf.hooks.beforeResolve.tap("IgnorePlugin", this.checkIgnore);
  77. });
  78. }
  79. }
  80. module.exports = IgnorePlugin;