CompatGetDefaultExportRuntimeModule.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 CompatGetDefaultExportRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("compat get default export");
  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.compatGetDefaultExport;
  20. return Template.asString([
  21. "// getDefaultExport function for compatibility with non-harmony modules",
  22. `${fn} = ${runtimeTemplate.basicFunction("module", [
  23. "var getter = module && module.__esModule ?",
  24. Template.indent([
  25. `${runtimeTemplate.returningFunction("module['default']")} :`,
  26. `${runtimeTemplate.returningFunction("module")};`
  27. ]),
  28. `${RuntimeGlobals.definePropertyGetters}(getter, { a: getter });`,
  29. "return getter;"
  30. ])};`
  31. ]);
  32. }
  33. }
  34. module.exports = CompatGetDefaultExportRuntimeModule;