CommonJsDependencyHelpers.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /** @typedef {import("../Module")} Module */
  8. /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
  9. /** @typedef {"exports" | "module.exports" | "this" | "Object.defineProperty(exports)" | "Object.defineProperty(module.exports)" | "Object.defineProperty(this)"} CommonJSDependencyBaseKeywords */
  10. /**
  11. * @param {CommonJSDependencyBaseKeywords} depBase commonjs dependency base
  12. * @param {Module} module module
  13. * @param {RuntimeRequirements} runtimeRequirements runtime requirements
  14. * @returns {[string, string]} type and base
  15. */
  16. exports.handleDependencyBase = (depBase, module, runtimeRequirements) => {
  17. let base = undefined;
  18. let type;
  19. switch (depBase) {
  20. case "exports":
  21. runtimeRequirements.add(RuntimeGlobals.exports);
  22. base = module.exportsArgument;
  23. type = "expression";
  24. break;
  25. case "module.exports":
  26. runtimeRequirements.add(RuntimeGlobals.module);
  27. base = `${module.moduleArgument}.exports`;
  28. type = "expression";
  29. break;
  30. case "this":
  31. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  32. base = "this";
  33. type = "expression";
  34. break;
  35. case "Object.defineProperty(exports)":
  36. runtimeRequirements.add(RuntimeGlobals.exports);
  37. base = module.exportsArgument;
  38. type = "Object.defineProperty";
  39. break;
  40. case "Object.defineProperty(module.exports)":
  41. runtimeRequirements.add(RuntimeGlobals.module);
  42. base = `${module.moduleArgument}.exports`;
  43. type = "Object.defineProperty";
  44. break;
  45. case "Object.defineProperty(this)":
  46. runtimeRequirements.add(RuntimeGlobals.thisAsExports);
  47. base = "this";
  48. type = "Object.defineProperty";
  49. break;
  50. default:
  51. throw new Error(`Unsupported base ${depBase}`);
  52. }
  53. return [type, base];
  54. };