You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

4 years ago
  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;