ContextDependency.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Dependency = require("../Dependency");
  7. const CriticalDependencyWarning = require("./CriticalDependencyWarning");
  8. const regExpToString = r => (r ? r + "" : "");
  9. class ContextDependency extends Dependency {
  10. // options: { request, recursive, regExp, include, exclude, mode, chunkName, groupOptions }
  11. constructor(options) {
  12. super();
  13. this.options = options;
  14. this.userRequest = this.options.request;
  15. /** @type {false | string} */
  16. this.critical = false;
  17. this.hadGlobalOrStickyRegExp = false;
  18. if (this.options.regExp.global || this.options.regExp.sticky) {
  19. this.options.regExp = null;
  20. this.hadGlobalOrStickyRegExp = true;
  21. }
  22. }
  23. getResourceIdentifier() {
  24. return (
  25. `context${this.options.request} ${this.options.recursive} ` +
  26. `${regExpToString(this.options.regExp)} ${regExpToString(
  27. this.options.include
  28. )} ${regExpToString(this.options.exclude)} ` +
  29. `${this.options.mode} ${this.options.chunkName} ` +
  30. `${JSON.stringify(this.options.groupOptions)}`
  31. );
  32. }
  33. getWarnings() {
  34. let warnings = super.getWarnings() || [];
  35. if (this.critical) {
  36. warnings.push(new CriticalDependencyWarning(this.critical));
  37. }
  38. if (this.hadGlobalOrStickyRegExp) {
  39. warnings.push(
  40. new CriticalDependencyWarning(
  41. "Contexts can't use RegExps with the 'g' or 'y' flags."
  42. )
  43. );
  44. }
  45. return warnings;
  46. }
  47. }
  48. // TODO remove in webpack 5
  49. Object.defineProperty(ContextDependency.prototype, "async", {
  50. configurable: false,
  51. get() {
  52. throw new Error(
  53. "ContextDependency.async was removed. Use ContextDependency.options.mode instead."
  54. );
  55. },
  56. set() {
  57. throw new Error(
  58. "ContextDependency.async was removed. Pass options.mode to constructor instead"
  59. );
  60. }
  61. });
  62. module.exports = ContextDependency;