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