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