@open-wxa/wxa-runner-core 0.4.1 → 0.6.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.
package/dist/index.mjs CHANGED
@@ -190,8 +190,10 @@ var AppBuilder = class {
190
190
  ]);
191
191
  this.wxmlCache = wxml;
192
192
  this.wxssCache = wxss;
193
- const workersDir = typeof app.workers === "string" ? app.workers : app.workers?.path;
194
- const modules = this.project.listFiles([".js"], workersDir ? [workersDir] : []);
193
+ const workersDir = this.project.workersDir(app);
194
+ const inWorkers = (file) => workersDir !== null && file.startsWith(workersDir + "/");
195
+ const scripts = this.project.listFiles([".js"]);
196
+ const modules = scripts.filter((file) => !inWorkers(file));
195
197
  const serviceTemplates = this.createTemplates(app, units, unitJson, wxml.cut);
196
198
  const pageTemplates = this.createTemplates(app, units, unitJson, wxml.full);
197
199
  const pageStyles = createStyles(units, wxss);
@@ -205,6 +207,7 @@ var AppBuilder = class {
205
207
  subPackages: this.project.subPackages(app)
206
208
  }),
207
209
  modules,
210
+ workerModules: scripts.filter(inWorkers),
208
211
  serviceTemplates,
209
212
  serviceBootstrap: this.createServiceBootstrap(serviceTemplates, units, modules),
210
213
  pageFrame: `${pageTemplates}
@@ -226,7 +229,7 @@ ${pageStyles}`,
226
229
  const files = this.project.listFiles([".wxml", ".wxs"]).map((f) => "./" + f);
227
230
  const components = units.filter((id) => this.project.exists(id + ".wxml")).map((id) => {
228
231
  const json = unitJson.get(id);
229
- return { path: id, tags: Object.keys({ ...json.usingComponents, ...json.componentGenerics || {} }) };
232
+ return { path: id, tags: Object.keys({ ...json.usingComponents, ...json.componentGenerics }) };
230
233
  });
231
234
  const cwd = this.project.root;
