Merge pull request #10901 from 2betop/feat-micro-app

feat: jssdk 支持 qiankun, wujie 微前端框架作为子应用加载
This commit is contained in:
hsm-lv 2024-09-18 10:49:11 +08:00 committed by GitHub
commit 931f4ae281
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 29 additions and 22 deletions

View File

@ -13,7 +13,10 @@
return;
}
var head = document.getElementsByTagName('head')[0];
// 无界微前端框架要在 iframe 中加载,否则不在同一个沙箱中
var head =
window.__WUJIE_RAW_DOCUMENT_HEAD__ ||
document.getElementsByTagName('head')[0];
var loadingMap = {};
var factoryMap = {};
var modulesMap = {};
@ -22,8 +25,6 @@
var pkgMap = {};
var createScripts = function (queues, onerror) {
var docFrag = document.createDocumentFragment();
for (var i = 0, len = queues.length; i < len; i++) {
var id = queues[i].id;
var url = queues[i].url;
@ -67,10 +68,10 @@
script.type = 'text/javascript';
script.src = url;
docFrag.appendChild(script);
// qiankun 微前端环境中只拦截了 append script 的情况,如果先插入一个 fragment 然后再 fragment 中添加 script 就不会被拦截
// 导致不在一个沙箱环境中
head.appendChild(script);
}
head.appendChild(docFrag);
};
var loadScripts = function (ids, callback, onerror) {
@ -308,6 +309,23 @@
require.timeout = 5000;
// 获取当前加载脚本的路径,从而推断 sdk 的目录,用于异步加载其他资源
// 仅用于 jssdk 场景
let host = '';
if (document.currentScript && document.currentScript.src) {
host = document.currentScript.src.replace(/\/[^\/]*$/, '');
} else {
try {
throw new Error();
} catch (e) {
host = (/((?:https?|file):.*?)\n/.test(e.stack) && RegExp.$1)?.replace(
/\/[^\/]*$/,
''
);
}
}
amis.sdkBasePath = host;
amis.require = require;
amis.define = define;
window.amis = amis;

View File

@ -656,14 +656,8 @@ if (fis.project.currentMedia() === 'publish-sdk') {
contents = contents.replace(
/function\sfilterUrl\(url\)\s\{\s*return\s*url;/m,
function () {
return `var _path = '';
try {
throw new Error()
} catch (e) {
_path = (/((?:https?|file):.*?)\\n/.test(e.stack) && RegExp.$1).replace(/\\/[^\\/]*$/, '');
}
function filterUrl(url) {
return _path + url.substring(1);`;
return `function filterUrl(url) {
return amis.sdkBasePath + url.substring(1);`;
}
);

View File

@ -126,17 +126,12 @@ module.exports = function (ret, pack, settings, opt) {
if (/_map\.js$/.test(file.subpath)) {
contents = `(function() {
var d = '';
try {
throw new Error()
} catch (e) {
d = (/((?:https?|file):.*?)\\n/.test(e.stack) && RegExp.$1).replace(/\\/[^\\/]*$/, '');
}
amis.host = d;
${contents.replace(
/\"url\"\s*\:\s*('|")(\.\/.*?)\1/g,
function (_, quote, value) {
return `"url": d + ${quote}${value.substring(1)}${quote}`;
return `"url": amis.sdkBasePath + ${quote}${value.substring(
1
)}${quote}`;
}
)}
})()`;