IgnorePlugin.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class IgnorePlugin {
  7. constructor(resourceRegExp, contextRegExp) {
  8. this.resourceRegExp = resourceRegExp;
  9. this.contextRegExp = contextRegExp;
  10. this.checkIgnore = this.checkIgnore.bind(this);
  11. }
  12. /*
  13. * Only returns true if a "resourceRegExp" exists
  14. * and the resource given matches the regexp.
  15. */
  16. checkResource(resource) {
  17. if(!this.resourceRegExp) {
  18. return false;
  19. }
  20. return this.resourceRegExp.test(resource);
  21. }
  22. /*
  23. * Returns true if contextRegExp does not exist
  24. * or if context matches the given regexp.
  25. */
  26. checkContext(context) {
  27. if(!this.contextRegExp) {
  28. return true;
  29. }
  30. return this.contextRegExp.test(context);
  31. }
  32. /*
  33. * Returns true if result should be ignored.
  34. * false if it shouldn't.
  35. *
  36. * Not that if "contextRegExp" is given, both the "resourceRegExp"
  37. * and "contextRegExp" have to match.
  38. */
  39. checkResult(result) {
  40. if(!result) {
  41. return true;
  42. }
  43. return this.checkResource(result.request) && this.checkContext(result.context);
  44. }
  45. checkIgnore(result, callback) {
  46. // check if result is ignored
  47. if(this.checkResult(result)) {
  48. return callback();
  49. }
  50. return callback(null, result);
  51. }
  52. apply(compiler) {
  53. compiler.plugin("normal-module-factory", (nmf) => {
  54. nmf.plugin("before-resolve", this.checkIgnore);
  55. });
  56. compiler.plugin("context-module-factory", (cmf) => {
  57. cmf.plugin("before-resolve", this.checkIgnore);
  58. });
  59. }
  60. }
  61. module.exports = IgnorePlugin;