[6a3a178] | 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 | class CreateFakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
---|
| 12 | constructor() {
|
---|
| 13 | super("create fake namespace object");
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @returns {string} runtime code
|
---|
| 18 | */
|
---|
| 19 | generate() {
|
---|
| 20 | const { runtimeTemplate } = this.compilation;
|
---|
| 21 | const fn = RuntimeGlobals.createFakeNamespaceObject;
|
---|
| 22 | return Template.asString([
|
---|
| 23 | `var getProto = Object.getPrototypeOf ? ${runtimeTemplate.returningFunction(
|
---|
| 24 | "Object.getPrototypeOf(obj)",
|
---|
| 25 | "obj"
|
---|
| 26 | )} : ${runtimeTemplate.returningFunction("obj.__proto__", "obj")};`,
|
---|
| 27 | "var 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 | "var ns = Object.create(null);",
|
---|
| 46 | `${RuntimeGlobals.makeNamespaceObject}(ns);`,
|
---|
| 47 | "var def = {};",
|
---|
| 48 | "leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];",
|
---|
| 49 | "for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {",
|
---|
| 50 | Template.indent([
|
---|
| 51 | `Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.expressionFunction(
|
---|
| 52 | `def[key] = ${runtimeTemplate.returningFunction("value[key]", "")}`,
|
---|
| 53 | "key"
|
---|
| 54 | )});`
|
---|
| 55 | ]),
|
---|
| 56 | "}",
|
---|
| 57 | `def['default'] = ${runtimeTemplate.returningFunction("value", "")};`,
|
---|
| 58 | `${RuntimeGlobals.definePropertyGetters}(ns, def);`,
|
---|
| 59 | "return ns;"
|
---|
| 60 | ]),
|
---|
| 61 | "};"
|
---|
| 62 | ]);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | module.exports = CreateFakeNamespaceObjectRuntimeModule;
|
---|