CompatGetDefaultExportRuntimeModule.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 CompatGetDefaultExportRuntimeModule extends HelperRuntimeModule {
  9. constructor() {
  10. super("compat get default export");
  11. }
  12. /**
  13. * @returns {string} runtime code
  14. */
  15. generate() {
  16. const { runtimeTemplate } = this.compilation;
  17. const fn = RuntimeGlobals.compatGetDefaultExport;
  18. return Template.asString([
  19. "// getDefaultExport function for compatibility with non-harmony modules",
  20. `${fn} = ${runtimeTemplate.basicFunction("module", [
  21. "var getter = module && module.__esModule ?",
  22. Template.indent([
  23. `${runtimeTemplate.returningFunction("module['default']")} :`,
  24. `${runtimeTemplate.returningFunction("module")};`
  25. ]),
  26. `${RuntimeGlobals.definePropertyGetters}(getter, { a: getter });`,
  27. "return getter;"
  28. ])};`
  29. ]);
  30. }
  31. }
  32. module.exports = CompatGetDefaultExportRuntimeModule;