CommonJsDependencyHelpers.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const RuntimeGlobals = require("../RuntimeGlobals");
  7. exports.handleDependencyBase = (depBase, module, runtimeRequirements) => {
  8. let base = undefined;
  9. let type;
  10. switch (depBase) {
  11. case "exports":
  12. runtimeRequirements.add(RuntimeGlobals.exports);
  13. base = module.exportsArgument;
  14. type = "expression";
  15. break;
  16. case "module.exports":
  17. runtimeRequirements.add(RuntimeGlobals.module);
  18. base = `${module.moduleArgument}.exports`;
  19. type = "expression";
  20. break;
  21. case "this":
  22. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  23. base = "this";
  24. type = "expression";
  25. break;
  26. case "Object.defineProperty(exports)":
  27. runtimeRequirements.add(RuntimeGlobals.exports);
  28. base = module.exportsArgument;
  29. type = "Object.defineProperty";
  30. break;
  31. case "Object.defineProperty(module.exports)":
  32. runtimeRequirements.add(RuntimeGlobals.module);
  33. base = `${module.moduleArgument}.exports`;
  34. type = "Object.defineProperty";
  35. break;
  36. case "Object.defineProperty(this)":
  37. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  38. base = "this";
  39. type = "Object.defineProperty";
  40. break;
  41. default:
  42. throw new Error(`Unsupported base ${depBase}`);
  43. }
  44. return [type, base];
  45. };