2018-11-20 17:33:02 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const Template = require("../Template");
|
|
|
|
const HelperRuntimeModule = require("./HelperRuntimeModule");
|
|
|
|
|
|
|
|
class CreateFakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
|
|
|
|
constructor() {
|
|
|
|
super("create fake namespace object");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
2018-11-20 19:05:12 +08:00
|
|
|
generate() {
|
2019-08-27 02:21:07 +08:00
|
|
|
const { runtimeTemplate } = this.compilation;
|
2018-11-20 17:33:02 +08:00
|
|
|
const fn = RuntimeGlobals.createFakeNamespaceObject;
|
|
|
|
return Template.asString([
|
2020-11-17 00:19:36 +08:00
|
|
|
`var getProto = Object.getPrototypeOf ? ${runtimeTemplate.returningFunction(
|
|
|
|
"Object.getPrototypeOf(obj)",
|
|
|
|
"obj"
|
|
|
|
)} : ${runtimeTemplate.returningFunction("obj.__proto__", "obj")};`,
|
|
|
|
"var leafPrototypes;",
|
2018-11-20 17:33:02 +08:00
|
|
|
"// create a fake namespace object",
|
|
|
|
"// mode & 1: value is a module id, require it",
|
|
|
|
"// mode & 2: merge all properties of value into the ns",
|
|
|
|
"// mode & 4: return value when already ns object",
|
2020-11-17 03:24:54 +08:00
|
|
|
"// mode & 16: return value when it's Promise-like",
|
2018-11-20 17:33:02 +08:00
|
|
|
"// mode & 8|1: behave like require",
|
2019-08-27 02:21:07 +08:00
|
|
|
// Note: must be a function (not arrow), because this is used in body!
|
2018-11-20 17:33:02 +08:00
|
|
|
`${fn} = function(value, mode) {`,
|
|
|
|
Template.indent([
|
2018-11-28 20:07:40 +08:00
|
|
|
`if(mode & 1) value = this(value);`,
|
2018-11-20 17:33:02 +08:00
|
|
|
`if(mode & 8) return value;`,
|
2020-11-17 03:24:54 +08:00
|
|
|
"if(typeof value === 'object' && value) {",
|
|
|
|
Template.indent([
|
|
|
|
"if((mode & 4) && value.__esModule) return value;",
|
|
|
|
"if((mode & 16) && typeof value.then === 'function') return value;"
|
|
|
|
]),
|
|
|
|
"}",
|
2018-11-20 17:33:02 +08:00
|
|
|
"var ns = Object.create(null);",
|
|
|
|
`${RuntimeGlobals.makeNamespaceObject}(ns);`,
|
2019-11-05 04:05:17 +08:00
|
|
|
"var def = {};",
|
2020-11-17 03:24:54 +08:00
|
|
|
"leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];",
|
2020-11-17 00:19:36 +08:00
|
|
|
"for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {",
|
2019-08-23 20:07:01 +08:00
|
|
|
Template.indent([
|
2021-03-14 00:27:11 +08:00
|
|
|
`Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.basicFunction(
|
|
|
|
"key",
|
2021-03-14 00:44:59 +08:00
|
|
|
`def[key] = ${runtimeTemplate.returningFunction("value[key]", "")};`
|
2021-03-14 00:27:11 +08:00
|
|
|
)});`
|
2019-08-23 20:07:01 +08:00
|
|
|
]),
|
|
|
|
"}",
|
2021-03-14 00:34:29 +08:00
|
|
|
`def['default'] = ${runtimeTemplate.returningFunction("value", "")};`,
|
2019-11-05 04:05:17 +08:00
|
|
|
`${RuntimeGlobals.definePropertyGetters}(ns, def);`,
|
2018-11-20 17:33:02 +08:00
|
|
|
"return ns;"
|
|
|
|
]),
|
|
|
|
"};"
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CreateFakeNamespaceObjectRuntimeModule;
|