@open-wxa/wxa-runner-core 0.10.0 → 0.11.0

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.
@@ -8,11 +8,15 @@
8
8
  var MINI_PROGRAM_ERROR = /^(\(in promise\) )?MiniProgramError\n/;
9
9
  var MODULE_URL = /\bhttps?:\/\/[^/\s]+\/__wxa__\/module\/([^?\s)]+?)(?:\?[^:\s)]*)?:(\d+):(\d+)/;
10
10
  var ConsoleForwarder = class {
11
- /** @param pageOf 出错的页面路径,不知道时返回 undefined */
12
- constructor(mode, send, pageOf) {
11
+ /**
12
+ * @param pageOf 出错的页面路径,不知道时返回 undefined
13
+ * @param hintsOf 附在报错后面的提示,帮开发者认出 wxa-runner 和真机不同造成的错误
14
+ */
15
+ constructor(mode, send, pageOf, hintsOf = () => []) {
13
16
  this.mode = mode;
14
17
  this.send = send;
15
18
  this.pageOf = pageOf;
19
+ this.hintsOf = hintsOf;
16
20
  this.recentErrors = /* @__PURE__ */ new Map();
17
21
  }
18
22
  /** 运行时错误:全局 error、未处理的 Promise 拒绝、基础库捕获的业务代码错误 */
@@ -20,8 +24,10 @@
20
24
  const frames = businessFrames(stack);
21
25
  if (!this.mode || this.isRepeated(`${kind}:${webviewId}:${message}
22
26
  ${frames}`)) return;
27
+ const hints = this.hintsOf(kind).map((hint) => `
28
+ \u63D0\u793A\uFF1A${hint}`).join("");
23
29
  this.post("error", `${this.origin(kind, webviewId)}\u62A5\u9519\uFF1A${message}${frames ? `
24
- ${frames}` : ""}`);
30
+ ${frames}` : ""}${hints}`);
25
31
  }
26
32
  /** 运行时自己的提醒,如调用了未实现的 wx API */
27
33
  warn(text) {
@@ -1012,9 +1018,9 @@ ${frames}` : "");
1012
1018
  (this.replies.get(query.id) ?? Promise.resolve(null)).then((reply) => port.postMessage(reply));
1013
1019
  }
1014
1020
  };
1015
- function installMockMirror() {
1016
- if (navigator.webdriver || !navigator.serviceWorker) return null;
1017
- navigator.serviceWorker.register(SERVICE_WORKER_URL, { scope: "/" }).catch((error) => console.warn("[wxa-runner] Service Worker \u6CE8\u518C\u5931\u8D25\uFF0C\u88AB\u62E6\u622A\u7684\u8BF7\u6C42\u4E0D\u4F1A\u663E\u793A\u5728 Network \u9762\u677F\u91CC", error));
1021
+ function installMockMirror(externalServiceWorker = false) {
1022
+ if (!externalServiceWorker && navigator.webdriver || !navigator.serviceWorker) return null;
1023
+ if (!externalServiceWorker) navigator.serviceWorker.register(SERVICE_WORKER_URL, { scope: "/" }).catch((error) => console.warn("[wxa-runner] Service Worker \u6CE8\u518C\u5931\u8D25\uFF0C\u88AB\u62E6\u622A\u7684\u8BF7\u6C42\u4E0D\u4F1A\u663E\u793A\u5728 Network \u9762\u677F\u91CC", error));
1018
1024
  return new MockMirror(navigator.serviceWorker);
1019
1025
  }
1020
1026
 
@@ -2547,8 +2553,6 @@ html, body { margin: 0; height: 100%; background: radial-gradient(120% 80% at 50
2547
2553
  this.api = new ApiRegistry((context) => this.reportUnimplemented(context));
2548
2554
  /** setup 和测试 API 声明的请求拦截 */
2549
2555
  this.routes = new RequestRoutes();
2550
- /** 把被拦截的请求显示在 Network 面板里 */
2551
- this.mockMirror = installMockMirror();
2552
2556
  this.mapOperations = new MapOperations((webviewId, event, data) => this.publishToPages(event, data, [webviewId]));
2553
2557
  this.service = null;
2554
2558
  /** 新版基础库的业务子上下文;旧版基础库业务代码直接在逻辑层 iframe 里 */
@@ -2564,11 +2568,13 @@ html, body { margin: 0; height: 100%; background: radial-gradient(120% 80% at 50
2564
2568
  this.routeListeners = [];
2565
2569
  /** 渲染层报来的 2d、webgl 画布,按编号记下所在页面和最近的位置,供 placeCanvas 认出逻辑层的画布元素 */
2566
2570
  this.canvases = /* @__PURE__ */ new Map();
2571
+ this.mockMirror = installMockMirror(info.externalServiceWorker);
2567
2572
  this.device = info.device;
2568
2573
  this.forwarder = new ConsoleForwarder(
2569
2574
  info.forwardConsole ?? false,
2570
2575
  sendReport,
2571
- (kind, webviewId) => kind === "service" ? this.currentPage()?.path : this.pages.get(webviewId)?.path
2576
+ (kind, webviewId) => kind === "service" ? this.currentPage()?.path : this.pages.get(webviewId)?.path,
2577
+ (kind) => this.launchHints(kind)
2572
2578
  );
2573
2579
  this.shell = new PhoneShell(root, info.device);
2574
2580
  this.tabPaths = new Set((info.wxConfig.tabBar?.list || []).map((item) => stripHtml(item.pagePath)));
@@ -2719,6 +2725,13 @@ html, body { margin: 0; height: 100%; background: radial-gradient(120% 80% at 50
2719
2725
  this.errors.push({ kind: "service", webviewId: 0, api, message });
2720
2726
  this.forwarder.warn(message);
2721
2727
  }
2728
+ /**
2729
+ * 逻辑层在首页创建前出错时的提示:onLaunch、启动阶段的接口回调里读 getCurrentPages()[0] 是常见原因,
2730
+ * 而报错本身只说读不到某个属性。被拦截的请求立即返回,比真机更容易在首页创建前执行回调
2731
+ */
2732
+ launchHints(kind) {
2733
+ return kind === "service" && !this.currentPage() ? ["\u51FA\u9519\u65F6\u9996\u9875\u8FD8\u6CA1\u521B\u5EFA\uFF0CgetCurrentPages() \u8FD4\u56DE\u7684\u662F\u7A7A\u6570\u7EC4"] : [];
2734
+ }
2722
2735
  // ---------------------------------------------------------------------------
2723
2736
  // 路由
2724
2737
  // ---------------------------------------------------------------------------
@@ -3404,7 +3417,7 @@ ${script}
3404
3417
  const root = document.getElementById("wxa-root");
3405
3418
  const info = await (await fetch("/__wxa__/app-info.json")).json();
3406
3419
  const restore = new PageStackRestore(info.appid);
3407
- const sendReport = connectDevServer(restore);
3420
+ const sendReport = info.liveReload === false ? () => void 0 : connectDevServer(restore);
3408
3421
  if (info.buildError) {
3409
3422
  new PhoneShell(root, info.device).showError(`\u7F16\u8BD1\u5931\u8D25
3410
3423