Skip to content

Commit

Permalink
fix: 修复 sdk 在单页中存在多个版本时渲染异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Oct 21, 2024
1 parent 9597f79 commit 570e31e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
37 changes: 20 additions & 17 deletions examples/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@
(function (global) {
var require, define;
var amis = window.amis || {};

// 获取当前加载脚本的路径,从而推断 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['sdk@@versionBasePath'] = host;

// 避免重复加载而导致已定义模块丢失
if (amis.require) {
return;
}

amis.sdkBasePath = host;

var head = document.getElementsByTagName('head')[0];
var loadingMap = {};
var factoryMap = {};
Expand Down Expand Up @@ -306,23 +326,6 @@

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;
Expand Down
2 changes: 1 addition & 1 deletion fis-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ if (fis.project.currentMedia() === 'publish-sdk') {
/function\sfilterUrl\(url\)\s\{\s*return\s*url;/m,
function () {
return `function filterUrl(url) {
return amis.sdkBasePath + url.substring(1);`;
return amis['sdk@${package.version}BasePath'] + url.substring(1);`;
}
);

Expand Down
7 changes: 4 additions & 3 deletions scripts/embed-packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var rSourceMap =
/(?:\/\/\#\s*sourceMappingURL[^\r\n\'\"]*|\/\*\#\s*sourceMappingURL[^\r\n\'\"]*\*\/)(?:\r?\n|$)/gi;
var caches = {};
var createResource = fis.require('postpackager-loader/lib/resource.js');
const package = require('../packages/amis/package.json');

function prefixCss(code, prefix) {
var cssAst = css.parse(code);
Expand Down Expand Up @@ -129,9 +130,9 @@ module.exports = function (ret, pack, settings, opt) {
${contents.replace(
/\"url\"\s*\:\s*('|")(\.\/.*?)\1/g,
function (_, quote, value) {
return `"url": amis.sdkBasePath + ${quote}${value.substring(
1
)}${quote}`;
return `"url": amis['sdk@${
package.version
}BasePath'] + ${quote}${value.substring(1)}${quote}`;
}
)}
})()`;
Expand Down

0 comments on commit 570e31e

Please sign in to comment.