From 2b699bf7e6a74b16db5979a418ca5090677b4f85 Mon Sep 17 00:00:00 2001 From: liaoxuezhi <2698393+2betop@users.noreply.github.com> Date: Mon, 21 Oct 2024 20:28:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20sdk=20=E5=9C=A8?= =?UTF-8?q?=E5=8D=95=E9=A1=B5=E4=B8=AD=E5=AD=98=E5=9C=A8=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=97=B6=E6=B8=B2=E6=9F=93=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/mod.js | 37 ++++++++++++++++++++----------------- fis-conf.js | 2 +- scripts/embed-packager.js | 7 ++++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/mod.js b/examples/mod.js index f0a4318b4..2a21ff5db 100644 --- a/examples/mod.js +++ b/examples/mod.js @@ -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 = {}; @@ -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; diff --git a/fis-conf.js b/fis-conf.js index e8e82c337..16109d1a2 100644 --- a/fis-conf.js +++ b/fis-conf.js @@ -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);`; } ); diff --git a/scripts/embed-packager.js b/scripts/embed-packager.js index ac70b08cd..c5ea47fe8 100644 --- a/scripts/embed-packager.js +++ b/scripts/embed-packager.js @@ -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); @@ -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}`; } )} })()`;