CreateFakeNamespaceObjectRuntimeModule.js 2.4 KB

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