MakeNamespaceObjectRuntimeModule.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /** @import Compilation from "../Compilation" */
  9. class MakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("make namespace object");
  12. }
  13. /**
  14. * Returns true, if the runtime module should get it's own scope.
  15. * When false, `generate()` must emit complete statements ending with `;`
  16. * so a following runtime IIFE is not parsed as a call (ASI).
  17. * @returns {boolean} true, if the runtime module should get it's own scope
  18. */
  19. shouldIsolate() {
  20. return false;
  21. }
  22. /**
  23. * Generates runtime code for this runtime module.
  24. * @returns {string | null} runtime code
  25. */
  26. generate() {
  27. const compilation = /** @type {Compilation} */ (this.compilation);
  28. const { runtimeTemplate } = compilation;
  29. const fn = RuntimeGlobals.makeNamespaceObject;
  30. const toStringTag =
  31. "Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });";
  32. return Template.asString([
  33. "// define __esModule on exports",
  34. `${fn} = ${runtimeTemplate.basicFunction("exports", [
  35. // `environment.symbol` promises the well-known symbols too, so only an
  36. // environment without it needs the feature test around the tag.
  37. ...(runtimeTemplate.supportsSymbol()
  38. ? [toStringTag]
  39. : [
  40. "if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {",
  41. Template.indent([toStringTag]),
  42. "}"
  43. ]),
  44. "Object.defineProperty(exports, '__esModule', { value: true });"
  45. ])};`
  46. ]);
  47. }
  48. }
  49. module.exports = MakeNamespaceObjectRuntimeModule;