NuclearDispersionSystem/ant-design-vue-jeecg/node_modules/qiankun/lib/prefetch.js
2023-09-14 14:47:11 +08:00

165 lines
5.2 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.doPrefetchStrategy = doPrefetchStrategy;
exports.prefetchImmediately = prefetchImmediately;
var _isFunction2 = _interopRequireDefault(require("lodash/isFunction"));
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _tslib = require("tslib");
var _importHtmlEntry = require("import-html-entry");
var _singleSpa = require("single-spa");
/**
* @author Kuitos
* @since 2019-02-26
*/
// RIC and shim for browsers setTimeout() without it
var requestIdleCallback = window.requestIdleCallback || function requestIdleCallback(cb) {
var start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function timeRemaining() {
return Math.max(0, 50 - (Date.now() - start));
}
});
}, 1);
};
var isSlowNetwork = navigator.connection ? navigator.connection.saveData || navigator.connection.type !== 'wifi' && navigator.connection.type !== 'ethernet' && /([23])g/.test(navigator.connection.effectiveType) : false;
/**
* prefetch assets, do nothing while in mobile network
* @param entry
* @param opts
*/
function prefetch(entry, opts) {
var _this = this;
if (!navigator.onLine || isSlowNetwork) {
// Don't prefetch if in a slow network or offline
return;
}
requestIdleCallback(function () {
return (0, _tslib.__awaiter)(_this, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee() {
var _yield$importEntry, getExternalScripts, getExternalStyleSheets;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _importHtmlEntry.importEntry)(entry, opts);
case 2:
_yield$importEntry = _context.sent;
getExternalScripts = _yield$importEntry.getExternalScripts;
getExternalStyleSheets = _yield$importEntry.getExternalStyleSheets;
requestIdleCallback(getExternalStyleSheets);
requestIdleCallback(getExternalScripts);
case 7:
case "end":
return _context.stop();
}
}
}, _callee);
}));
});
}
function prefetchAfterFirstMounted(apps, opts) {
window.addEventListener('single-spa:first-mount', function listener() {
var notLoadedApps = apps.filter(function (app) {
return (0, _singleSpa.getAppStatus)(app.name) === _singleSpa.NOT_LOADED;
});
if (process.env.NODE_ENV === 'development') {
var mountedApps = (0, _singleSpa.getMountedApps)();
console.log("[qiankun] prefetch starting after ".concat(mountedApps, " mounted..."), notLoadedApps);
}
notLoadedApps.forEach(function (_ref) {
var entry = _ref.entry;
return prefetch(entry, opts);
});
window.removeEventListener('single-spa:first-mount', listener);
});
}
function prefetchImmediately(apps, opts) {
if (process.env.NODE_ENV === 'development') {
console.log('[qiankun] prefetch starting for apps...', apps);
}
apps.forEach(function (_ref2) {
var entry = _ref2.entry;
return prefetch(entry, opts);
});
}
function doPrefetchStrategy(apps, prefetchStrategy, importEntryOpts) {
var _this2 = this;
var appsName2Apps = function appsName2Apps(names) {
return apps.filter(function (app) {
return names.includes(app.name);
});
};
if (Array.isArray(prefetchStrategy)) {
prefetchAfterFirstMounted(appsName2Apps(prefetchStrategy), importEntryOpts);
} else if ((0, _isFunction2.default)(prefetchStrategy)) {
(function () {
return (0, _tslib.__awaiter)(_this2, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee2() {
var _yield$prefetchStrate, _yield$prefetchStrate2, criticalAppNames, _yield$prefetchStrate3, minorAppsName;
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return prefetchStrategy(apps);
case 2:
_yield$prefetchStrate = _context2.sent;
_yield$prefetchStrate2 = _yield$prefetchStrate.criticalAppNames;
criticalAppNames = _yield$prefetchStrate2 === void 0 ? [] : _yield$prefetchStrate2;
_yield$prefetchStrate3 = _yield$prefetchStrate.minorAppsName;
minorAppsName = _yield$prefetchStrate3 === void 0 ? [] : _yield$prefetchStrate3;
prefetchImmediately(appsName2Apps(criticalAppNames), importEntryOpts);
prefetchAfterFirstMounted(appsName2Apps(minorAppsName), importEntryOpts);
case 9:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
})();
} else {
switch (prefetchStrategy) {
case true:
prefetchAfterFirstMounted(apps, importEntryOpts);
break;
case 'all':
prefetchImmediately(apps, importEntryOpts);
break;
default:
break;
}
}
}