CreateFakeNamespaceObjectRuntimeModule.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 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. return Template.asString([
  22. `var getProto = Object.getPrototypeOf ? ${runtimeTemplate.returningFunction(
  23. "Object.getPrototypeOf(obj)",
  24. "obj"
  25. )} : ${runtimeTemplate.returningFunction("obj.__proto__", "obj")};`,
  26. "var leafPrototypes;",
  27. "// create a fake namespace object",
  28. "// mode & 1: value is a module id, require it",
  29. "// mode & 2: merge all properties of value into the ns",
  30. "// mode & 4: return value when already ns object",
  31. "// mode & 16: return value when it's Promise-like",
  32. "// mode & 8|1: behave like require",
  33. // Note: must be a function (not arrow), because this is used in body!
  34. `${fn} = function(value, mode) {`,
  35. Template.indent([
  36. "if(mode & 1) value = this(value);",
  37. "if(mode & 8) return value;",
  38. "if(typeof value === 'object' && value) {",
  39. Template.indent([
  40. "if((mode & 4) && value.__esModule) return value;",
  41. "if((mode & 16) && typeof value.then === 'function') return value;"
  42. ]),
  43. "}",
  44. "var ns = Object.create(null);",
  45. `${RuntimeGlobals.makeNamespaceObject}(ns);`,
  46. "var def = {};",
  47. "leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];",
  48. "for(var current = mode & 2 && value; (typeof current == 'object' || typeof current == 'function') && !~leafPrototypes.indexOf(current); current = getProto(current)) {",
  49. Template.indent([
  50. `Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.expressionFunction(
  51. `def[key] = ${runtimeTemplate.returningFunction("value[key]", "")}`,
  52. "key"
  53. )});`
  54. ]),
  55. "}",
  56. `def['default'] = ${runtimeTemplate.returningFunction("value", "")};`,
  57. `${RuntimeGlobals.definePropertyGetters}(ns, def);`,
  58. "return ns;"
  59. ]),
  60. "};"
  61. ]);
  62. }
  63. }
  64. module.exports = CreateFakeNamespaceObjectRuntimeModule;