“use strict”;
var _interopRequireDefault = require(“@babel/runtime/helpers/interopRequireDefault”);
Object.defineProperty(exports, “__esModule”, {
value: true
});
exports.defaultGetPublicPath = defaultGetPublicPath;
exports.evalCode = evalCode;
exports.getGlobalProp = getGlobalProp;
exports.getInlineCode = getInlineCode;
exports.isModuleScriptSupported = isModuleScriptSupported;
exports.noteGlobalProps = noteGlobalProps;
exports.readResAsString = readResAsString;
exports.requestIdleCallback = void 0;
var _slicedToArray2 = _interopRequireDefault(require(“@babel/runtime/helpers/slicedToArray”));
var _typeof2 = _interopRequireDefault(require(“@babel/runtime/helpers/typeof”));
/**
function shouldSkipProperty(global, p) {
// 如果不是global原生的属性,p不是一个数字类型并且p小于global的长度,返回true
if (!global.hasOwnProperty§ || !isNaN§ && p < global.length) return true;
if (isIE11) {
// https://github.com/kuitos/import-html-entry/pull/32,最小化 try 范围
try {
return global[p] && typeof window !== ‘undefined’ && global[p].parent === window;
} catch (err) {
return true;
}
} else {
return false;
}
} // safari unpredictably lists some new globals first or second in object order
var firstGlobalProp, secondGlobalProp, lastGlobalProp;
// 获取全局定义的属性
function getGlobalProp(global) {
// 遍历次数,global属性名数组对应的索引,每遍历一次都会加1
var cnt = 0;
// 上一次的属性名
var lastProp;
var hasIframe = false;
// 遍历global对象
for (var p in global) {
if (shouldSkipProperty(global, p)) continue; // 遍历 iframe,检查 window 上的属性值是否是 iframe,是则跳过后面的 first 和 second 判断
// 遍历window.frames
for (var i = 0; i < window.frames.length && !hasIframe; i++) {
var frame = window.frames[i];
if (frame === global[p]) {
// 拥有iframe
hasIframe = true;
break;
}
}
// 支遍历两次?cnt = 0、cnt = 1?还是当前条件不成立,会一直遍历,cnt = Object.keys(global).length
if (!hasIframe && (cnt === 0 && p !== firstGlobalProp || cnt === 1 && p !== secondGlobalProp)) return p;
cnt++;
// 记录上一次的属性名
lastProp = p;
}
if (lastProp !== lastGlobalProp) return lastProp;
}
// 标记全局属性
function noteGlobalProps(global) {
// alternatively Object.keys(global).pop()
// but this may be faster (pending benchmarks)
firstGlobalProp = secondGlobalProp = undefined;
for (var p in global) {
if (shouldSkipProperty(global, p)) continue;
// 标记第一个、第二个全局属性
if (!firstGlobalProp) firstGlobalProp = p;else if (!secondGlobalProp) secondGlobalProp = p;
// 上一个全局属性
lastGlobalProp = p;
}
return lastGlobalProp;
}
// 获取内联代码
function getInlineCode(match) {
//
// 内联代码的开始位置
var start = match.indexOf(‘>’) + 1;
// 内联代码的结束位置,从后向前检索字符串
var end = match.lastIndexOf(‘<’);
// 不包含结束位置
return match.substring(start, end);
}
// 默认获取公共路径
function defaultGetPublicPath(entry) {
// 如果entry是对象类型,返回"/"
if ((0, _typeof2[“default”])(entry) === ‘object’) {
return ‘/’;
}
try {
var _URL = new URL(entry, location.href),
origin = _URL.origin,
pathname = _URL.pathname;
// 除去文件名x.js得到公共路径
var paths = pathname.split(‘/’); // 移除最后一个元素
paths.pop();
return "".concat(origin).concat(paths.join('/'), "/");
} catch (e) {
console.warn(e);
return ‘’;
}
} // Detect whether browser supports or not
// 是否支持模块脚本
function isModuleScriptSupported() {
var s = document.createElement(‘script’);
return ‘noModule’ in s;
} // 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);
};
exports.requestIdleCallback = requestIdleCallback;
function readResAsString(response, autoDetectCharset) {
// 未启用自动检测
if (!autoDetectCharset) {
return response.text();
} // 如果没headers,发生在test环境下的mock数据,为兼容原有测试用例
if (!response.headers) {
return response.text();
} // 如果没返回content-type,走默认逻辑
var contentType = response.headers.get(‘Content-Type’);
if (!contentType) {
return response.text();
} // 解析content-type内的charset
// Content-Type: text/html; charset=utf-8
// Content-Type: multipart/form-data; boundary=something
// GET请求下不会出现第二种content-type
var charset = ‘utf-8’;
var parts = contentType.split(‘;’);
if (parts.length === 2) {
var _parts 1 1 1split = parts[1].split(‘=’), // [charset, utf-8]
_parts 1 1 1split2 = (0, _slicedToArray2[“default”])(_parts 1 1 1split, 2), // [charset, utf-8]
value = _parts 1 1 1split2[1]; // utf-8
var encoding = value && value.trim(); // utf-8
if (encoding) {
charset = encoding;
}
} // 如果还是utf-8,那么走默认,兼容原有逻辑,这段代码删除也应该工作
if (charset.toUpperCase() === ‘UTF-8’) {
return response.text();
} // 走流读取,编码可能是gbk,gb2312等,比如sofa 3默认是gbk编码
return response.blob().then(function (file) {
return new Promise(function (resolve, reject) {
var reader = new window.FileReader();
reader.onload = function () {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsText(file, charset);
});
});
}
var evalCache = {};
// 执行代码
// js文件远程路径
// code js文件内容
function evalCode(scriptSrc, code) {
var key = scriptSrc;
if (!evalCache[key]) {
var functionWrappedCode = “window.TEMP_EVAL_FUNC = function(){”.concat(code, “}”);
(0, eval)(functionWrappedCode);
evalCache[key] = window.TEMP_EVAL_FUNC;
delete window.TEMP_EVAL_FUNC;
}
var evalFunc = evalCache[key];
evalFunc.call(window);
}