UNPKG

@vue-async/module-loader

Version:
280 lines 13.7 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; /** * moduleLoader */ import warning from 'warning'; import { isPlainObject, isArray, isFunction } from '@ace-util/core'; import * as spa from '../utils/spa'; var noop = function () { }; function promisify(promise) { if (promise && promise instanceof Promise && typeof promise.then === 'function') { return promise; } return Promise.resolve(promise); } /** 格式化模块配置 */ function formatModules(config, errorCallback) { if (errorCallback === void 0) { errorCallback = function () { }; } if (Array.isArray(config)) { return config.reduce(function (prev, curr) { return __spreadArray(__spreadArray([], __read(prev), false), __read(formatModules(curr)), false); }, []); } else if (isPlainObject(config)) { var _modules_1 = config; // no lib name if (_modules_1.entry && typeof _modules_1.entry === 'string') { return [ __assign(__assign({}, config), { styles: _modules_1.styles ? (!Array.isArray(_modules_1.styles) ? [_modules_1.styles] : _modules_1.styles) : [] }), ]; } else { return Object.keys(_modules_1).map(function (moduleName) { return typeof _modules_1[moduleName] === 'string' ? { entry: _modules_1[moduleName], moduleName: moduleName, } : __assign(__assign({}, _modules_1[moduleName]), { styles: _modules_1[moduleName].styles ? !Array.isArray(_modules_1[moduleName].styles) ? [_modules_1[moduleName].styles] : _modules_1[moduleName].styles : [], moduleName: moduleName }); }); } } else if (isFunction(config)) { return [config]; } else { errorCallback("not support \"".concat(typeof config, "\" type module"), config); return []; } } /** 校验子应用导出的 生命周期 对象是否正确 */ function validateExportLifecycle(exports) { // export default function(){} 兼容处理 var _export = (exports && exports.default) || exports; if (isFunction(_export)) { return true; } else { // 生命周期 // todo: 延迟加载 // const { bootstrap ,mount, unmount} = exports ?? {}; // return isFunction(bootstrap) && isFunction(mount) && isFunction(unmount); var bootstrap = (exports || {}).bootstrap; return isFunction(bootstrap); } } /** 获取生命周期对象 */ function getLifecyclesFromExports(scriptExports, moduleName, global) { if (validateExportLifecycle(scriptExports)) { var _export = (scriptExports && scriptExports.default) || scriptExports; return isFunction(_export) ? { bootstrap: _export } : scriptExports; } warning(process.env.NODE_ENV === 'production', "[moduleLoader] lifecycle not found from ".concat(moduleName, " entry exports, fallback to get from window['").concat(moduleName, "']")); // fallback to global variable who named with ${moduleName} while module exports not found var globalVariableExports = global.exports ? global.exports[moduleName] : global[moduleName]; if (validateExportLifecycle(globalVariableExports)) { var _export = (globalVariableExports && globalVariableExports.default) || globalVariableExports; return isFunction(_export) ? { bootstrap: _export } : globalVariableExports; } throw new Error("[moduleLoader] You need to export lifecycle functions in ".concat(moduleName, " entry")); } export default (function (Vue, status) { return function loader(config, options) { if (options === void 0) { options = {}; } var sync = options.sync, _a = options.register, register = _a === void 0 ? noop : _a, _b = options.onLoading, onLoading = _b === void 0 ? noop : _b, _c = options.onLoaded, onLoaded = _c === void 0 ? noop : _c, _d = options.onError, onError = _d === void 0 ? function (name, error) { warning(false, "An error occurred while loading module \"".concat(name, "\", Error: ").concat(error.message)); } : _d, success = options.success, error = options.error; status.current = false; // eslint-disable-next-line @typescript-eslint/no-this-alias var _self = this; var _modules = formatModules(config, error); // 按顺序同步执行 if (sync === true) { return new Promise(function (resolve) { return schedule(0, function () { status.current = true; success ? success() : resolve(); }); }); } else { return new Promise(function (resolve) { return Promise.all(_modules.map(function (module) { return exec(module); })).then(function () { status.current = true; success ? success() : resolve(); }); }); } function exec(module) { return __awaiter(this, void 0, void 0, function () { var bootstrapResult, result, moduleName, entry, _a, styles, args, global_1, scriptExports, bootstrap, bootstrapResult, result, err_1, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!isFunction(module)) return [3 /*break*/, 2]; return [4 /*yield*/, promisify(module.call(_self, Vue))]; case 1: bootstrapResult = _c.sent(); if (bootstrapResult) { result = {}; if (isArray(bootstrapResult)) { result = { routes: bootstrapResult, }; } else { result = bootstrapResult; } register(result); } return [3 /*break*/, 14]; case 2: moduleName = module.moduleName, entry = module.entry, _a = module.styles, styles = _a === void 0 ? [] : _a, args = module.args; global_1 = window; if (!global_1.Vue) { global_1.Vue = Vue; } _c.label = 3; case 3: _c.trys.push([3, 9, , 14]); // load styles return [4 /*yield*/, spa.execStyles(styles, moduleName)]; case 4: // load styles _c.sent(); return [4 /*yield*/, spa.execScript(entry, global_1)]; case 5: scriptExports = _c.sent(); return [4 /*yield*/, promisify(onLoading(moduleName))]; case 6: _c.sent(); bootstrap = getLifecyclesFromExports(scriptExports, moduleName, global_1).bootstrap; return [4 /*yield*/, promisify(bootstrap.call(_self, Vue, args))]; case 7: bootstrapResult = _c.sent(); if (bootstrapResult) { result = {}; if (isArray(bootstrapResult)) { result = { routes: bootstrapResult, }; } else { result = bootstrapResult; } register(result); } return [4 /*yield*/, promisify(onLoaded(moduleName))]; case 8: _c.sent(); return [3 /*break*/, 14]; case 9: err_1 = _c.sent(); _c.label = 10; case 10: _c.trys.push([10, 12, , 13]); return [4 /*yield*/, promisify(onError(moduleName, err_1))]; case 11: _c.sent(); // 方法已过时 if (error && typeof error === 'function') { // eslint-disable-next-line no-console console.warn('"error" function is deprecated, using "onError" function!'); error(err_1.message, module); } return [3 /*break*/, 13]; case 12: _b = _c.sent(); return [3 /*break*/, 13]; case 13: return [3 /*break*/, 14]; case 14: return [2 /*return*/]; } }); }); } function schedule(index, resolve) { if (index < _modules.length) { exec(_modules[index]).then(function () { schedule(index + 1, resolve); }); } else { resolve(); } } }; }); //# sourceMappingURL=moduleLoader.js.map