@mpcore/simulator 0.6.0 → 0.6.1

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.d.mts CHANGED
@@ -1230,6 +1230,13 @@ export interface CreateArtifactProjectOptions {
1230
1230
  }
1231
1231
  export declare function createArtifactProject(options: CreateArtifactProjectOptions): HeadlessProjectDescriptor;
1232
1232
  //#endregion
1233
+ //#region src/project/hostConfig.d.ts
1234
+ /**
1235
+ * @description 根据构建后的 app.json 创建模拟器提供给小程序脚本读取的宿主配置。
1236
+ * 微信开发者工具的 tabBar 页面路径通常带有 `.html` 后缀,保留该形态以覆盖路由归一化边界。
1237
+ */
1238
+ export declare function createMiniProgramHostConfig(appConfig: Record<string, any>, override?: unknown): Record<string, any>;
1239
+ //#endregion
1233
1240
  //#region src/project/loadProject.d.ts
1234
1241
  export declare function loadProject(projectPath: string): HeadlessProjectDescriptor;
1235
1242
  //#endregion
@@ -1929,6 +1936,7 @@ export interface HeadlessSessionOptions {
1929
1936
  export declare class HeadlessSession {
1930
1937
  readonly project: HeadlessProjectDescriptor;
1931
1938
  private readonly kernel;
1939
+ private readonly startupNavigation;
1932
1940
  private appDefinition;
1933
1941
  private appInstance;
1934
1942
  private readonly appLifecycle;
@@ -2308,6 +2316,7 @@ export declare class BrowserHeadlessSession {
2308
2316
  private enterOptions;
2309
2317
  private launchOptions;
2310
2318
  private readonly kernel;
2319
+ private readonly startupNavigation;
2311
2320
  private readonly wxState;
2312
2321
  private pullDownRefreshState;
2313
2322
  constructor(options: BrowserHeadlessSessionOptions);
package/dist/index.mjs CHANGED
@@ -219,6 +219,57 @@ function createHeadlessLogManager(runtimeConsole) {
219
219
  };
220
220
  }
221
221
  //#endregion
222
+ //#region src/host/wx/startupNavigation.ts
223
+ const queues = /* @__PURE__ */ new WeakMap();
224
+ /** 冷启动先完成 App 与首屏装载,再顺序提交启动钩子请求的导航。 */
225
+ var StartupNavigationQueue = class {
226
+ scheduler;
227
+ collecting = false;
228
+ pending = [];
229
+ constructor(scheduler) {
230
+ this.scheduler = scheduler;
231
+ }
232
+ capture(operation) {
233
+ this.collecting = true;
234
+ try {
235
+ operation();
236
+ } catch (error) {
237
+ this.pending.length = 0;
238
+ throw error;
239
+ } finally {
240
+ this.collecting = false;
241
+ }
242
+ }
243
+ run(option, operation) {
244
+ if (!this.collecting) return operation(option);
245
+ const defer = (callback) => callback ? (...args) => {
246
+ this.scheduler.setTimeout(() => callback(...args), 0);
247
+ } : void 0;
248
+ const deferred = option && {
249
+ ...option,
250
+ success: defer(option.success),
251
+ fail: defer(option.fail),
252
+ complete: defer(option.complete)
253
+ };
254
+ this.pending.push(() => {
255
+ operation(deferred);
256
+ });
257
+ }
258
+ flush() {
259
+ const pending = this.pending.splice(0);
260
+ for (const operation of pending) operation();
261
+ }
262
+ };
263
+ /** 内部绑定保持 driver 的公开形状不变,两种 runtime 共享启动调度。 */
264
+ function bindStartupNavigation(driver, queue) {
265
+ queues.set(driver, queue);
266
+ return driver;
267
+ }
268
+ function runNavigationApi(driver, option, operation) {
269
+ const queue = queues.get(driver);
270
+ return queue ? queue.run(option, operation) : operation(option);
271
+ }
272
+ //#endregion
222
273
  //#region src/host/wx/index.ts
223
274
  function invokeWxApi(operation, option) {
224
275
  try {
@@ -616,12 +667,12 @@ function createHeadlessWx(driver, runtimeConsole = console) {
616
667
  hideLoading: (option) => invokeWxApi(() => driver.hideLoading(), option),
617
668
  hideToast: () => driver.hideToast(),
618
669
  downloadFile: (option) => driver.downloadFile(option),
619
- navigateBack: (option) => invokeWxApi(() => {
620
- driver.navigateBack(option);
621
- }, option),
622
- navigateTo: (option) => invokeWxApi(() => {
623
- driver.navigateTo(option);
624
- }, option),
670
+ navigateBack: (option) => runNavigationApi(driver, option, (deferred) => invokeWxApi(() => {
671
+ driver.navigateBack(deferred);
672
+ }, deferred)),
673
+ navigateTo: (option) => runNavigationApi(driver, option, (deferred) => invokeWxApi(() => {
674
+ driver.navigateTo(deferred);
675
+ }, deferred)),
625
676
  nextTick: (callback) => driver.nextTick(callback),
626
677
  offAppHide: (callback) => driver.offAppHide(callback),
627
678
  offAppShow: (callback) => driver.offAppShow(callback),
@@ -634,12 +685,12 @@ function createHeadlessWx(driver, runtimeConsole = console) {
634
685
  driver.pageScrollTo(option);
635
686
  }, option),
636
687
  previewImage: (option) => invokeWxApi(() => driver.previewImage(option), option),
637
- reLaunch: (option) => invokeWxApi(() => {
638
- driver.reLaunch(option);
639
- }, option),
640
- redirectTo: (option) => invokeWxApi(() => {
641
- driver.redirectTo(option);
642
- }, option),
688
+ reLaunch: (option) => runNavigationApi(driver, option, (deferred) => invokeWxApi(() => {
689
+ driver.reLaunch(deferred);
690
+ }, deferred)),
691
+ redirectTo: (option) => runNavigationApi(driver, option, (deferred) => invokeWxApi(() => {
692
+ driver.redirectTo(deferred);
693
+ }, deferred)),
643
694
  removeSavedFile: (option) => invokeWxApi(() => driver.removeSavedFile(option), option),
644
695
  saveImageToPhotosAlbum: (option) => invokeWxApi(() => driver.saveImageToPhotosAlbum(option), option),
645
696
  saveVideoToPhotosAlbum: (option) => invokeWxApi(() => driver.saveVideoToPhotosAlbum(option), option),
@@ -675,7 +726,7 @@ function createHeadlessWx(driver, runtimeConsole = console) {
675
726
  showToast: (option) => invokeWxApi(() => driver.showToast(option), option),
676
727
  startPullDownRefresh: (option) => invokeWxApi(() => driver.startPullDownRefresh(), option),
677
728
  stopPullDownRefresh: () => driver.stopPullDownRefresh(),
678
- switchTab: (option) => driver.switchTab(option),
729
+ switchTab: (option) => runNavigationApi(driver, option, (deferred) => driver.switchTab(deferred)),
679
730
  uploadFile: (option) => driver.uploadFile(option),
680
731
  removeTabBarBadge: (option) => invokeWxApi(() => driver.removeTabBarBadge(option), option),
681
732
  setTabBarBadge: (option) => invokeWxApi(() => driver.setTabBarBadge(option), option),
@@ -2961,6 +3012,40 @@ function invokePreparedNavigationApi(prepare, option) {
2961
3012
  }
2962
3013
  }
2963
3014
  //#endregion
3015
+ //#region src/project/hostConfig.ts
3016
+ /**
3017
+ * @description 根据构建后的 app.json 创建模拟器提供给小程序脚本读取的宿主配置。
3018
+ * 微信开发者工具的 tabBar 页面路径通常带有 `.html` 后缀,保留该形态以覆盖路由归一化边界。
3019
+ */
3020
+ function createMiniProgramHostConfig(appConfig, override) {
3021
+ const appTabBar = appConfig.tabBar && typeof appConfig.tabBar === "object" ? appConfig.tabBar : void 0;
3022
+ const tabBar = appTabBar ? {
3023
+ ...appTabBar,
3024
+ ...Array.isArray(appTabBar.list) ? { list: appTabBar.list.map((item) => {
3025
+ if (!item || typeof item !== "object") return item;
3026
+ const pagePath = typeof item.pagePath === "string" ? item.pagePath.replace(/^\/+/, "").replace(/\.html$/, "") : void 0;
3027
+ return pagePath ? {
3028
+ ...item,
3029
+ pagePath: `${pagePath}.html`
3030
+ } : { ...item };
3031
+ }) } : {}
3032
+ } : void 0;
3033
+ const generated = {
3034
+ ...appConfig,
3035
+ ...tabBar ? { tabBar } : {}
3036
+ };
3037
+ if (!override || typeof override !== "object" || Array.isArray(override)) return generated;
3038
+ const overrideRecord = override;
3039
+ return {
3040
+ ...generated,
3041
+ ...overrideRecord,
3042
+ ...generated.tabBar || overrideRecord.tabBar ? { tabBar: {
3043
+ ...generated.tabBar ?? {},
3044
+ ...overrideRecord.tabBar ?? {}
3045
+ } } : {}
3046
+ };
3047
+ }
3048
+ //#endregion
2964
3049
  //#region src/project/pageConfig.ts
2965
3050
  const DEFAULT_NAVIGATION_BAR_BACKGROUND_COLOR = "#ffffff";
2966
3051
  const DEFAULT_NAVIGATION_BAR_FRONT_COLOR = "#000000";
@@ -6279,6 +6364,7 @@ var BrowserHeadlessSession = class {
6279
6364
  enterOptions = createAppLaunchOptions("", {});
6280
6365
  launchOptions = createAppLaunchOptions("", {});
6281
6366
  kernel = new RuntimeKernel();
6367
+ startupNavigation = new StartupNavigationQueue(this.kernel.scheduler);
6282
6368
  wxState;
6283
6369
  pullDownRefreshState = {
6284
6370
  active: false,
@@ -6311,7 +6397,7 @@ var BrowserHeadlessSession = class {
6311
6397
  });
6312
6398
  });
6313
6399
  this.tabBarVisible = this.tabBarRoutes.size > 0;
6314
- this.moduleLoader = createBrowserModuleLoader(this.files, this.registries, () => this.pages.slice(), () => this.getApp(), {
6400
+ this.moduleLoader = createBrowserModuleLoader(this.files, this.registries, () => this.pages.slice(), () => this.getApp(), bindStartupNavigation({
6315
6401
  chooseImage: (option) => this.wxState.chooseImage(option ?? {}),
6316
6402
  chooseMessageFile: (option) => this.wxState.chooseMessageFile(option ?? {}),
6317
6403
  chooseMedia: (option) => this.wxState.chooseMedia(option ?? {}),
@@ -6390,8 +6476,11 @@ var BrowserHeadlessSession = class {
6390
6476
  removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
6391
6477
  setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
6392
6478
  updateShareMenu: (option) => this.wxState.updateShareMenu(option)
6393
- }, {
6394
- globals: options.globals,
6479
+ }, this.startupNavigation), {
6480
+ globals: {
6481
+ ...options.globals,
6482
+ __wxConfig: createMiniProgramHostConfig(this.project.appConfig, options.globals?.__wxConfig)
6483
+ },
6395
6484
  kernel: this.kernel,
6396
6485
  miniprogramRootPath: this.project.miniprogramRootPath,
6397
6486
  plugins: this.project.plugins
@@ -6867,9 +6956,16 @@ var BrowserHeadlessSession = class {
6867
6956
  return this.appInstance;
6868
6957
  }
6869
6958
  bootstrapNavigation(launchOptions = createAppLaunchOptions("", {})) {
6870
- const isLaunching = !this.appInstance;
6871
- this.bootstrap(launchOptions);
6872
- return isLaunching ? this.currentPageInstance : null;
6959
+ if (this.appInstance) return null;
6960
+ this.startupNavigation.capture(() => {
6961
+ this.bootstrap(launchOptions);
6962
+ });
6963
+ if (launchOptions.path) {
6964
+ const query = new URLSearchParams(launchOptions.query).toString();
6965
+ this.reLaunch(`/${launchOptions.path}${query ? `?${query}` : ""}`);
6966
+ }
6967
+ this.startupNavigation.flush();
6968
+ return this.currentPageInstance;
6873
6969
  }
6874
6970
  reLaunch(url) {
6875
6971
  const target = this.resolveNavigationTarget(url);
@@ -8277,6 +8373,7 @@ function resolveNavigationPath(targetPath, baseRoute) {
8277
8373
  var HeadlessSession = class {
8278
8374
  project;
8279
8375
  kernel = new RuntimeKernel();
8376
+ startupNavigation = new StartupNavigationQueue(this.kernel.scheduler);
8280
8377
  appDefinition = null;
8281
8378
  appInstance = null;
8282
8379
  appLifecycle = new HeadlessAppLifecycle();
@@ -8331,7 +8428,7 @@ var HeadlessSession = class {
8331
8428
  });
8332
8429
  });
8333
8430
  this.tabBarVisible = this.tabBarRoutes.size > 0;
8334
- this.moduleLoader = createModuleLoader(this.registries, () => this.pages.slice(), () => this.getApp(), {
8431
+ this.moduleLoader = createModuleLoader(this.registries, () => this.pages.slice(), () => this.getApp(), bindStartupNavigation({
8335
8432
  chooseImage: (option) => this.wxState.chooseImage(option ?? {}),
8336
8433
  chooseMessageFile: (option) => this.wxState.chooseMessageFile(option ?? {}),
8337
8434
  chooseMedia: (option) => this.wxState.chooseMedia(option ?? {}),
@@ -8410,9 +8507,12 @@ var HeadlessSession = class {
8410
8507
  removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
8411
8508
  setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
8412
8509
  updateShareMenu: (option) => this.wxState.updateShareMenu(option)
8413
- }, {
8510
+ }, this.startupNavigation), {
8414
8511
  artifactSource: this.project.artifactSource,
8415
- globals: options.globals,
8512
+ globals: {
8513
+ ...options.globals,
8514
+ __wxConfig: createMiniProgramHostConfig(this.project.appConfig, options.globals?.__wxConfig)
8515
+ },
8416
8516
  kernel: this.kernel,
8417
8517
  miniprogramRootPath: this.project.miniprogramRootPath,
8418
8518
  onConsole: (entry) => this.emit("console", entry),
@@ -8866,9 +8966,16 @@ var HeadlessSession = class {
8866
8966
  if (Object.hasOwn(runtimeApp, "routes")) this.appInstance.routes = runtimeApp.routes;
8867
8967
  }
8868
8968
  bootstrapNavigation(launchOptions = createAppLaunchOptions("", {})) {
8869
- const isLaunching = !this.appInstance;
8870
- this.bootstrap(launchOptions);
8871
- return isLaunching ? this.currentPageInstance : null;
8969
+ if (this.appInstance) return null;
8970
+ this.startupNavigation.capture(() => {
8971
+ this.bootstrap(launchOptions);
8972
+ });
8973
+ if (launchOptions.path) {
8974
+ const query = new URLSearchParams(launchOptions.query).toString();
8975
+ this.reLaunch(`/${launchOptions.path}${query ? `?${query}` : ""}`);
8976
+ }
8977
+ this.startupNavigation.flush();
8978
+ return this.currentPageInstance;
8872
8979
  }
8873
8980
  reLaunch(url) {
8874
8981
  const target = this.resolveNavigationTarget(url);
@@ -9933,4 +10040,4 @@ async function launch(options) {
9933
10040
  }
9934
10041
  }
9935
10042
  //#endregion
9936
- export { BrowserHeadlessSession, HEADLESS_PLUGIN_VIRTUAL_ROOT, HeadlessSession, HeadlessTestingNodeHandle, HeadlessTestingPageHandle, HeadlessTestingScopeHandle, HeadlessTestingSessionHandle, RuntimeDiagnostics, RuntimeKernel, RuntimeScheduler, cloneValue, coerceComponentPropertyValue, collectMiniProgramEventBindings, createAppInstance, createArtifactProject, createBrowserHeadlessSession, createBrowserModuleLoader, createBrowserProject, createBrowserVirtualFiles, createBrowserVirtualFilesFromDirectory, createComponentInstance, createFileSystemArtifactSource, createHeadlessSession, createHeadlessUniEventBus, createHeadlessWx, createHeadlessWxState, createHostRegistries, createLogicalNode, createMemoryArtifactSource, createOverlayArtifactSource, createPageInstance, createPluginArtifactSource, createPluginVirtualRoot, createProjectDescriptor, executeSelectorQueryRequests, getPageInstanceId, hasBrowserVirtualFile, hasComponentPropertyValueChanged, launch, loadProject, normalizeBrowserFilePath, normalizeComponentPageDefinition, normalizeComponentPropertyValue, querySelectorAll, readBrowserVirtualFile, readRenderedNodes, readRenderedSelectorNodes, registerAppDefinition, registerComponentDefinition, registerExportedComponentDefinition, registerPageDefinition, renderBrowserPageTree, renderPageTree, renderRuntimePageTree, resolveBrowserPageStyles, resolveMiniProgramEventBinding, resolvePluginRequest, resolveRoutesFromAppConfig, resolveSelectorQueryScopeRoot, resolveSelectorScrollTop, runComponentLifecycle, runComponentObservers, runComponentPageLifetime, waitForPageComponent, waitForPageComponents, waitForPageData, waitForPageSelector, waitForPageText, waitForPageTextGone, waitForRenderedPage, wechat };
10043
+ export { BrowserHeadlessSession, HEADLESS_PLUGIN_VIRTUAL_ROOT, HeadlessSession, HeadlessTestingNodeHandle, HeadlessTestingPageHandle, HeadlessTestingScopeHandle, HeadlessTestingSessionHandle, RuntimeDiagnostics, RuntimeKernel, RuntimeScheduler, cloneValue, coerceComponentPropertyValue, collectMiniProgramEventBindings, createAppInstance, createArtifactProject, createBrowserHeadlessSession, createBrowserModuleLoader, createBrowserProject, createBrowserVirtualFiles, createBrowserVirtualFilesFromDirectory, createComponentInstance, createFileSystemArtifactSource, createHeadlessSession, createHeadlessUniEventBus, createHeadlessWx, createHeadlessWxState, createHostRegistries, createLogicalNode, createMemoryArtifactSource, createMiniProgramHostConfig, createOverlayArtifactSource, createPageInstance, createPluginArtifactSource, createPluginVirtualRoot, createProjectDescriptor, executeSelectorQueryRequests, getPageInstanceId, hasBrowserVirtualFile, hasComponentPropertyValueChanged, launch, loadProject, normalizeBrowserFilePath, normalizeComponentPageDefinition, normalizeComponentPropertyValue, querySelectorAll, readBrowserVirtualFile, readRenderedNodes, readRenderedSelectorNodes, registerAppDefinition, registerComponentDefinition, registerExportedComponentDefinition, registerPageDefinition, renderBrowserPageTree, renderPageTree, renderRuntimePageTree, resolveBrowserPageStyles, resolveMiniProgramEventBinding, resolvePluginRequest, resolveRoutesFromAppConfig, resolveSelectorQueryScopeRoot, resolveSelectorScrollTop, runComponentLifecycle, runComponentObservers, runComponentPageLifetime, waitForPageComponent, waitForPageComponents, waitForPageData, waitForPageSelector, waitForPageText, waitForPageTextGone, waitForRenderedPage, wechat };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mpcore/simulator",
3
3
  "type": "module",
4
- "version": "0.6.0",
4
+ "version": "0.6.1",
5
5
  "description": "Headless mini-program runtime and testing kernel for the weapp-vite ecosystem",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -43,8 +43,8 @@
43
43
  "node": "^20.19.0 || >=22.12.0"
44
44
  },
45
45
  "dependencies": {
46
- "@babel/parser": "^8.0.5",
47
- "@weapp-core/constants": "0.2.4",
46
+ "@babel/parser": "^8.0.6",
47
+ "@weapp-core/constants": "0.2.5",
48
48
  "css-select": "^7.0.0",
49
49
  "css-what": "^8.0.0",
50
50
  "fontoxpath": "^3.34.0",