232
235
  const [full, cut] = await Promise.all([
@@ -319,6 +322,11 @@ var MiniProgram = class {
319
322
  subPackages(app) {
320
323
  return app.subPackages || app.subpackages || [];
321
324
  }
325
+ /** Worker 代码所在目录,不带首尾的 /;没有配置 workers 时为 null */
326
+ workersDir(app) {
327
+ const dir = typeof app.workers === "string" ? app.workers : app.workers?.path;
328
+ return dir ? path.posix.normalize(dir).replace(/^\/+|\/+$/g, "") : null;
329
+ }
322
330
  /** 主包页面 + 分包页面(带 root 前缀) */
323
331
  allPages(app) {
324
332
  const pages = [...app.pages];
@@ -332,8 +340,8 @@ var MiniProgram = class {
332
340
  }
333
341
  /** 读取并预处理页面/组件 json,id 不带扩展名 */
334
342
  readUnitJson(id, app) {
335
- const json = { ...readJsonFile(path.join(this.root, id + ".json")) || {} };
336
- json.usingComponents = { ...json.usingComponents || {} };
343
+ const json = { ...readJsonFile(path.join(this.root, id + ".json")) };
344
+ json.usingComponents = { ...json.usingComponents };
337
345
  if (!id.startsWith("miniprogram_npm/") && !this.isInIndependentSubPackage(id, app)) {
338
346
  for (const [tag, ref] of Object.entries(app.usingComponents || {})) {
339
347
  if (json.usingComponents[tag]) continue;
@@ -400,15 +408,14 @@ var MiniProgram = class {
400
408
  return { pages, components };
401
409
  }
402
410
  /** 列出代码根目录下指定扩展名的文件,返回 posix 风格的相对路径 */
403
- listFiles(extensions, excludeDirs = []) {
411
+ listFiles(extensions) {
404
412
  const result = [];
405
- const excluded = new Set(excludeDirs.map((dir) => dir.replace(/^\/|\/$/g, "")));
406
413
  const walk = (dir, rel) => {
407
414
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
408
415
  if (entry.name.startsWith(".") || IGNORED_DIRS.has(entry.name)) continue;
409
416
  const childRel = rel ? `${rel}/${entry.name}` : entry.name;
410
417
  if (entry.isDirectory()) {
411
- if (!excluded.has(childRel)) walk(path.join(dir, entry.name), childRel);
418
+ walk(path.join(dir, entry.name), childRel);
412
419
  } else if (extensions.some((ext) => entry.name.endsWith(ext))) {
413
420
  if (!rel && PROJECT_FILES.has(entry.name)) continue;
414
421
  result.push(childRel);
@@ -529,6 +536,9 @@ function webviewHtml(build2, pagePath, webviewId, options) {
529
536
  <!-- \u771F\u673A\u7684\u6EDA\u52A8\u6761\u662F\u4E0D\u5360\u5E03\u5C40\u7684\u6D6E\u5C42\uFF1B\u684C\u9762\u6D4F\u89C8\u5668\u7684\u5E38\u89C4\u6EDA\u52A8\u6761\u548C\u81EA\u5B9A\u4E49 ::-webkit-scrollbar \u4F1A\u6324\u7A84\u9875\u9762\uFF0C\u4E00\u5F8B\u9690\u85CF\u3002
530
537
  scrollbar-width \u4F18\u5148\u4E8E ::-webkit-scrollbar\uFF0C\u9875\u9762\u6837\u5F0F\u4E0D\u4F1A\u518D\u628A\u6EDA\u52A8\u6761\u52A0\u56DE\u6765 -->
531
538
  <style>*{scrollbar-width:none}::-webkit-scrollbar{display:none}</style>
539
+ <!-- \u903B\u8F91\u5C42\u5EFA\u7684 2d\u3001webgl \u753B\u5E03\u642C\u8FDB wx-canvas \u540E\u94FA\u6EE1\u5B83\uFF08\u89C1 frame.ts \u7684 moveCanvasesIntoPages\uFF09\u3002
540
+ \u57FA\u7840\u5E93\u8FD8\u4F1A\u6309\u903B\u8F91\u5C42\u91CC\u7684\u5E03\u5C40\u6539\u5B83\u7684\u4F4D\u7F6E\u548C\u663E\u9690\uFF0C\u8FD9\u91CC\u4E00\u5F8B\u538B\u4F4F\uFF1B\u89E6\u6478\u7167\u5E38\u4EA4\u7ED9 wx-canvas \u81EA\u5DF1\u7684 canvas -->
541
+ <style>.wxa-service-canvas{position:absolute!important;left:0!important;top:0!important;width:100%!important;height:100%!important;visibility:inherit!important;pointer-events:none}</style>
532
542
  <script>var __wxConfig = ${toJs(wxConfig)};</script>
533
543
  <script src="/__wxa__/runtime/frame.js" data-kind="webview" data-webview-id="${id}"></script>
534
544
  <script src="/__baselib__/WAWebview.js"></script>
@@ -553,6 +563,22 @@ ${window.disableScroll ? "<style>body{overflow-y:hidden}</style>" : ""}
553
563
  </body>
554
564
  </html>`;
555
565
  }
566
+ function workerScript(build2, baseLib) {
567
+ const modules = build2.workerModules.map((id) => `/__wxa__/module/${encodeURI(id)}?v=${build2.version}`);
568
+ const prelude = `var __wxConfig = ${toJs(build2.wxConfig)};
569
+ importScripts('/__wxa__/runtime/worker.js');
570
+ `;
571
+ if (!baseLib.hasWorker) {
572
+ const message = `[wxa-runner] \u57FA\u7840\u5E93 ${baseLib.version} \u91CC\u6CA1\u6709 WAWorker.js\uFF0CWorker \u4E0D\u80FD\u8FD0\u884C\u3002\u6362\u7528\u5F00\u53D1\u8005\u5DE5\u5177\u4E0B\u8F7D\u7684\u57FA\u7840\u5E93\uFF0C\u6216\u5347\u7EA7 wxa-runner \u5E26\u4E0A\u65B0\u7684\u5185\u7F6E\u57FA\u7840\u5E93`;
573
+ return `${prelude}throw new Error(${toJs(message)});
574
+ `;
575
+ }
576
+ return `${prelude}var __wxaConsole = self.console;
577
+ importScripts('/__baselib__/WAWorker.js');
578
+ self.console = __wxaConsole;
579
+ importScripts(${modules.map((url) => toJs(url)).join(", ")});
580
+ `;
581
+ }
556
582
  function escapeHtml(text) {
557
583
  return text.replace(/[&<>"]/g, (ch) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[ch]);
558
584
  }
@@ -1094,12 +1120,16 @@ ${error.message}`);
1094
1120
  }));
1095
1121
  app.get(/^\/__wxa__\/module\/(.+\.js)$/, this.withBuild((build2, req, res) => {
1096
1122
  const id = decodeURIComponent(req.params[0]);
1097
- if (!build2.modules.includes(id)) {
1123
+ if (!build2.modules.includes(id) && !build2.workerModules.includes(id)) {
1098
1124
  res.status(404).type("js").send(`console.error(${JSON.stringify(`[wxa-runner] \u6A21\u5757 ${id} \u4E0D\u5B58\u5728`)})`);
1099
1125
  return;
1100
1126
  }
1101
1127
  res.type("js").send(this.builder.getModuleScript(id));
1102
1128
  }));
1129
+ app.get("/__wxa__/worker.js", this.withBuild((build2, _req, res) => {
1130
+ const hasWorker = fs4.existsSync(path4.join(this.baseLib.dir, "WAWorker.js"));
1131
+ res.type("js").send(workerScript(build2, { version: this.baseLib.version, hasWorker }));
1132
+ }));
1103
1133
  app.get("/__wxa__/service-bootstrap.js", this.withBuild((build2, _req, res) => {
1104
1134
  res.type("js").send(build2.serviceBootstrap);
1105
1135
  }));
@@ -1155,7 +1185,22 @@ ${error.message}`);
1155
1185
  res.status(404).end();
1156
1186
  return;
1157
1187
  }
1158
- res.type(path4.extname(req.params.name)).send(data);
1188
+ res.type(path4.extname(req.params.name));
1189
+ res.setHeader("ETag", `"${req.params.name}"`);
1190
+ res.setHeader("Accept-Ranges", "bytes");
1191
+ const ranges = req.range(data.length);
1192
+ if (ranges === -1) {
1193
+ res.status(416).setHeader("Content-Range", `bytes */${data.length}`);
1194
+ res.end();
1195
+ return;
1196
+ }
1197
+ if (Array.isArray(ranges) && ranges.type === "bytes" && ranges.length === 1) {
1198
+ const { start, end } = ranges[0];
1199
+ res.status(206).setHeader("Content-Range", `bytes ${start}-${end}/${data.length}`);
1200
+ res.send(data.subarray(start, end + 1));
1201
+ return;
1202
+ }
1203
+ res.send(data);
1159
1204
  });
1160
1205
  app.delete("/__wxa__/tmp/:name", (req, res) => {
1161
1206
  res.status(this.tempFiles.delete(req.params.name) ? 204 : 404).end();