@blueking/bk-weweb 0.0.2-5.beta.1 → 0.0.2-5.beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -37,6 +37,7 @@ var AppState;
37
37
  AppState["UNSET"] = "UNSET";
38
38
  })(AppState || (AppState = {}));
39
39
 
40
+ // 当前正在运行的app
40
41
  let currentRunningApp = null;
41
42
  function getCurrentRunningApp() {
42
43
  return currentRunningApp;
@@ -325,6 +326,11 @@ class Style {
325
326
  this.fromHtml = fromHtml;
326
327
  this.initial = initial ?? false;
327
328
  }
329
+ /**
330
+ * @param templateStyle 模板样式
331
+ * @param styleElement 样式node
332
+ * @param app 应用实例
333
+ */
328
334
  commonScoped(templateStyle, styleElement, app) {
329
335
  if (app.scopeCss && !(app.container instanceof ShadowRoot)) {
330
336
  const rules = Array.from(templateStyle.sheet?.cssRules ?? []);
@@ -361,6 +367,10 @@ class Style {
361
367
  delete styleElement.__BK_WEWEB_APP_KEY__;
362
368
  return styleElement;
363
369
  }
370
+ /**
371
+ * @param app 应用实例
372
+ * @returns 返回执行后的style标签
373
+ */
364
374
  async excuteCode(app) {
365
375
  app.registerRunningApp();
366
376
  let styleElement = this.createStyleElement();
@@ -577,21 +587,22 @@ async function excuteAppStyles(app, container) {
577
587
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
578
588
  * IN THE SOFTWARE.
579
589
  */
590
+ // bk-kweweb 微模块模式
580
591
  class MicroInstanceModel {
581
- state = AppState.UNSET;
582
- appCacheKey;
583
- container;
584
- data;
585
- initSource;
586
- isPreLoad = false;
587
- keepAlive;
588
- name;
589
- sandBox;
590
- scopeCss = true;
591
- scopeJs = false;
592
- showSourceCode = true;
593
- source;
594
- url;
592
+ state = AppState.UNSET; // 当前实例状态
593
+ appCacheKey; // 缓存key
594
+ container; // 容器
595
+ data; // 数据
596
+ initSource; // 初始资源
597
+ isPreLoad = false; // 是否预加载
598
+ keepAlive; // 是否缓存
599
+ name; // 名称
600
+ sandBox; // 沙箱
601
+ scopeCss = true; // 是否隔离样式
602
+ scopeJs = false; // 是否隔离js
603
+ showSourceCode = true; // 是否显示源码
604
+ source; // 入口资源
605
+ url; // url
595
606
  constructor(props) {
596
607
  this.name = props.id !== props.url ? props.id : random(5);
597
608
  this.appCacheKey = props.id || this.name;
@@ -673,12 +684,6 @@ class MicroInstanceModel {
673
684
  await this.source.importEntery(this);
674
685
  }
675
686
  }
676
- set status(v) {
677
- this.state = v;
678
- }
679
- get status() {
680
- return this.state;
681
- }
682
687
  unmount(needDestroy) {
683
688
  this.state = AppState.UNMOUNT;
684
689
  this.sandBox?.deactivated();
@@ -686,6 +691,12 @@ class MicroInstanceModel {
686
691
  this.container.innerHTML = '';
687
692
  this.container = undefined;
688
693
  }
694
+ set status(v) {
695
+ this.state = v;
696
+ }
697
+ get status() {
698
+ return this.state;
699
+ }
689
700
  }
690
701
 
691
702
  /*
@@ -740,6 +751,11 @@ class Script {
740
751
  this.fromHtml = fromHtml ?? false;
741
752
  this.initial = initial ?? false;
742
753
  }
754
+ /**
755
+ * @param app 应用
756
+ * @param needRelaceScriptElement 是否需要替换script标签
757
+ * @returns 返回执行后的script标签或注释
758
+ */
743
759
  async excuteCode(app, needRelaceScriptElement = false) {
744
760
  try {
745
761
  if (!this.code)
@@ -785,6 +801,7 @@ class Script {
785
801
  }
786
802
  return;
787
803
  }
804
+ // 内存脚本执行
788
805
  executeMemoryScript(app, scopedCode) {
789
806
  try {
790
807
  const isScopedLocation = app instanceof MicroAppModel && app.scopeLocation;
@@ -796,6 +813,7 @@ class Script {
796
813
  console.error(e);
797
814
  }
798
815
  }
816
+ // 脚本标签执行
799
817
  executeSourceScript(scriptElement, scopedCode) {
800
818
  if (this.isModule) {
801
819
  if (this.url?.match(/\.ts$/)) {
@@ -812,6 +830,7 @@ class Script {
812
830
  }
813
831
  this.url && scriptElement.setAttribute('origin-src', this.url);
814
832
  }
833
+ // 获取脚本内容
815
834
  async getCode(app) {
816
835
  if (this.code.length || !this.url) {
817
836
  return this.code;
@@ -835,6 +854,7 @@ class Script {
835
854
  setCode(code) {
836
855
  this.code = code;
837
856
  }
857
+ // 转换脚本内容
838
858
  transformCode(app) {
839
859
  if (app.sandBox) {
840
860
  if (app.showSourceCode || this.isModule) {
@@ -915,8 +935,13 @@ async function execAppScripts(app) {
915
935
  }
916
936
  const appScriptList = Array.from(app.source.scripts.values()).filter(script => script.fromHtml && !script.initial);
917
937
  const commomList = appScriptList.filter(script => (!script.async && !script.defer) || script.isModule);
938
+ let i = 0;
939
+ while (i < commomList.length && commomList[i]) {
940
+ await commomList[i].excuteCode(app);
941
+ i++;
942
+ }
918
943
  // 保证同步脚本 和 module类型 最先执行
919
- await Promise.all(commomList.map(script => script.excuteCode(app)));
944
+ // await Promise.all(commomList.map(script => script.excuteCode(app)));
920
945
  // 最后执行 defer 和 async 脚本
921
946
  const deferScriptList = [];
922
947
  const asyncScriptList = [];
@@ -1231,6 +1256,7 @@ function resetBodyAndHeaderMethods() {
1231
1256
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1232
1257
  * IN THE SOFTWARE.
1233
1258
  */
1259
+ // before load
1234
1260
  function beforeLoad() {
1235
1261
  rewriteBodyAndHeaderMethods();
1236
1262
  }
@@ -1260,6 +1286,7 @@ function beforeLoad() {
1260
1286
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1261
1287
  * IN THE SOFTWARE.
1262
1288
  */
1289
+ // 激活应用
1263
1290
  function activated(appKey, container, callback) {
1264
1291
  const app = appCache.getApp(appKey);
1265
1292
  if (app?.status === AppState.DEACTIVATED && app.keepAlive) {
@@ -1302,6 +1329,7 @@ function activated(appKey, container, callback) {
1302
1329
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1303
1330
  * IN THE SOFTWARE.
1304
1331
  */
1332
+ // deactivated
1305
1333
  function deactivated(appKey) {
1306
1334
  const app = appCache.getApp(appKey);
1307
1335
  if (app && [AppState.ACTIVATED, AppState.MOUNTED].includes(app.status)) {
@@ -1403,6 +1431,7 @@ function loadInstance(props) {
1403
1431
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1404
1432
  * IN THE SOFTWARE.
1405
1433
  */
1434
+ // unmount
1406
1435
  function unmount(appKey) {
1407
1436
  const app = appCache.getApp(appKey);
1408
1437
  if (app && app.status !== AppState.UNMOUNT) {
@@ -2198,23 +2227,24 @@ class SandBox {
2198
2227
  * IN THE SOFTWARE.
2199
2228
  */
2200
2229
  const BLANK_ORIGN = 'about:blank';
2230
+ // bk-weweb 微应用模式
2201
2231
  class MicroAppModel {
2202
- state = AppState.UNSET;
2203
- container;
2204
- data;
2205
- iframe = null;
2206
- initSource;
2207
- isPreLoad = false;
2208
- keepAlive;
2232
+ state = AppState.UNSET; // 状态
2233
+ container; // 容器
2234
+ data; // 数据
2235
+ iframe = null; // scoped iframe
2236
+ initSource; // 初始资源
2237
+ isPreLoad = false; // 是否预加载
2238
+ keepAlive; // 是否缓存
2209
2239
  mode = WewebMode.APP;
2210
- name;
2211
- sandBox;
2212
- scopeCss;
2213
- scopeJs;
2214
- scopeLocation;
2215
- showSourceCode;
2216
- source;
2217
- url;
2240
+ name; // 名称
2241
+ sandBox; // 沙箱
2242
+ scopeCss; // 是否隔离样式
2243
+ scopeJs; // 是否隔离js
2244
+ scopeLocation; // 是否隔离location
2245
+ showSourceCode; // 是否显示源码
2246
+ source; // 入口资源
2247
+ url; // url
2218
2248
  constructor(props) {
2219
2249
  this.name = props.id !== props.url ? props.id : random(5);
2220
2250
  this.mode = props.mode ?? WewebMode.APP;
@@ -2235,6 +2265,7 @@ class MicroAppModel {
2235
2265
  this.container.setAttribute(CSS_ATTRIBUTE_KEY, this.name);
2236
2266
  }
2237
2267
  }
2268
+ // 激活
2238
2269
  activated(container, callback) {
2239
2270
  this.isPreLoad = false;
2240
2271
  this.state = AppState.ACTIVATED;
@@ -2252,6 +2283,7 @@ class MicroAppModel {
2252
2283
  callback?.(this);
2253
2284
  }
2254
2285
  }
2286
+ // 创建隔离iframe
2255
2287
  createIframe() {
2256
2288
  return new Promise(resolve => {
2257
2289
  const iframe = document.createElement('iframe');
@@ -2699,11 +2731,12 @@ class AppCache {
2699
2731
  constructor() {
2700
2732
  this.cache = new Map();
2701
2733
  this.baseSource = new EntrySource(location.href);
2702
- // this.baseApp = new
2703
2734
  }
2735
+ // 删除缓存
2704
2736
  deleteApp(url) {
2705
2737
  this.cache.delete(url);
2706
2738
  }
2739
+ // 获取缓存app
2707
2740
  getApp(name) {
2708
2741
  if (!name)
2709
2742
  return undefined;
@@ -2712,6 +2745,7 @@ class AppCache {
2712
2745
  return app;
2713
2746
  return Array.from(this.cache.values()).find((item) => item.name === name);
2714
2747
  }
2748
+ // 获取缓存app
2715
2749
  getBaseAppStyle(urlOrCode) {
2716
2750
  return this.baseSource.getStyle(urlOrCode);
2717
2751
  }
@@ -2790,6 +2824,12 @@ window.__getAppOrInstance__ = function (id) {
2790
2824
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2791
2825
  * IN THE SOFTWARE.
2792
2826
  */
2827
+ /**
2828
+ * @param url 资源地址
2829
+ * @param style 样式实例
2830
+ * @param originLink 原始link标签
2831
+ * @returns 返回替换的style标签
2832
+ */
2793
2833
  function getStyleSource(url, style, originLink) {
2794
2834
  const replaceStyle = document.createElement('style');
2795
2835
  setMarkElement(replaceStyle);
@@ -2806,6 +2846,12 @@ function getStyleSource(url, style, originLink) {
2806
2846
  });
2807
2847
  return replaceStyle;
2808
2848
  }
2849
+ /**
2850
+ * @param url 资源地址
2851
+ * @param script 脚本实例
2852
+ * @param originScript 原始script标签
2853
+ * @returns 返回替换的script标签
2854
+ */
2809
2855
  function getScriptSource(url, script, originScript) {
2810
2856
  const replaceScript = document.createElement('script');
2811
2857
  setMarkElement(replaceScript);
@@ -2829,6 +2875,10 @@ function getScriptSource(url, script, originScript) {
2829
2875
  });
2830
2876
  return replaceScript;
2831
2877
  }
2878
+ /**
2879
+ * @param child link或者script标签
2880
+ * @returns 返回替换的link或者script标签
2881
+ */
2832
2882
  function createNewNode(child) {
2833
2883
  if (child instanceof HTMLLinkElement) {
2834
2884
  const rel = child.getAttribute('rel');
@@ -2863,9 +2913,20 @@ function createNewNode(child) {
2863
2913
  }
2864
2914
  return child;
2865
2915
  }
2916
+ /**
2917
+ * @param node 节点
2918
+ * @returns 返回是否是link或者script标签
2919
+ */
2866
2920
  function isLinkOrScript(node) {
2867
2921
  return node instanceof HTMLLinkElement || node instanceof HTMLScriptElement;
2868
2922
  }
2923
+ /**
2924
+ * @param parent 父节点
2925
+ * @param newChild 新节点
2926
+ * @param passiveChild 被动节点
2927
+ * @param rawMethod 原始方法
2928
+ * @returns 返回原始方法的执行结果
2929
+ */
2869
2930
  function baseElementInertHandle(parent, newChild, passiveChild, rawMethod) {
2870
2931
  if (isLinkOrScript(newChild)) {
2871
2932
  const targetChild = createNewNode(newChild);
@@ -2873,6 +2934,12 @@ function baseElementInertHandle(parent, newChild, passiveChild, rawMethod) {
2873
2934
  }
2874
2935
  return rawMethod.call(parent, newChild, passiveChild);
2875
2936
  }
2937
+ /**
2938
+ * @param parent 父节点
2939
+ * @param newChild 新节点
2940
+ * @param rawMethod 原始方法
2941
+ * @returns 返回原始方法的执行结果
2942
+ */
2876
2943
  function baseElementAppendHandle(parent, newChild, rawMethod) {
2877
2944
  if (isLinkOrScript(newChild)) {
2878
2945
  const targetChild = createNewNode(newChild);
@@ -2906,6 +2973,9 @@ function baseElementAppendHandle(parent, newChild, rawMethod) {
2906
2973
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2907
2974
  * IN THE SOFTWARE.
2908
2975
  */
2976
+ /**
2977
+ * 收集主应用的资源
2978
+ */
2909
2979
  function collectBaseSource() {
2910
2980
  const rawBodyAppendChild = HTMLBodyElement.prototype.appendChild;
2911
2981
  const rawHeadAppendChild = HTMLHeadElement.prototype.appendChild;
@@ -2957,6 +3027,7 @@ function collectBaseSource() {
2957
3027
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2958
3028
  * IN THE SOFTWARE.
2959
3029
  */
3030
+ // mount
2960
3031
  function mount(appKey, container, callback) {
2961
3032
  const app = appCache.getApp(appKey);
2962
3033
  app &&
@@ -2991,6 +3062,7 @@ function mount(appKey, container, callback) {
2991
3062
  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2992
3063
  * IN THE SOFTWARE.
2993
3064
  */
3065
+ // unload
2994
3066
  function unload(url) {
2995
3067
  appCache.deleteApp(url);
2996
3068
  }