CreateFakeNamespaceObjectRuntimeModule.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 CreateFakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
  10. constructor() {
  11. super("create fake namespace object");
  12. }
  13. /**
  14. * Generates runtime code for this runtime module.
  15. * @returns {string | null} runtime code
  16. */
  17. generate() {
  18. const compilation = /** @type {Compilation} */ (this.compilation);
  19. const { runtimeTemplate } = compilation;
  20. const fn = RuntimeGlobals.createFakeNamespaceObject;
  21. const cst = runtimeTemplate.renderConst();
  22. const lt = runtimeTemplate.renderLet();
  23. return Template.asString([
  24. // No `__proto__` fallback: `Object.create` and `Object.defineProperty`
  25. // below already require the same ES5 baseline.
  26. `${cst} getProto = Object.getPrototypeOf;`,
  27. `${lt} leafPrototypes;`,
  28. "// create a fake namespace object",
  29. "// mode & 1: value is a module id, require it",
  30. "// mode & 2: merge all properties of value into the ns",
  31. "// mode & 4: return value when already ns object",
  32. "// mode & 16: return value when it's Promise-like",
  33. "// mode & 8|1: behave like require",
  34. // Note: must be a function (not arrow), because this is used in body!
  35. `${fn} = function(value, mode) {`,
  36. Template.indent([
  37. "if(mode & 1) value = this(value);",
  38. "if(mode & 8) return value;",
  39. "if(typeof value === 'object' && value) {",
  40. Template.indent([
  41. "if((mode & 4) && value.__esModule) return value;",
  42. "if((mode & 16) && typeof value.then === 'function') return value;"
  43. ]),
  44. "}",
  45. `${cst} ns = Object.create(null);`,
  46. `${RuntimeGlobals.makeNamespaceObject}(ns);`,
  47. `${cst} def = {};`,
  48. `${runtimeTemplate.assignOr(
  49. "leafPrototypes",
  50. "[null, getProto({}), getProto([]), getProto(getProto)]"
  51. )};`,
  52. "for(var current = mode & 2 && value; (typeof current == 'object' || typeof current == 'function') && !~leafPrototypes.indexOf(current); current = getProto(current)) {",
  53. Template.indent([
  54. `Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.expressionFunction(
  55. `def[key] = ${runtimeTemplate.returningFunction("value[key]", "")}`,
  56. "key"
  57. )});`
  58. ]),
  59. "}",
  60. `def['default'] = ${runtimeTemplate.returningFunction("value", "")};`,
  61. `${RuntimeGlobals.definePropertyGetters}(ns, def);`,
  62. "return ns;"
  63. ]),
  64. "};"
  65. ]);
  66. }
  67. }
  68. module.exports = CreateFakeNamespaceObjectRuntimeModule;