DefinePropertyGettersRuntimeModule.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. class DefinePropertyGettersRuntimeModule extends HelperRuntimeModule {
  9. constructor() {
  10. super("define property getters");
  11. }
  12. /**
  13. * @returns {string} runtime code
  14. */
  15. generate() {
  16. const { runtimeTemplate } = this.compilation;
  17. const fn = RuntimeGlobals.definePropertyGetters;
  18. return Template.asString([
  19. "// define getter functions for harmony exports",
  20. `${fn} = ${runtimeTemplate.basicFunction("exports, definition", [
  21. `for(var key in definition) {`,
  22. Template.indent([
  23. `if(${RuntimeGlobals.hasOwnProperty}(definition, key) && !${RuntimeGlobals.hasOwnProperty}(exports, key)) {`,
  24. Template.indent([
  25. "Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });"
  26. ]),
  27. "}"
  28. ]),
  29. "}"
  30. ])};`
  31. ]);
  32. }
  33. }
  34. module.exports = DefinePropertyGettersRuntimeModule;