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

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
@@ -935,13 +935,9 @@ async function execAppScripts(app) {
935
935
  }
936
936
  const appScriptList = Array.from(app.source.scripts.values()).filter(script => script.fromHtml && !script.initial);
937
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
- }
943
938
  // 保证同步脚本 和 module类型 最先执行
944
- // await Promise.all(commomList.map(script => script.excuteCode(app)));
939
+ await Promise.all(commomList.map(script => script.getCode(app)));
940
+ await Promise.all(commomList.map(script => script.excuteCode(app)));
945
941
  // 最后执行 defer 和 async 脚本
946
942
  const deferScriptList = [];
947
943
  const asyncScriptList = [];
@@ -1708,7 +1704,12 @@ const createProxyDocument = (rawDocument, app) => {
1708
1704
  return rawDocument.querySelector.call(this, selectors);
1709
1705
  }
1710
1706
  // 返回当前应用程序容器中匹配选择器的元素,如果没有匹配的元素则返回 null
1711
- return app?.container?.querySelector(selectors) ?? null;
1707
+ try {
1708
+ return app?.container?.querySelector(selectors) ?? null;
1709
+ }
1710
+ catch {
1711
+ return null;
1712
+ }
1712
1713
  }
1713
1714
  /**
1714
1715
  * 重写了 Document 类的 querySelectorAll 方法
@@ -1734,7 +1735,7 @@ const createProxyDocument = (rawDocument, app) => {
1734
1735
  }
1735
1736
  function getElementsByTagName(key) {
1736
1737
  if (SPECIAL_ELEMENT_TAG.includes(key) || (!app?.showSourceCode && key.toLocaleLowerCase() === 'script')) {
1737
- return rawDocument.getElementsByName(key);
1738
+ return rawDocument.getElementsByTagName(key);
1738
1739
  }
1739
1740
  return querySelectorAllNew(key);
1740
1741
  }
@@ -2585,9 +2586,10 @@ class EntrySource {
2585
2586
  const nonceStr = randomUrl();
2586
2587
  const scriptInstance = new Script({
2587
2588
  async: false,
2588
- code: script.textContent,
2589
+ code: script.textContent.replace(/var\s+([^=,]+)=/gm, `window.$1 = `),
2589
2590
  defer: script.type === 'module',
2590
2591
  fromHtml: !needReplaceELement,
2592
+ initial: true,
2591
2593
  isModule: script.type === 'module',
2592
2594
  url: nonceStr,
2593
2595
  });
@@ -2601,36 +2603,64 @@ class EntrySource {
2601
2603
  }
2602
2604
  return { replace: script };
2603
2605
  }
2604
- collectScriptAndStyle(parent, app) {
2605
- const children = Array.from(parent.children);
2606
- children.length &&
2607
- children.forEach(child => {
2608
- this.collectScriptAndStyle(child, app);
2609
- });
2610
- children?.forEach(dom => {
2611
- if (dom instanceof HTMLLinkElement) {
2612
- this.collectLink(dom, parent);
2613
- }
2614
- else if (dom instanceof HTMLStyleElement) {
2615
- if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2616
- this.styles.set(randomUrl(), new Style({
2617
- code: dom.textContent || '',
2618
- fromHtml: true,
2619
- url: '',
2620
- }));
2621
- dom.remove();
2622
- }
2623
- }
2624
- else if (dom instanceof HTMLScriptElement) {
2625
- this.collectScript(dom, parent);
2626
- }
2627
- else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2628
- parent.removeChild(dom);
2606
+ collectScriptAndStyle(parent) {
2607
+ const links = Array.from(parent.querySelectorAll('link'));
2608
+ links?.forEach(link => {
2609
+ this.collectLink(link, link.parentElement);
2610
+ });
2611
+ const styles = Array.from(parent.querySelectorAll('style'));
2612
+ styles?.forEach(style => {
2613
+ if (!style.hasAttribute('exclude') && !style.hasAttribute('ignore')) {
2614
+ this.styles.set(randomUrl(), new Style({
2615
+ code: style.textContent || '',
2616
+ fromHtml: true,
2617
+ url: '',
2618
+ }));
2619
+ style.remove();
2629
2620
  }
2630
- else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2631
- dom.setAttribute('src', fillUpPath(dom.getAttribute('src'), this.url));
2621
+ });
2622
+ const scripts = Array.from(parent.querySelectorAll('script'));
2623
+ scripts?.forEach(script => {
2624
+ this.collectScript(script, script.parentElement);
2625
+ });
2626
+ const metas = Array.from(parent.querySelectorAll('meta'));
2627
+ metas?.forEach(meta => {
2628
+ meta.parentElement.removeChild(meta);
2629
+ });
2630
+ const imgs = Array.from(parent.querySelectorAll('img'));
2631
+ imgs?.forEach(img => {
2632
+ if (img.hasAttribute('src')) {
2633
+ img.setAttribute('src', fillUpPath(img.getAttribute('src'), this.url));
2632
2634
  }
2633
2635
  });
2636
+ // const children = Array.from(parent.children);
2637
+ // children?.forEach(dom => {
2638
+ // if (dom instanceof HTMLLinkElement) {
2639
+ // this.collectLink(dom, parent);
2640
+ // } else if (dom instanceof HTMLStyleElement) {
2641
+ // if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2642
+ // this.styles.set(
2643
+ // randomUrl(),
2644
+ // new Style({
2645
+ // code: dom.textContent || '',
2646
+ // fromHtml: true,
2647
+ // url: '',
2648
+ // }),
2649
+ // );
2650
+ // dom.remove();
2651
+ // }
2652
+ // } else if (dom instanceof HTMLScriptElement) {
2653
+ // this.collectScript(dom, parent);
2654
+ // } else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2655
+ // parent.removeChild(dom);
2656
+ // } else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2657
+ // dom.setAttribute('src', fillUpPath(dom.getAttribute('src')!, this.url));
2658
+ // }
2659
+ // });
2660
+ // children.length &&
2661
+ // children.forEach(child => {
2662
+ // this.collectScriptAndStyle(child as HTMLElement, app);
2663
+ // });
2634
2664
  }
2635
2665
  getScript(url) {
2636
2666
  return this.scripts.get(url);
@@ -2668,7 +2698,7 @@ class EntrySource {
2668
2698
  if (wrapElement.__BK_WEWEB_APP_KEY__)
2669
2699
  delete wrapElement.__BK_WEWEB_APP_KEY__;
2670
2700
  wrapElement.innerHTML = htmlStr.replace(/<\/?head>/gim, '').replace(/<\/?body>/i, '');
2671
- this.collectScriptAndStyle(wrapElement, app);
2701
+ this.collectScriptAndStyle(wrapElement);
2672
2702
  await excuteAppStyles(app, wrapElement);
2673
2703
  this.html = wrapElement;
2674
2704
  }