DefinePropertyGettersRuntimeModule.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const RuntimeGlobals = require("../RuntimeGlobals");
  6. const Template = require("../Template");
  7. const HelperRuntimeModule = require("./HelperRuntimeModule");
  8. /** @typedef {import("../Compilation")} Compilation */
  9. class DefinePropertyGettersRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("define property getters");
  12. }
  13. /**
  14. * @returns {string | null} runtime code
  15. */
  16. generate() {
  17. const compilation = /** @type {Compilation} */ (this.compilation);
  18. const { runtimeTemplate } = compilation;
  19. const fn = RuntimeGlobals.definePropertyGetters;
  20. return Template.asString([
  21. "// define getter functions for harmony exports",
  22. `${fn} = ${runtimeTemplate.basicFunction("exports, definition", [
  23. `for(var key in definition) {`,
  24. Template.indent([
  25. `if(${RuntimeGlobals.hasOwnProperty}(definition, key) && !${RuntimeGlobals.hasOwnProperty}(exports, key)) {`,
  26. Template.indent([
  27. "Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });"
  28. ]),
  29. "}"
  30. ]),
  31. "}"
  32. ])};`
  33. ]);
  34. }
  35. }
  36. module.exports = DefinePropertyGettersRuntimeModule;