@mpcore/simulator 0.5.0 → 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/README.md +20 -0
- package/dist/index.d.mts +47 -15
- package/dist/index.mjs +321 -123
- package/package.json +12 -6
package/README.md
CHANGED
|
@@ -24,3 +24,23 @@
|
|
|
24
24
|
浏览器会话的 `renderCurrentPage().styles` 提供当前页面的 `cssText`、项目相对 `dependencies` 和 `appWxssEnabled`,Web demo 将该结果挂载到页面预览的 Shadow DOM。解析器读取 `app.wxss`、页面 `.wxss` 及递归本地 `@import`,保留导入位置和全局在前、页面在后的层叠顺序;页面 `.wxss` 内联的全局样式快照也会动态加载,并保留其后页面自有样式的覆盖优先级。文件修改后重新渲染会读取最新内容,包括只更新样式的场景,不重建页面或 App 实例。
|
|
25
25
|
|
|
26
26
|
此入口只负责页面样式依赖和 CSS 兼容声明,不实现嵌套组件的完整样式隔离、类名前缀转换或 `rpx` 布局换算。Component 页面的 `page-isolated`、`page-strong-isolated`、`page-apply-shared`、`page-shared` 禁用隐式 `app.wxss`,页面仍可显式导入样式;最终页面 JSON 的 `styleIsolation` 优先于 JS 定义,JSON 未提供该值时才保留 JS 选项。普通 `Page` 注册不应用组件隔离选项,普通 `isolated` 也不等同于 `page-isolated`。缺失依赖、循环导入、非法 CSS 和不支持的导入会抛出明确错误。支持带媒体条件的本地导入;远程导入及 `layer` / `supports` 导入条件不受支持,也不会触发外部请求。
|
|
27
|
+
|
|
28
|
+
## 分块请求 mock
|
|
29
|
+
|
|
30
|
+
`session.mockRequest()` 支持为 `enableChunked: true` 的请求声明分块,默认仍不访问网络:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
session.mockRequest({
|
|
34
|
+
url: 'https://example.test/data',
|
|
35
|
+
response: '',
|
|
36
|
+
delay: 10,
|
|
37
|
+
chunks: [
|
|
38
|
+
{ data: new Uint8Array([65]).buffer, delay: 10 },
|
|
39
|
+
{ data: new Uint8Array([66]).buffer, delay: 10 },
|
|
40
|
+
],
|
|
41
|
+
})
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
顶层 `delay` 控制响应头阶段,每个分块的 `delay` 相对于前一个阶段。请求提供 `onHeadersReceived` / `offHeadersReceived`、`onChunkReceived` / `offChunkReceived`;不传回调的 off 方法移除全部对应监听。取消会停止后续分块并仅触发一次 `fail → complete`。可设置 `error` 模拟已发送分块后的连接失败。
|
|
45
|
+
|
|
46
|
+
分块请求的 `success.data` 按已验证的微信 DevTools 行为返回空字符串;数据只通过 chunk 交付。未设置 `chunks` 时不会将普通 `response` 自动拆分。自定义 driver 仍可只实现 abort,公开监听方法类型为可选,使用前需检测能力。此模型不提供 Streams 或传输反压保证。
|
package/dist/index.d.mts
CHANGED
|
@@ -641,7 +641,10 @@ export interface HeadlessWxRequestSuccessResult {
|
|
|
641
641
|
header: Record<string, string>;
|
|
642
642
|
statusCode: number;
|
|
643
643
|
}
|
|
644
|
-
export interface HeadlessWxRequestOption extends HeadlessWxCallbackOption<HeadlessWxRequestSuccessResult> {
|
|
644
|
+
export interface HeadlessWxRequestOption extends Omit<HeadlessWxCallbackOption<HeadlessWxRequestSuccessResult>, 'complete'> {
|
|
645
|
+
complete?: (result?: HeadlessWxRequestSuccessResult | Error) => void;
|
|
646
|
+
enableChunked?: boolean;
|
|
647
|
+
responseType?: 'text' | 'arraybuffer';
|
|
645
648
|
data?: unknown;
|
|
646
649
|
header?: Record<string, string>;
|
|
647
650
|
method?: string;
|
|
@@ -650,6 +653,21 @@ export interface HeadlessWxRequestOption extends HeadlessWxCallbackOption<Headle
|
|
|
650
653
|
export interface HeadlessWxRequestTask {
|
|
651
654
|
abort: () => void;
|
|
652
655
|
}
|
|
656
|
+
export interface HeadlessWxRequestHeadersResult {
|
|
657
|
+
cookies: string[];
|
|
658
|
+
header: Record<string, string>;
|
|
659
|
+
statusCode: number;
|
|
660
|
+
}
|
|
661
|
+
export interface HeadlessWxRequestChunkResult {
|
|
662
|
+
data: ArrayBuffer;
|
|
663
|
+
}
|
|
664
|
+
export interface HeadlessWxNetworkRequestTask extends HeadlessWxRequestTask {
|
|
665
|
+
/** 自定义传输可仅实现 abort,使用监听前应检测能力。 */
|
|
666
|
+
onHeadersReceived?: (callback: (result: HeadlessWxRequestHeadersResult) => void) => void;
|
|
667
|
+
offHeadersReceived?: (callback?: (result: HeadlessWxRequestHeadersResult) => void) => void;
|
|
668
|
+
onChunkReceived?: (callback: (result: HeadlessWxRequestChunkResult) => void) => void;
|
|
669
|
+
offChunkReceived?: (callback?: (result: HeadlessWxRequestChunkResult) => void) => void;
|
|
670
|
+
}
|
|
653
671
|
export interface HeadlessWxDownloadFileSuccessResult {
|
|
654
672
|
errMsg: string;
|
|
655
673
|
statusCode: number;
|
|
@@ -934,7 +952,7 @@ export interface HeadlessWxDriver extends HeadlessWxDriverCapabilities {
|
|
|
934
952
|
removeSavedFile: (option: HeadlessWxRemoveSavedFileOption) => {
|
|
935
953
|
errMsg: string;
|
|
936
954
|
};
|
|
937
|
-
request: (option: HeadlessWxRequestOption) =>
|
|
955
|
+
request: (option: HeadlessWxRequestOption) => HeadlessWxNetworkRequestTask;
|
|
938
956
|
saveFile: (option: HeadlessWxSaveFileOption) => HeadlessWxSaveFileSuccessResult;
|
|
939
957
|
saveImageToPhotosAlbum: (option: HeadlessWxSaveImageToPhotosAlbumOption) => HeadlessWxSaveImageToPhotosAlbumResult;
|
|
940
958
|
saveVideoToPhotosAlbum: (option: HeadlessWxSaveVideoToPhotosAlbumOption) => HeadlessWxSaveVideoToPhotosAlbumResult;
|
|
@@ -1011,7 +1029,7 @@ export interface HeadlessWx extends HeadlessUniEventBus {
|
|
|
1011
1029
|
removeStorage: (option: HeadlessWxRemoveStorageOption) => HeadlessWxStorageResult | undefined;
|
|
1012
1030
|
removeStorageSync: (key: string) => void;
|
|
1013
1031
|
rpx2px: (value: number) => number;
|
|
1014
|
-
request: (option: HeadlessWxRequestOption) =>
|
|
1032
|
+
request: (option: HeadlessWxRequestOption) => HeadlessWxNetworkRequestTask;
|
|
1015
1033
|
saveFile: (option: HeadlessWxSaveFileOption) => HeadlessWxSaveFileSuccessResult | undefined;
|
|
1016
1034
|
setBackgroundColor: (option: HeadlessWxSetBackgroundColorOption) => {
|
|
1017
1035
|
errMsg: string;
|
|
@@ -1380,6 +1398,7 @@ interface DomNodeLike$5 {
|
|
|
1380
1398
|
attribs?: Record<string, string>;
|
|
1381
1399
|
children?: DomNodeLike$5[];
|
|
1382
1400
|
data?: string;
|
|
1401
|
+
dataset?: Record<string, unknown>;
|
|
1383
1402
|
name?: string;
|
|
1384
1403
|
parent?: DomNodeLike$5 | null;
|
|
1385
1404
|
type?: string;
|
|
@@ -1389,7 +1408,7 @@ interface BrowserRenderScope {
|
|
|
1389
1408
|
alias?: string;
|
|
1390
1409
|
classList?: string[];
|
|
1391
1410
|
data: Record<string, any>;
|
|
1392
|
-
dataset?: Record<string,
|
|
1411
|
+
dataset?: Record<string, unknown>;
|
|
1393
1412
|
eventBindings?: Map<string, {
|
|
1394
1413
|
method: string;
|
|
1395
1414
|
stopAfter: boolean;
|
|
@@ -1457,6 +1476,7 @@ interface DomNodeLike$4 {
|
|
|
1457
1476
|
attribs?: Record<string, string>;
|
|
1458
1477
|
children?: DomNodeLike$4[];
|
|
1459
1478
|
data?: string;
|
|
1479
|
+
dataset?: Record<string, unknown>;
|
|
1460
1480
|
name?: string;
|
|
1461
1481
|
parent?: DomNodeLike$4 | null;
|
|
1462
1482
|
type?: string;
|
|
@@ -1466,7 +1486,7 @@ interface RuntimeRenderScope {
|
|
|
1466
1486
|
alias?: string;
|
|
1467
1487
|
classList?: string[];
|
|
1468
1488
|
data: Record<string, any>;
|
|
1469
|
-
dataset?: Record<string,
|
|
1489
|
+
dataset?: Record<string, unknown>;
|
|
1470
1490
|
eventBindings?: Map<string, {
|
|
1471
1491
|
method: string;
|
|
1472
1492
|
stopAfter: boolean;
|
|
@@ -1567,6 +1587,13 @@ export interface HeadlessWxModalLogEntry {
|
|
|
1567
1587
|
title: string;
|
|
1568
1588
|
}
|
|
1569
1589
|
export interface HeadlessWxRequestMockDefinition {
|
|
1590
|
+
/** enableChunked 请求的显式分块,每个 delay 相对于前一个阶段。 */
|
|
1591
|
+
chunks?: Array<{
|
|
1592
|
+
data: ArrayBuffer;
|
|
1593
|
+
delay?: number;
|
|
1594
|
+
}>;
|
|
1595
|
+
/** 分块发送完成后触发的模拟错误。 */
|
|
1596
|
+
error?: string;
|
|
1570
1597
|
delay?: number;
|
|
1571
1598
|
header?: Record<string, string>;
|
|
1572
1599
|
method?: string;
|
|
@@ -1849,7 +1876,7 @@ export declare function createHeadlessWxState(scheduler?: Pick<RuntimeScheduler,
|
|
|
1849
1876
|
errMsg: string;
|
|
1850
1877
|
};
|
|
1851
1878
|
removeStorageSync(key: string): void;
|
|
1852
|
-
request(option: HeadlessWxRequestOption):
|
|
1879
|
+
request(option: HeadlessWxRequestOption): HeadlessWxNetworkRequestTask;
|
|
1853
1880
|
removeSavedFile(option: HeadlessWxRemoveSavedFileOption): {
|
|
1854
1881
|
errMsg: string;
|
|
1855
1882
|
};
|
|
@@ -2194,6 +2221,7 @@ export declare class HeadlessSession {
|
|
|
2194
2221
|
};
|
|
2195
2222
|
bootstrap(launchOptions?: HeadlessWxLaunchOptions): HeadlessAppInstance;
|
|
2196
2223
|
private syncRuntimeAppState;
|
|
2224
|
+
private bootstrapNavigation;
|
|
2197
2225
|
reLaunch(url: string): HeadlessPageInstance;
|
|
2198
2226
|
navigateTo(url: string): HeadlessPageInstance;
|
|
2199
2227
|
redirectTo(url: string): HeadlessPageInstance;
|
|
@@ -2544,18 +2572,19 @@ export declare class BrowserHeadlessSession {
|
|
|
2544
2572
|
callTapBinding(scopeId: string, methodName: string): any;
|
|
2545
2573
|
callTapBindingWithEvent(scopeId: string, methodName: string, event?: {
|
|
2546
2574
|
currentTarget?: {
|
|
2547
|
-
dataset?: Record<string,
|
|
2575
|
+
dataset?: Record<string, unknown>;
|
|
2548
2576
|
id?: string;
|
|
2549
2577
|
};
|
|
2550
|
-
dataset?: Record<string,
|
|
2578
|
+
dataset?: Record<string, unknown>;
|
|
2551
2579
|
id?: string;
|
|
2552
2580
|
mark?: Record<string, unknown>;
|
|
2553
2581
|
target?: {
|
|
2554
|
-
dataset?: Record<string,
|
|
2582
|
+
dataset?: Record<string, unknown>;
|
|
2555
2583
|
id?: string;
|
|
2556
2584
|
};
|
|
2557
2585
|
}): any;
|
|
2558
2586
|
bootstrap(launchOptions?: HeadlessWxLaunchOptions): HeadlessAppInstance;
|
|
2587
|
+
private bootstrapNavigation;
|
|
2559
2588
|
reLaunch(url: string): HeadlessPageInstance;
|
|
2560
2589
|
navigateTo(url: string): HeadlessPageInstance;
|
|
2561
2590
|
redirectTo(url: string): HeadlessPageInstance;
|
|
@@ -2640,25 +2669,26 @@ interface DomNodeLike$3 {
|
|
|
2640
2669
|
attribs?: Record<string, string>;
|
|
2641
2670
|
children?: DomNodeLike$3[];
|
|
2642
2671
|
data?: string;
|
|
2672
|
+
dataset?: Record<string, unknown>;
|
|
2643
2673
|
name?: string;
|
|
2644
2674
|
parent?: DomNodeLike$3 | null;
|
|
2645
2675
|
type?: string;
|
|
2646
2676
|
}
|
|
2647
|
-
interface HeadlessTestingNodeEventInit {
|
|
2677
|
+
export interface HeadlessTestingNodeEventInit {
|
|
2648
2678
|
currentTarget?: {
|
|
2649
|
-
dataset?: Record<string,
|
|
2679
|
+
dataset?: Record<string, unknown>;
|
|
2650
2680
|
id?: string;
|
|
2651
2681
|
};
|
|
2652
|
-
dataset?: Record<string,
|
|
2682
|
+
dataset?: Record<string, unknown>;
|
|
2653
2683
|
detail?: unknown;
|
|
2654
2684
|
id?: string;
|
|
2655
2685
|
mark?: Record<string, unknown>;
|
|
2656
2686
|
target?: {
|
|
2657
|
-
dataset?: Record<string,
|
|
2687
|
+
dataset?: Record<string, unknown>;
|
|
2658
2688
|
id?: string;
|
|
2659
2689
|
};
|
|
2660
2690
|
}
|
|
2661
|
-
interface HeadlessTestingNodeValueEventInit extends HeadlessTestingNodeEventInit {
|
|
2691
|
+
export interface HeadlessTestingNodeValueEventInit extends HeadlessTestingNodeEventInit {
|
|
2662
2692
|
detail?: {
|
|
2663
2693
|
value?: string;
|
|
2664
2694
|
[key: string]: unknown;
|
|
@@ -2686,7 +2716,7 @@ export declare class HeadlessTestingNodeHandle {
|
|
|
2686
2716
|
$$(selector: string): Promise<HeadlessTestingNodeHandle[]>;
|
|
2687
2717
|
getElementsByXpath(expression: string): Promise<HeadlessTestingNodeHandle[]>;
|
|
2688
2718
|
attr(name: string): Promise<string | undefined>;
|
|
2689
|
-
dataset(): Promise<Record<string,
|
|
2719
|
+
dataset(): Promise<Record<string, unknown>>;
|
|
2690
2720
|
scope(): Promise<{
|
|
2691
2721
|
scopeId: string;
|
|
2692
2722
|
snapshot: () => Promise<unknown>;
|
|
@@ -2721,6 +2751,7 @@ interface DomNodeLike$2 {
|
|
|
2721
2751
|
attribs?: Record<string, string>;
|
|
2722
2752
|
children?: DomNodeLike$2[];
|
|
2723
2753
|
data?: string;
|
|
2754
|
+
dataset?: Record<string, unknown>;
|
|
2724
2755
|
name?: string;
|
|
2725
2756
|
parent?: DomNodeLike$2 | null;
|
|
2726
2757
|
type?: string;
|
|
@@ -2736,6 +2767,7 @@ interface DomNodeLike$1 {
|
|
|
2736
2767
|
attribs?: Record<string, string>;
|
|
2737
2768
|
children?: DomNodeLike$1[];
|
|
2738
2769
|
data?: string;
|
|
2770
|
+
dataset?: Record<string, unknown>;
|
|
2739
2771
|
name?: string;
|
|
2740
2772
|
parent?: DomNodeLike$1 | null;
|
|
2741
2773
|
type?: string;
|
package/dist/index.mjs
CHANGED
|
@@ -1776,6 +1776,26 @@ function flushComponentAttachments(instances, attach) {
|
|
|
1776
1776
|
return pending.length > 0;
|
|
1777
1777
|
}
|
|
1778
1778
|
//#endregion
|
|
1779
|
+
//#region src/runtime/componentInstance/discardedReady.ts
|
|
1780
|
+
const discardedReady = /* @__PURE__ */ new WeakMap();
|
|
1781
|
+
/** 初始模板分支被 props 替换时,微信仍发送 ready,但不发送 attached 或 detached。 */
|
|
1782
|
+
function scheduleDiscardedComponentReady(cache, scopeId, instance) {
|
|
1783
|
+
const pending = discardedReady.get(cache) ?? /* @__PURE__ */ new Map();
|
|
1784
|
+
pending.set(scopeId, instance);
|
|
1785
|
+
discardedReady.set(cache, pending);
|
|
1786
|
+
}
|
|
1787
|
+
function flushDiscardedComponentReady(cache, pageScopeId, ready) {
|
|
1788
|
+
const pending = discardedReady.get(cache);
|
|
1789
|
+
if (!pending) return;
|
|
1790
|
+
const current = [...pending].filter(([scopeId]) => scopeId.startsWith(`${pageScopeId}/`));
|
|
1791
|
+
for (const [scopeId, instance] of current) {
|
|
1792
|
+
pending.delete(scopeId);
|
|
1793
|
+
instance.__ready__ = true;
|
|
1794
|
+
}
|
|
1795
|
+
for (const [, instance] of current) ready(instance);
|
|
1796
|
+
if (!pending.size) discardedReady.delete(cache);
|
|
1797
|
+
}
|
|
1798
|
+
//#endregion
|
|
1779
1799
|
//#region src/runtime/pageLifecycle.ts
|
|
1780
1800
|
const loadingPages = /* @__PURE__ */ new WeakSet();
|
|
1781
1801
|
function isPageBeforeReady(page) {
|
|
@@ -1876,13 +1896,13 @@ const BLOCKED_MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
|
1876
1896
|
"prototype"
|
|
1877
1897
|
]);
|
|
1878
1898
|
const NUMERIC_DOT_PATH_RE = /\.(\d+)(?=[.[\s,}\]]|$)/g;
|
|
1879
|
-
function isMustacheOnly$
|
|
1899
|
+
function isMustacheOnly$3(value) {
|
|
1880
1900
|
const trimmed = value.trim();
|
|
1881
1901
|
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
1882
1902
|
}
|
|
1883
1903
|
function unwrapMustacheExpression(expression) {
|
|
1884
1904
|
const normalized = expression.trim();
|
|
1885
|
-
if (isMustacheOnly$
|
|
1905
|
+
if (isMustacheOnly$3(normalized)) return normalized.slice(2, -2).trim();
|
|
1886
1906
|
return normalized;
|
|
1887
1907
|
}
|
|
1888
1908
|
function parseTemplateExpression(expression) {
|
|
@@ -2128,6 +2148,20 @@ function resolveBrowserComponentPageStyleIsolation(files, page, route, miniprogr
|
|
|
2128
2148
|
return isolation === void 0 ? definitionIsolation : typeof isolation === "string" ? isolation : void 0;
|
|
2129
2149
|
}
|
|
2130
2150
|
//#endregion
|
|
2151
|
+
//#region src/runtime/componentInstance/selection.ts
|
|
2152
|
+
function hasExportBehavior(definition, visited = /* @__PURE__ */ new Set()) {
|
|
2153
|
+
if (visited.has(definition)) return false;
|
|
2154
|
+
visited.add(definition);
|
|
2155
|
+
return (Array.isArray(definition.behaviors) ? definition.behaviors : []).some((behavior) => behavior === "wx://component-export" || behavior !== null && typeof behavior === "object" && hasExportBehavior(behavior, visited));
|
|
2156
|
+
}
|
|
2157
|
+
/** 原生选择器尊重组件导出行为;测试桥接继续保留完整组件实例。 */
|
|
2158
|
+
function resolveNativeComponentSelection(instance) {
|
|
2159
|
+
if (!instance) return null;
|
|
2160
|
+
const definition = instance.__definition__;
|
|
2161
|
+
if (!definition || !hasExportBehavior(definition) || typeof instance.export !== "function") return instance;
|
|
2162
|
+
return instance.export();
|
|
2163
|
+
}
|
|
2164
|
+
//#endregion
|
|
2131
2165
|
//#region src/runtime/componentResolution.ts
|
|
2132
2166
|
function resolveMiniProgramComponent(ownerFilePath, request, miniprogramRootPath, hasFile) {
|
|
2133
2167
|
const localPath = request.startsWith("/") ? request.replace(/^\/+/, "") : join(dirname(ownerFilePath), request);
|
|
@@ -2249,6 +2283,21 @@ function resolveSelectorQueryScopeSnapshot(scope, snapshots) {
|
|
|
2249
2283
|
return null;
|
|
2250
2284
|
}
|
|
2251
2285
|
//#endregion
|
|
2286
|
+
//#region src/view/nodeDataset.ts
|
|
2287
|
+
const DATASET_NAME_RE = /-([a-z])/g;
|
|
2288
|
+
function isDatasetAttribute(attributeName) {
|
|
2289
|
+
return attributeName.startsWith("data-") && !attributeName.startsWith("data-sim-");
|
|
2290
|
+
}
|
|
2291
|
+
function toDatasetKey(attributeName) {
|
|
2292
|
+
return attributeName.slice(5).replace(DATASET_NAME_RE, (_match, char) => char.toUpperCase());
|
|
2293
|
+
}
|
|
2294
|
+
function collectNodeDataset(node) {
|
|
2295
|
+
if (node.dataset) return { ...node.dataset };
|
|
2296
|
+
const dataset = {};
|
|
2297
|
+
for (const [key, value] of Object.entries(node.attribs ?? {})) if (isDatasetAttribute(key)) dataset[toDatasetKey(key)] = value;
|
|
2298
|
+
return dataset;
|
|
2299
|
+
}
|
|
2300
|
+
//#endregion
|
|
2252
2301
|
//#region src/view/templateImports.ts
|
|
2253
2302
|
function createImportedTemplateState(root, filePath, loadImport, loadWxs) {
|
|
2254
2303
|
const documents = /* @__PURE__ */ new Map();
|
|
@@ -2278,7 +2327,8 @@ function createImportedTemplateState(root, filePath, loadImport, loadWxs) {
|
|
|
2278
2327
|
return;
|
|
2279
2328
|
}
|
|
2280
2329
|
if (child.name === "import" && child.attribs?.src) {
|
|
2281
|
-
const
|
|
2330
|
+
const source = child.attribs.src;
|
|
2331
|
+
const imported = loadImport(owner, source.endsWith(".wxml") ? source : `${source}.wxml`);
|
|
2282
2332
|
const importedDocument = collect(imported.root, imported.filePath);
|
|
2283
2333
|
for (const [name, definition] of importedDocument.exports) state.definitions.set(name, definition);
|
|
2284
2334
|
}
|
|
@@ -2334,7 +2384,6 @@ function parseWxsTemplateDocument(templateSource) {
|
|
|
2334
2384
|
//#endregion
|
|
2335
2385
|
//#region src/browser/render/shared.ts
|
|
2336
2386
|
const TEMPLATE_INTERPOLATION_RE$2 = /\{\{([^{}]+)\}\}/g;
|
|
2337
|
-
const DATASET_NAME_RE$4 = /-([a-z])/g;
|
|
2338
2387
|
const LEADING_SLASH_RE$5 = /^\/+/;
|
|
2339
2388
|
const EVENT_BINDING_ATTRS$1 = [
|
|
2340
2389
|
"bindtap",
|
|
@@ -2353,19 +2402,16 @@ const STRUCTURAL_ATTRS$1 = [
|
|
|
2353
2402
|
];
|
|
2354
2403
|
const CLASS_SPLIT_RE$1 = /\s+/;
|
|
2355
2404
|
const JS_FILE_RE$1 = /\.js$/;
|
|
2356
|
-
function isMustacheOnly$
|
|
2405
|
+
function isMustacheOnly$2(value) {
|
|
2357
2406
|
const trimmed = value.trim();
|
|
2358
2407
|
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
2359
2408
|
}
|
|
2360
|
-
function
|
|
2361
|
-
|
|
2362
|
-
}
|
|
2363
|
-
function collectDataset$4(node, source) {
|
|
2409
|
+
function collectDataset$1(node, source) {
|
|
2410
|
+
if (node.dataset) return collectNodeDataset(node);
|
|
2364
2411
|
const dataset = {};
|
|
2365
2412
|
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
2366
|
-
if (!
|
|
2367
|
-
|
|
2368
|
-
dataset[toDatasetKey$4(key)] = String(resolvedValue ?? "");
|
|
2413
|
+
if (!isDatasetAttribute(key)) continue;
|
|
2414
|
+
dataset[toDatasetKey(key)] = source && isMustacheOnly$2(value) ? resolveTemplateExpression(source, value) : value;
|
|
2369
2415
|
}
|
|
2370
2416
|
return dataset;
|
|
2371
2417
|
}
|
|
@@ -2379,13 +2425,14 @@ function cloneNode$1(node) {
|
|
|
2379
2425
|
return {
|
|
2380
2426
|
...node,
|
|
2381
2427
|
attribs: node.attribs ? { ...node.attribs } : void 0,
|
|
2428
|
+
dataset: node.dataset ? { ...node.dataset } : void 0,
|
|
2382
2429
|
children: node.children?.map((child) => cloneNode$1(child))
|
|
2383
2430
|
};
|
|
2384
2431
|
}
|
|
2385
2432
|
function resolveValueByPath$2(source, expression) {
|
|
2386
2433
|
return resolveTemplateExpression(source, expression) ?? "";
|
|
2387
2434
|
}
|
|
2388
|
-
function resolveRawValueByPath$
|
|
2435
|
+
function resolveRawValueByPath$2(source, expression) {
|
|
2389
2436
|
return resolveTemplateExpression(source, expression);
|
|
2390
2437
|
}
|
|
2391
2438
|
function interpolateTemplate$2(input, data) {
|
|
@@ -2428,16 +2475,16 @@ function isTagNode$1(node) {
|
|
|
2428
2475
|
return node.type === "tag" && typeof node.name === "string";
|
|
2429
2476
|
}
|
|
2430
2477
|
function resolveAttributeValue$1(value, scope) {
|
|
2431
|
-
if (isMustacheOnly$
|
|
2478
|
+
if (isMustacheOnly$2(value)) {
|
|
2432
2479
|
const expression = value.trim().slice(2, -2);
|
|
2433
|
-
return
|
|
2480
|
+
return resolveRawValueByPath$2(wxsScopeData(scope), expression);
|
|
2434
2481
|
}
|
|
2435
2482
|
return interpolateTemplate$2(value, wxsScopeData(scope));
|
|
2436
2483
|
}
|
|
2437
2484
|
function resolveComponentAttributeValue$1(value, scope) {
|
|
2438
|
-
if (isMustacheOnly$
|
|
2485
|
+
if (isMustacheOnly$2(value)) {
|
|
2439
2486
|
const expression = value.trim().slice(2, -2);
|
|
2440
|
-
return resolveRawValueByPath$
|
|
2487
|
+
return resolveRawValueByPath$2(wxsScopeData(scope), expression) ?? null;
|
|
2441
2488
|
}
|
|
2442
2489
|
return interpolateTemplate$2(value, wxsScopeData(scope));
|
|
2443
2490
|
}
|
|
@@ -2449,18 +2496,25 @@ function applyNodeBindings$1(node, scope) {
|
|
|
2449
2496
|
node.attribs ??= {};
|
|
2450
2497
|
node.attribs["data-sim-scope"] = scope.getScopeId();
|
|
2451
2498
|
for (const key of STRUCTURAL_ATTRS$1) delete node.attribs[key];
|
|
2499
|
+
let dataset;
|
|
2452
2500
|
for (const [key, value] of Object.entries({ ...node.attribs })) {
|
|
2453
2501
|
if (EVENT_BINDING_ATTRS$1.includes(key)) {
|
|
2454
2502
|
node.attribs["data-sim-tap"] = value;
|
|
2455
2503
|
continue;
|
|
2456
2504
|
}
|
|
2457
|
-
|
|
2505
|
+
const resolvedValue = typeof value === "string" ? resolveAttributeValue$1(value, scope) : value;
|
|
2506
|
+
node.attribs[key] = String(resolvedValue ?? "");
|
|
2507
|
+
if (isDatasetAttribute(key)) {
|
|
2508
|
+
dataset ??= {};
|
|
2509
|
+
dataset[toDatasetKey(key)] = resolvedValue;
|
|
2510
|
+
}
|
|
2458
2511
|
}
|
|
2512
|
+
node.dataset = dataset;
|
|
2459
2513
|
}
|
|
2460
2514
|
function evaluateConditionalBranch$1(node, scope) {
|
|
2461
2515
|
const condition = node.attribs?.["wx:if"] ?? node.attribs?.["wx:elif"];
|
|
2462
2516
|
if (condition == null) return true;
|
|
2463
|
-
return Boolean(resolveRawValueByPath$
|
|
2517
|
+
return Boolean(resolveRawValueByPath$2(wxsScopeData(scope), condition));
|
|
2464
2518
|
}
|
|
2465
2519
|
function createLoopScope$1(scope, itemName, indexName, item, index) {
|
|
2466
2520
|
return {
|
|
@@ -2534,7 +2588,7 @@ function collectComponentEventBindings$1(hostNode) {
|
|
|
2534
2588
|
return collectMiniProgramEventBindings(hostNode.attribs);
|
|
2535
2589
|
}
|
|
2536
2590
|
function buildComponentTrigger$1(componentScopeId, context, hostNode) {
|
|
2537
|
-
const hostDataset = collectDataset$
|
|
2591
|
+
const hostDataset = collectDataset$1(hostNode);
|
|
2538
2592
|
const hostId = hostNode.attribs?.id ?? "";
|
|
2539
2593
|
return (instance, eventName, detail, triggerOptions) => {
|
|
2540
2594
|
const originScope = context.componentScopes.get(componentScopeId);
|
|
@@ -2596,7 +2650,7 @@ function createComponentScope$1(clonedNode, scope, componentScopeId, componentIn
|
|
|
2596
2650
|
alias: clonedNode.name,
|
|
2597
2651
|
classList: String(clonedNode.attribs?.class ?? "").split(CLASS_SPLIT_RE$1).map((item) => item.trim()).filter(Boolean),
|
|
2598
2652
|
data: { ...componentInstance.data },
|
|
2599
|
-
dataset: collectDataset$
|
|
2653
|
+
dataset: collectDataset$1(clonedNode, wxsScopeData(scope)),
|
|
2600
2654
|
eventBindings: collectComponentEventBindings$1(clonedNode),
|
|
2601
2655
|
getMethod: (methodName) => {
|
|
2602
2656
|
const method = componentInstance?.[methodName];
|
|
@@ -2619,7 +2673,7 @@ function resolveComponentProperties$1(clonedNode, scope, definition) {
|
|
|
2619
2673
|
if (key.startsWith("bind") || key.startsWith("generic:")) continue;
|
|
2620
2674
|
const camelizedKey = key.replace(/-([a-z])/g, (_match, char) => char.toUpperCase());
|
|
2621
2675
|
const propertyKey = key in declaredProperties || !(camelizedKey in declaredProperties) ? key : camelizedKey;
|
|
2622
|
-
if (isMustacheOnly$
|
|
2676
|
+
if (isMustacheOnly$2(String(value))) bindingExpressions[propertyKey] = String(value).trim().slice(2, -2).trim();
|
|
2623
2677
|
nextProperties[propertyKey] = resolveComponentAttributeValue$1(String(value), scope);
|
|
2624
2678
|
}
|
|
2625
2679
|
return {
|
|
@@ -2627,7 +2681,7 @@ function resolveComponentProperties$1(clonedNode, scope, definition) {
|
|
|
2627
2681
|
bindingExpressions
|
|
2628
2682
|
};
|
|
2629
2683
|
}
|
|
2630
|
-
function createBrowserComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId) {
|
|
2684
|
+
function createBrowserComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, beforeCreated) {
|
|
2631
2685
|
const componentProperties = Object.keys(componentEntry.definition.methods ?? {}).some((key) => key.startsWith("__weapp_vite_")) || Object.hasOwn(componentEntry.definition.properties ?? {}, "__wvSlotOwnerId") ? Object.fromEntries(Object.keys(componentEntry.definition.properties ?? {}).filter((key) => Object.hasOwn(nextProperties, key)).map((key) => [key, nextProperties[key]])) : nextProperties;
|
|
2632
2686
|
const componentInstance = createComponentInstance({
|
|
2633
2687
|
definition: componentEntry.definition,
|
|
@@ -2639,10 +2693,11 @@ function createBrowserComponentInstance(componentScopeId, context, clonedNode, c
|
|
|
2639
2693
|
componentInstance.createIntersectionObserver = (options) => context.session.createIntersectionObserver(componentInstance, options);
|
|
2640
2694
|
componentInstance.createMediaQueryObserver = () => context.session.createMediaQueryObserver(componentInstance);
|
|
2641
2695
|
componentInstance.createSelectorQuery = () => context.moduleLoader.wx.createSelectorQuery().in(componentInstance);
|
|
2642
|
-
componentInstance.selectComponent = (selector) => context.session.selectComponentWithin(componentScopeId, selector);
|
|
2643
|
-
componentInstance.selectAllComponents = (selector) => context.session.selectAllComponentsWithin(componentScopeId, selector);
|
|
2696
|
+
componentInstance.selectComponent = (selector) => resolveNativeComponentSelection(context.session.selectComponentWithin(componentScopeId, selector));
|
|
2697
|
+
componentInstance.selectAllComponents = (selector) => context.session.selectAllComponentsWithin(componentScopeId, selector).map(resolveNativeComponentSelection);
|
|
2644
2698
|
componentInstance.selectOwnerComponent = () => ownerScopeId ? context.componentCache.get(ownerScopeId) ?? null : null;
|
|
2645
2699
|
context.componentCache.set(componentScopeId, componentInstance);
|
|
2700
|
+
beforeCreated?.(componentInstance);
|
|
2646
2701
|
runComponentLifecycle(componentInstance, "created");
|
|
2647
2702
|
componentInstance.__propertySnapshots = Object.fromEntries(Object.entries(componentInstance.properties).map(([key, propertyValue]) => [key, cloneValue(propertyValue)]));
|
|
2648
2703
|
syncComponentProperties$1(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, componentProperties, {}, []);
|
|
@@ -2664,7 +2719,7 @@ function expandNodeByFor$1(node, scope) {
|
|
|
2664
2719
|
scope,
|
|
2665
2720
|
instanceSuffix: ""
|
|
2666
2721
|
}];
|
|
2667
|
-
const items = resolveLoopEntries(resolveRawValueByPath$
|
|
2722
|
+
const items = resolveLoopEntries(resolveRawValueByPath$2(wxsScopeData(scope), forExpression));
|
|
2668
2723
|
const itemName = node.attribs?.["wx:for-item"]?.trim() || "item";
|
|
2669
2724
|
const indexName = node.attribs?.["wx:for-index"]?.trim() || "index";
|
|
2670
2725
|
return items.map(([index, item]) => ({
|
|
@@ -2776,12 +2831,16 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
|
|
|
2776
2831
|
const componentScopeId = `${instancePath}/${clonedNode.name}`;
|
|
2777
2832
|
const ownerScopeId = scope.getScopeId().includes("/") ? scope.getScopeId() : void 0;
|
|
2778
2833
|
const { nextProperties, bindingExpressions } = resolveComponentProperties$1(clonedNode, scope, componentEntry.definition);
|
|
2834
|
+
const genericComponents = resolveComponentGenerics$1(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
2835
|
+
const slots = collectComponentSlots$1(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
2779
2836
|
let componentInstance = context.componentCache.get(componentScopeId);
|
|
2780
|
-
if (!componentInstance) componentInstance = createBrowserComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId)
|
|
2837
|
+
if (!componentInstance) componentInstance = createBrowserComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, (instance) => {
|
|
2838
|
+
const initialScope = createComponentScope$1(clonedNode, scope, componentScopeId, instance, genericComponents, slots);
|
|
2839
|
+
context.componentScopes.set(componentScopeId, initialScope);
|
|
2840
|
+
renderBrowserComponentTemplate(context, componentEntry, renderNodeTree$1, initialScope, componentScopeId, /* @__PURE__ */ new Set());
|
|
2841
|
+
});
|
|
2781
2842
|
else syncComponentProperties$1(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, nextProperties, bindingExpressions, context.changedPageKeys);
|
|
2782
2843
|
seenComponentScopes.add(componentScopeId);
|
|
2783
|
-
const genericComponents = resolveComponentGenerics$1(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
2784
|
-
const slots = collectComponentSlots$1(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
2785
2844
|
const componentScope = createComponentScope$1(clonedNode, scope, componentScopeId, componentInstance, genericComponents, slots);
|
|
2786
2845
|
context.componentScopes.set(componentScopeId, componentScope);
|
|
2787
2846
|
const renderedComponentRoot = renderBrowserComponentTemplate(context, componentEntry, renderNodeTree$1, componentScope, componentScopeId, seenComponentScopes);
|
|
@@ -2791,6 +2850,10 @@ function renderNodeTree$1(node, scope, context, ownerJsonPath, ownerFilePath, in
|
|
|
2791
2850
|
...clonedNode.attribs,
|
|
2792
2851
|
...renderedComponentRoot.attribs
|
|
2793
2852
|
};
|
|
2853
|
+
if (clonedNode.dataset || renderedComponentRoot.dataset) renderedComponentRoot.dataset = {
|
|
2854
|
+
...clonedNode.dataset,
|
|
2855
|
+
...renderedComponentRoot.dataset
|
|
2856
|
+
};
|
|
2794
2857
|
renderedComponentRoot.attribs["data-sim-component"] = clonedNode.name;
|
|
2795
2858
|
renderedComponentRoot.attribs["data-sim-scope"] = componentScopeId;
|
|
2796
2859
|
}
|
|
@@ -2832,10 +2895,11 @@ function renderBrowserPageTree(context, page) {
|
|
|
2832
2895
|
const pageAttaching = isComponentPageAttaching(page);
|
|
2833
2896
|
const attached = !pageAttaching && flushComponentAttachments([...seenComponentScopes].map((scopeId) => context.componentCache.get(scopeId)), (instance) => {
|
|
2834
2897
|
runComponentLifecycle(instance, "attached");
|
|
2835
|
-
runComponentPageLifetime(instance, "show");
|
|
2898
|
+
if (instance !== context.componentCache.get(customTabBarScopeId(route))) runComponentPageLifetime(instance, "show");
|
|
2836
2899
|
});
|
|
2837
2900
|
const removed = [...context.componentCache].filter(([scopeId]) => scopeId.startsWith(`${pageScopeId}/`) && !seenComponentScopes.has(scopeId));
|
|
2838
|
-
for (const [, instance] of removed) runComponentLifecycle(instance, "detached");
|
|
2901
|
+
for (const [scopeId, instance] of removed) if (isComponentAttached(instance)) runComponentLifecycle(instance, "detached");
|
|
2902
|
+
else scheduleDiscardedComponentReady(context.componentCache, scopeId, instance);
|
|
2839
2903
|
const instances = [...seenComponentScopes].map((scopeId) => context.componentCache.get(scopeId));
|
|
2840
2904
|
const relationsChanged = !pageAttaching && !hasPendingComponentAttachments(instances) && syncComponentRelations(context.componentCache, seenComponentScopes, pageScopeId);
|
|
2841
2905
|
for (const [scopeId] of removed) {
|
|
@@ -2843,6 +2907,7 @@ function renderBrowserPageTree(context, page) {
|
|
|
2843
2907
|
context.componentScopes.delete(scopeId);
|
|
2844
2908
|
}
|
|
2845
2909
|
if (pageAttached || attached || relationsChanged) return renderBrowserPageTree(context, page);
|
|
2910
|
+
if (!isPageBeforeReady(page)) flushDiscardedComponentReady(context.componentCache, pageScopeId, (instance) => runComponentLifecycle(instance, "ready"));
|
|
2846
2911
|
if (!isPageBeforeReady(page) && flushComponentReady(instances, (instance) => runComponentLifecycle(instance, "ready"))) return renderBrowserPageTree(context, page);
|
|
2847
2912
|
const treeRoot = roots.length > 1 ? {
|
|
2848
2913
|
type: "root",
|
|
@@ -3251,6 +3316,110 @@ function deriveMenuButtonBoundingClientRect(systemInfo) {
|
|
|
3251
3316
|
};
|
|
3252
3317
|
}
|
|
3253
3318
|
//#endregion
|
|
3319
|
+
//#region src/runtime/request/task.ts
|
|
3320
|
+
/** mock 与显式网络桥接共用终止状态;终止后不再分发 headers/chunk。 */
|
|
3321
|
+
function createRequestTask(option, onAbort = () => {}) {
|
|
3322
|
+
const headers = /* @__PURE__ */ new Set();
|
|
3323
|
+
const chunks = /* @__PURE__ */ new Set();
|
|
3324
|
+
let settled = false;
|
|
3325
|
+
function finish(result, success) {
|
|
3326
|
+
if (settled) return;
|
|
3327
|
+
settled = true;
|
|
3328
|
+
headers.clear();
|
|
3329
|
+
chunks.clear();
|
|
3330
|
+
try {
|
|
3331
|
+
if (success) option.success?.(result);
|
|
3332
|
+
else option.fail?.(result);
|
|
3333
|
+
} finally {
|
|
3334
|
+
option.complete?.(result);
|
|
3335
|
+
}
|
|
3336
|
+
}
|
|
3337
|
+
return {
|
|
3338
|
+
task: {
|
|
3339
|
+
abort() {
|
|
3340
|
+
if (settled) return;
|
|
3341
|
+
const error = Object.assign(/* @__PURE__ */ new Error("request:fail abort"), { errMsg: "request:fail abort" });
|
|
3342
|
+
try {
|
|
3343
|
+
finish(error, false);
|
|
3344
|
+
} finally {
|
|
3345
|
+
onAbort();
|
|
3346
|
+
}
|
|
3347
|
+
},
|
|
3348
|
+
onHeadersReceived(callback) {
|
|
3349
|
+
headers.add(callback);
|
|
3350
|
+
},
|
|
3351
|
+
offHeadersReceived(callback) {
|
|
3352
|
+
if (callback) headers.delete(callback);
|
|
3353
|
+
else headers.clear();
|
|
3354
|
+
},
|
|
3355
|
+
onChunkReceived(callback) {
|
|
3356
|
+
chunks.add(callback);
|
|
3357
|
+
},
|
|
3358
|
+
offChunkReceived(callback) {
|
|
3359
|
+
if (callback) chunks.delete(callback);
|
|
3360
|
+
else chunks.clear();
|
|
3361
|
+
}
|
|
3362
|
+
},
|
|
3363
|
+
get settled() {
|
|
3364
|
+
return settled;
|
|
3365
|
+
},
|
|
3366
|
+
headers(result) {
|
|
3367
|
+
if (settled) return;
|
|
3368
|
+
for (const callback of Array.from(headers)) if (!settled && headers.has(callback)) callback(result);
|
|
3369
|
+
},
|
|
3370
|
+
chunk(data) {
|
|
3371
|
+
if (settled || !option.enableChunked) return;
|
|
3372
|
+
for (const callback of Array.from(chunks)) if (!settled && chunks.has(callback)) callback({ data: data.slice(0) });
|
|
3373
|
+
},
|
|
3374
|
+
succeed(result) {
|
|
3375
|
+
finish(result, true);
|
|
3376
|
+
},
|
|
3377
|
+
fail(error) {
|
|
3378
|
+
finish(Object.assign(error, { errMsg: "errMsg" in error ? error.errMsg : error.message }), false);
|
|
3379
|
+
}
|
|
3380
|
+
};
|
|
3381
|
+
}
|
|
3382
|
+
//#endregion
|
|
3383
|
+
//#region src/runtime/request/mock.ts
|
|
3384
|
+
/** 仅模拟已声明的分块;不进行网络访问,也不把完整 response 拆成假分块。 */
|
|
3385
|
+
function createChunkedMockRequest(option, mock, response, scheduler, onSuccess) {
|
|
3386
|
+
let timer;
|
|
3387
|
+
const request = createRequestTask(option, () => scheduler.clearTimeout(timer));
|
|
3388
|
+
const chunks = (mock.chunks ?? []).map((chunk) => ({
|
|
3389
|
+
...chunk,
|
|
3390
|
+
data: chunk.data.slice(0)
|
|
3391
|
+
}));
|
|
3392
|
+
let index = 0;
|
|
3393
|
+
function next() {
|
|
3394
|
+
if (request.settled) return;
|
|
3395
|
+
const chunk = chunks[index++];
|
|
3396
|
+
if (chunk) timer = scheduler.setTimeout(() => {
|
|
3397
|
+
request.chunk(chunk.data);
|
|
3398
|
+
next();
|
|
3399
|
+
}, Math.max(0, chunk.delay ?? 0));
|
|
3400
|
+
else timer = scheduler.setTimeout(() => {
|
|
3401
|
+
if (request.settled) return;
|
|
3402
|
+
if (mock.error) request.fail(new Error(mock.error));
|
|
3403
|
+
else {
|
|
3404
|
+
onSuccess();
|
|
3405
|
+
request.succeed({
|
|
3406
|
+
...response,
|
|
3407
|
+
data: ""
|
|
3408
|
+
});
|
|
3409
|
+
}
|
|
3410
|
+
}, 0);
|
|
3411
|
+
}
|
|
3412
|
+
timer = scheduler.setTimeout(() => {
|
|
3413
|
+
request.headers({
|
|
3414
|
+
cookies: response.cookies,
|
|
3415
|
+
header: response.header,
|
|
3416
|
+
statusCode: response.statusCode
|
|
3417
|
+
});
|
|
3418
|
+
next();
|
|
3419
|
+
}, Math.max(0, mock.delay ?? 0));
|
|
3420
|
+
return request.task;
|
|
3421
|
+
}
|
|
3422
|
+
//#endregion
|
|
3254
3423
|
//#region src/runtime/wxState.ts
|
|
3255
3424
|
const STORAGE_LIMIT_SIZE = 10240;
|
|
3256
3425
|
const DEFAULT_MODAL_CANCEL_COLOR = "#000000";
|
|
@@ -4572,6 +4741,7 @@ function createHeadlessWxState(scheduler = nativeScheduler, options = {}) {
|
|
|
4572
4741
|
request(option) {
|
|
4573
4742
|
const matchedMock = requestMocks.find((mock) => matchesRequestMock(mock, option));
|
|
4574
4743
|
if (!matchedMock) {
|
|
4744
|
+
const request = createRequestTask(option);
|
|
4575
4745
|
const logEntry = {
|
|
4576
4746
|
data: cloneValue$1(option.data),
|
|
4577
4747
|
header: { ...option.header ?? {} },
|
|
@@ -4582,9 +4752,8 @@ function createHeadlessWxState(scheduler = nativeScheduler, options = {}) {
|
|
|
4582
4752
|
requestLogs.push(logEntry);
|
|
4583
4753
|
const error = /* @__PURE__ */ new Error(`No request mock matched in headless runtime: ${normalizeMethod(option.method)} ${option.url}`);
|
|
4584
4754
|
if (options.strictMocks) throw error;
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
return createNoopTask();
|
|
4755
|
+
request.fail(error);
|
|
4756
|
+
return request.task;
|
|
4588
4757
|
}
|
|
4589
4758
|
const response = resolveRequestResponse(matchedMock, option);
|
|
4590
4759
|
const requestLogEntry = {
|
|
@@ -4596,20 +4765,31 @@ function createHeadlessWxState(scheduler = nativeScheduler, options = {}) {
|
|
|
4596
4765
|
url: option.url
|
|
4597
4766
|
};
|
|
4598
4767
|
const delay = Number.isFinite(matchedMock.delay) ? Math.max(0, Math.trunc(matchedMock.delay ?? 0)) : 0;
|
|
4768
|
+
if (option.enableChunked) return createChunkedMockRequest(option, matchedMock, response, scheduler, () => {
|
|
4769
|
+
requestLogs.push({
|
|
4770
|
+
...requestLogEntry,
|
|
4771
|
+
response: {
|
|
4772
|
+
...response,
|
|
4773
|
+
data: ""
|
|
4774
|
+
}
|
|
4775
|
+
});
|
|
4776
|
+
});
|
|
4777
|
+
const request = createRequestTask(option);
|
|
4599
4778
|
if (delay <= 0) {
|
|
4600
4779
|
requestLogs.push(requestLogEntry);
|
|
4601
|
-
|
|
4602
|
-
|
|
4603
|
-
return createNoopTask();
|
|
4780
|
+
request.succeed(response);
|
|
4781
|
+
return request.task;
|
|
4604
4782
|
}
|
|
4605
|
-
|
|
4783
|
+
const delayed = runDelayedTask(delay, () => {
|
|
4606
4784
|
requestLogs.push(requestLogEntry);
|
|
4607
|
-
|
|
4608
|
-
option.complete?.(response);
|
|
4785
|
+
request.succeed(response);
|
|
4609
4786
|
}, (error) => {
|
|
4610
|
-
|
|
4611
|
-
option.complete?.();
|
|
4787
|
+
request.fail(error);
|
|
4612
4788
|
}, "request:fail abort");
|
|
4789
|
+
return {
|
|
4790
|
+
...request.task,
|
|
4791
|
+
abort: delayed.abort
|
|
4792
|
+
};
|
|
4613
4793
|
},
|
|
4614
4794
|
removeSavedFile(option) {
|
|
4615
4795
|
return removeSavedFile(option.filePath);
|
|
@@ -4891,23 +5071,11 @@ function queryXPathElements(root, expression) {
|
|
|
4891
5071
|
}
|
|
4892
5072
|
//#endregion
|
|
4893
5073
|
//#region src/view/nodeHandle.ts
|
|
4894
|
-
const DATASET_NAME_RE$3 = /-([a-z])/g;
|
|
4895
5074
|
function escapeText$2(text) {
|
|
4896
5075
|
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
4897
5076
|
}
|
|
4898
|
-
function toDatasetKey$3(attributeName) {
|
|
4899
|
-
return attributeName.slice(5).replace(DATASET_NAME_RE$3, (_match, char) => char.toUpperCase());
|
|
4900
|
-
}
|
|
4901
|
-
function collectDataset$3(node) {
|
|
4902
|
-
const dataset = {};
|
|
4903
|
-
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
4904
|
-
if (!key.startsWith("data-") || key.startsWith("data-sim-")) continue;
|
|
4905
|
-
dataset[toDatasetKey$3(key)] = value;
|
|
4906
|
-
}
|
|
4907
|
-
return dataset;
|
|
4908
|
-
}
|
|
4909
5077
|
function createEventPayload$1(node, eventName, event) {
|
|
4910
|
-
const dataset =
|
|
5078
|
+
const dataset = collectNodeDataset(node);
|
|
4911
5079
|
const nodeId = node.attribs?.id ?? "";
|
|
4912
5080
|
return {
|
|
4913
5081
|
bubbles: false,
|
|
@@ -4995,7 +5163,7 @@ var HeadlessTestingNodeHandle = class HeadlessTestingNodeHandle {
|
|
|
4995
5163
|
}
|
|
4996
5164
|
async dataset() {
|
|
4997
5165
|
this.assertActive();
|
|
4998
|
-
return
|
|
5166
|
+
return collectNodeDataset(this.node);
|
|
4999
5167
|
}
|
|
5000
5168
|
async scope() {
|
|
5001
5169
|
this.assertActive();
|
|
@@ -5070,13 +5238,19 @@ var HeadlessTestingNodeHandle = class HeadlessTestingNodeHandle {
|
|
|
5070
5238
|
//#region src/view/render.ts
|
|
5071
5239
|
const LEADING_SLASH_RE$4 = /^\/+/;
|
|
5072
5240
|
const TEMPLATE_INTERPOLATION_RE$1 = /\{\{([^{}]+)\}\}/g;
|
|
5073
|
-
function
|
|
5241
|
+
function resolveRawValueByPath$1(source, expression) {
|
|
5074
5242
|
const normalized = expression.trim();
|
|
5075
|
-
if (!normalized) return
|
|
5243
|
+
if (!normalized) return;
|
|
5076
5244
|
const segments = normalized.split(".").filter(Boolean);
|
|
5077
5245
|
let current = source;
|
|
5078
|
-
for (const segment of segments)
|
|
5079
|
-
|
|
5246
|
+
for (const segment of segments) {
|
|
5247
|
+
if (current == null || typeof current !== "object" && typeof current !== "function") return;
|
|
5248
|
+
current = Reflect.get(current, segment);
|
|
5249
|
+
}
|
|
5250
|
+
return current;
|
|
5251
|
+
}
|
|
5252
|
+
function resolveValueByPath$1(source, expression) {
|
|
5253
|
+
return resolveRawValueByPath$1(source, expression) ?? "";
|
|
5080
5254
|
}
|
|
5081
5255
|
function interpolateTemplate$1(input, data) {
|
|
5082
5256
|
return input.replace(TEMPLATE_INTERPOLATION_RE$1, (_match, expression) => {
|
|
@@ -5084,6 +5258,10 @@ function interpolateTemplate$1(input, data) {
|
|
|
5084
5258
|
return typeof value === "string" ? value : String(value);
|
|
5085
5259
|
});
|
|
5086
5260
|
}
|
|
5261
|
+
function isMustacheOnly$1(value) {
|
|
5262
|
+
const trimmed = value.trim();
|
|
5263
|
+
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
5264
|
+
}
|
|
5087
5265
|
function visitDom(node, visitor) {
|
|
5088
5266
|
visitor(node);
|
|
5089
5267
|
for (const child of node.children ?? []) visitDom(child, visitor);
|
|
@@ -5091,7 +5269,18 @@ function visitDom(node, visitor) {
|
|
|
5091
5269
|
function interpolateDomTree(root, data) {
|
|
5092
5270
|
visitDom(root, (node) => {
|
|
5093
5271
|
if (typeof node.data === "string") node.data = interpolateTemplateText(node.data, data);
|
|
5094
|
-
if (node.attribs)
|
|
5272
|
+
if (node.attribs) {
|
|
5273
|
+
let dataset;
|
|
5274
|
+
for (const [key, value] of Object.entries(node.attribs)) {
|
|
5275
|
+
const resolvedValue = isMustacheOnly$1(value) ? resolveRawValueByPath$1(data, value.trim().slice(2, -2)) : interpolateTemplate$1(value, data);
|
|
5276
|
+
node.attribs[key] = String(resolvedValue ?? "");
|
|
5277
|
+
if (isDatasetAttribute(key)) {
|
|
5278
|
+
dataset ??= {};
|
|
5279
|
+
dataset[toDatasetKey(key)] = resolvedValue;
|
|
5280
|
+
}
|
|
5281
|
+
}
|
|
5282
|
+
node.dataset = dataset;
|
|
5283
|
+
}
|
|
5095
5284
|
});
|
|
5096
5285
|
}
|
|
5097
5286
|
function escapeText$1(text) {
|
|
@@ -5125,21 +5314,9 @@ function renderPageTree(project, page) {
|
|
|
5125
5314
|
}
|
|
5126
5315
|
//#endregion
|
|
5127
5316
|
//#region src/view/selectorQuery.ts
|
|
5128
|
-
const DATASET_NAME_RE$2 = /-([a-z])/g;
|
|
5129
5317
|
const LEADING_MARK_PREFIX_RE = /^mark[:\-]?/;
|
|
5130
5318
|
const MARK_NAME_RE = /[:\-]([a-z])/g;
|
|
5131
5319
|
const NUMERIC_LIKE_VALUE_RE$1 = /-?\d+(?:\.\d+)?/;
|
|
5132
|
-
function toDatasetKey$2(attributeName) {
|
|
5133
|
-
return attributeName.slice(5).replace(DATASET_NAME_RE$2, (_match, char) => char.toUpperCase());
|
|
5134
|
-
}
|
|
5135
|
-
function collectDataset$2(node) {
|
|
5136
|
-
const dataset = {};
|
|
5137
|
-
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
5138
|
-
if (!key.startsWith("data-") || key.startsWith("data-sim-")) continue;
|
|
5139
|
-
dataset[toDatasetKey$2(key)] = value;
|
|
5140
|
-
}
|
|
5141
|
-
return dataset;
|
|
5142
|
-
}
|
|
5143
5320
|
function toMarkKey(attributeName) {
|
|
5144
5321
|
return attributeName.replace(LEADING_MARK_PREFIX_RE, "").replace(MARK_NAME_RE, (_match, char) => char.toUpperCase());
|
|
5145
5322
|
}
|
|
@@ -5216,7 +5393,7 @@ function resolvePropertyValue(node, propertyName) {
|
|
|
5216
5393
|
if (!normalizedPropertyName) return;
|
|
5217
5394
|
if (normalizedPropertyName === "id") return node.attribs?.id ?? "";
|
|
5218
5395
|
if (normalizedPropertyName === "class") return node.attribs?.class ?? "";
|
|
5219
|
-
if (normalizedPropertyName === "dataset") return
|
|
5396
|
+
if (normalizedPropertyName === "dataset") return collectNodeDataset(node);
|
|
5220
5397
|
return node.attribs?.[normalizedPropertyName];
|
|
5221
5398
|
}
|
|
5222
5399
|
function pickProperties(node, propertyNames) {
|
|
@@ -5233,7 +5410,7 @@ function pickComputedStyle(node, propertyNames) {
|
|
|
5233
5410
|
function resolveFieldsResult(node, fields, options) {
|
|
5234
5411
|
const result = {};
|
|
5235
5412
|
if (fields.id) result.id = node.attribs?.id ?? "";
|
|
5236
|
-
if (fields.dataset) result.dataset =
|
|
5413
|
+
if (fields.dataset) result.dataset = collectNodeDataset(node);
|
|
5237
5414
|
if (fields.mark) result.mark = collectMark(node);
|
|
5238
5415
|
if (fields.rect || fields.size) {
|
|
5239
5416
|
const rect = resolveRect$1(node, options.root.name === "page" ? options.windowInfo : void 0);
|
|
@@ -5938,20 +6115,8 @@ function createHeadlessMediaQueryObserver(driver, onDisconnect) {
|
|
|
5938
6115
|
}
|
|
5939
6116
|
//#endregion
|
|
5940
6117
|
//#region src/view/videoContext.ts
|
|
5941
|
-
const DATASET_NAME_RE$1 = /-([a-z])/g;
|
|
5942
|
-
function toDatasetKey$1(attributeName) {
|
|
5943
|
-
return attributeName.slice(5).replace(DATASET_NAME_RE$1, (_match, char) => char.toUpperCase());
|
|
5944
|
-
}
|
|
5945
|
-
function collectDataset$1(node) {
|
|
5946
|
-
const dataset = {};
|
|
5947
|
-
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
5948
|
-
if (!key.startsWith("data-") || key.startsWith("data-sim-")) continue;
|
|
5949
|
-
dataset[toDatasetKey$1(key)] = value;
|
|
5950
|
-
}
|
|
5951
|
-
return dataset;
|
|
5952
|
-
}
|
|
5953
6118
|
function createEventPayload(node, eventName, detail) {
|
|
5954
|
-
const dataset =
|
|
6119
|
+
const dataset = collectNodeDataset(node);
|
|
5955
6120
|
const nodeId = node.attribs?.id ?? "";
|
|
5956
6121
|
return {
|
|
5957
6122
|
bubbles: false,
|
|
@@ -6063,7 +6228,7 @@ function matchesComponentSelector$1(scope, selector) {
|
|
|
6063
6228
|
if (dataAttrMatch) {
|
|
6064
6229
|
const [, key, value] = dataAttrMatch;
|
|
6065
6230
|
const datasetKey = key.replace(DATASET_KEY_RE$1, (_match, char) => char.toUpperCase());
|
|
6066
|
-
return scope.dataset
|
|
6231
|
+
return scope.dataset != null && Object.hasOwn(scope.dataset, datasetKey) && String(scope.dataset[datasetKey] ?? "") === value;
|
|
6067
6232
|
}
|
|
6068
6233
|
return scope.alias === part;
|
|
6069
6234
|
});
|
|
@@ -6701,9 +6866,15 @@ var BrowserHeadlessSession = class {
|
|
|
6701
6866
|
this.appLifecycle.triggerAppShow(this.appInstance, launchOptions);
|
|
6702
6867
|
return this.appInstance;
|
|
6703
6868
|
}
|
|
6869
|
+
bootstrapNavigation(launchOptions = createAppLaunchOptions("", {})) {
|
|
6870
|
+
const isLaunching = !this.appInstance;
|
|
6871
|
+
this.bootstrap(launchOptions);
|
|
6872
|
+
return isLaunching ? this.currentPageInstance : null;
|
|
6873
|
+
}
|
|
6704
6874
|
reLaunch(url) {
|
|
6705
6875
|
const target = this.resolveNavigationTarget(url);
|
|
6706
|
-
this.
|
|
6876
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
6877
|
+
if (launchedPage) return launchedPage;
|
|
6707
6878
|
this.unloadAllPages();
|
|
6708
6879
|
const pageInstance = this.createFreshPage(target);
|
|
6709
6880
|
this.pages.push(pageInstance);
|
|
@@ -6713,7 +6884,8 @@ var BrowserHeadlessSession = class {
|
|
|
6713
6884
|
}
|
|
6714
6885
|
navigateTo(url) {
|
|
6715
6886
|
const target = this.resolveNavigationTarget(url);
|
|
6716
|
-
this.
|
|
6887
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
6888
|
+
if (launchedPage) return launchedPage;
|
|
6717
6889
|
if (this.pages.length >= PAGE_STACK_LIMIT$1) throw new Error(`Cannot navigateTo() beyond a ${PAGE_STACK_LIMIT$1}-page stack in browser simulator runtime.`);
|
|
6718
6890
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.navigateTo() cannot open a tabBar page in browser simulator runtime: ${url}`);
|
|
6719
6891
|
this.currentPageInstance?.onHide?.();
|
|
@@ -6726,7 +6898,8 @@ var BrowserHeadlessSession = class {
|
|
|
6726
6898
|
}
|
|
6727
6899
|
redirectTo(url) {
|
|
6728
6900
|
const target = this.resolveNavigationTarget(url);
|
|
6729
|
-
this.
|
|
6901
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
6902
|
+
if (launchedPage) return launchedPage;
|
|
6730
6903
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.redirectTo() cannot open a tabBar page in browser simulator runtime: ${url}`);
|
|
6731
6904
|
const current = this.currentPageInstance;
|
|
6732
6905
|
if (current) {
|
|
@@ -6740,7 +6913,8 @@ var BrowserHeadlessSession = class {
|
|
|
6740
6913
|
return pageInstance;
|
|
6741
6914
|
}
|
|
6742
6915
|
navigateBack(delta = 1) {
|
|
6743
|
-
this.
|
|
6916
|
+
const launchedPage = this.bootstrapNavigation();
|
|
6917
|
+
if (launchedPage) return launchedPage;
|
|
6744
6918
|
if (this.pages.length <= 1) return this.currentPageInstance;
|
|
6745
6919
|
const removableCount = Math.min(Number.isFinite(delta) ? Math.max(1, Math.trunc(delta)) : 1, this.pages.length - 1);
|
|
6746
6920
|
const removedPages = this.pages.splice(this.pages.length - removableCount, removableCount);
|
|
@@ -6759,7 +6933,7 @@ var BrowserHeadlessSession = class {
|
|
|
6759
6933
|
const target = this.resolveNavigationTarget(url);
|
|
6760
6934
|
if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in browser simulator runtime: ${url}`);
|
|
6761
6935
|
if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in browser simulator runtime: ${url}`);
|
|
6762
|
-
this.
|
|
6936
|
+
if (this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query))) return () => () => {};
|
|
6763
6937
|
return () => this.commitSwitchTab(target);
|
|
6764
6938
|
}
|
|
6765
6939
|
commitSwitchTab(target) {
|
|
@@ -6925,8 +7099,8 @@ var BrowserHeadlessSession = class {
|
|
|
6925
7099
|
});
|
|
6926
7100
|
pageInstance.createIntersectionObserver = (options) => this.createIntersectionObserver(pageInstance, options);
|
|
6927
7101
|
pageInstance.createMediaQueryObserver = () => this.createMediaQueryObserver(pageInstance);
|
|
6928
|
-
pageInstance.selectComponent = (selector) => this.selectComponent(selector);
|
|
6929
|
-
pageInstance.selectAllComponents = (selector) => this.selectAllComponents(selector);
|
|
7102
|
+
pageInstance.selectComponent = (selector) => resolveNativeComponentSelection(this.selectComponent(selector));
|
|
7103
|
+
pageInstance.selectAllComponents = (selector) => this.selectAllComponents(selector).map(resolveNativeComponentSelection);
|
|
6930
7104
|
pageInstance.getTabBar = () => {
|
|
6931
7105
|
if (this.currentPageInstance === pageInstance) this.renderCurrentPage();
|
|
6932
7106
|
return this.componentCache.get(customTabBarScopeId(pageInstance.route)) ?? null;
|
|
@@ -7014,7 +7188,7 @@ var BrowserHeadlessSession = class {
|
|
|
7014
7188
|
runPageComponentLifetime(route, lifetimeName, payload) {
|
|
7015
7189
|
const prefix = `page:${stripLeadingSlash$1(route)}`;
|
|
7016
7190
|
for (const [scopeId, instance] of this.componentCache.entries()) {
|
|
7017
|
-
if (!scopeId.startsWith(prefix)) continue;
|
|
7191
|
+
if (!scopeId.startsWith(prefix) || scopeId === customTabBarScopeId(route)) continue;
|
|
7018
7192
|
runComponentPageLifetime(instance, lifetimeName, payload);
|
|
7019
7193
|
}
|
|
7020
7194
|
}
|
|
@@ -7268,7 +7442,6 @@ function getRuntimeWxsLoader(owner, artifactSource) {
|
|
|
7268
7442
|
//#endregion
|
|
7269
7443
|
//#region src/runtime/render/shared.ts
|
|
7270
7444
|
const TEMPLATE_INTERPOLATION_RE = /\{\{([^{}]+)\}\}/g;
|
|
7271
|
-
const DATASET_NAME_RE = /-([a-z])/g;
|
|
7272
7445
|
const LEADING_SLASH_RE$2 = /^\/+/;
|
|
7273
7446
|
const EVENT_BINDING_ATTRS = [
|
|
7274
7447
|
"bindtap",
|
|
@@ -7291,15 +7464,12 @@ function isMustacheOnly(value) {
|
|
|
7291
7464
|
const trimmed = value.trim();
|
|
7292
7465
|
return trimmed.startsWith("{{") && trimmed.endsWith("}}") && !trimmed.includes("{{", 2);
|
|
7293
7466
|
}
|
|
7294
|
-
function toDatasetKey(attributeName) {
|
|
7295
|
-
return attributeName.slice(5).replace(DATASET_NAME_RE, (_match, char) => char.toUpperCase());
|
|
7296
|
-
}
|
|
7297
7467
|
function collectDataset(node, source) {
|
|
7468
|
+
if (node.dataset) return collectNodeDataset(node);
|
|
7298
7469
|
const dataset = {};
|
|
7299
7470
|
for (const [key, value] of Object.entries(node.attribs ?? {})) {
|
|
7300
|
-
if (!
|
|
7301
|
-
|
|
7302
|
-
dataset[toDatasetKey(key)] = String(resolvedValue ?? "");
|
|
7471
|
+
if (!isDatasetAttribute(key)) continue;
|
|
7472
|
+
dataset[toDatasetKey(key)] = source && isMustacheOnly(value) ? resolveTemplateExpression(source, value) : value;
|
|
7303
7473
|
}
|
|
7304
7474
|
return dataset;
|
|
7305
7475
|
}
|
|
@@ -7313,6 +7483,7 @@ function cloneNode(node) {
|
|
|
7313
7483
|
return {
|
|
7314
7484
|
...node,
|
|
7315
7485
|
attribs: node.attribs ? { ...node.attribs } : void 0,
|
|
7486
|
+
dataset: node.dataset ? { ...node.dataset } : void 0,
|
|
7316
7487
|
children: node.children?.map((child) => cloneNode(child))
|
|
7317
7488
|
};
|
|
7318
7489
|
}
|
|
@@ -7364,7 +7535,7 @@ function isTagNode(node) {
|
|
|
7364
7535
|
function resolveAttributeValue(value, scope) {
|
|
7365
7536
|
if (isMustacheOnly(value)) {
|
|
7366
7537
|
const expression = value.trim().slice(2, -2);
|
|
7367
|
-
return
|
|
7538
|
+
return resolveRawValueByPath(wxsScopeData(scope), expression);
|
|
7368
7539
|
}
|
|
7369
7540
|
return interpolateTemplate(value, wxsScopeData(scope));
|
|
7370
7541
|
}
|
|
@@ -7383,13 +7554,20 @@ function applyNodeBindings(node, scope) {
|
|
|
7383
7554
|
node.attribs ??= {};
|
|
7384
7555
|
node.attribs["data-sim-scope"] = scope.getScopeId();
|
|
7385
7556
|
for (const key of STRUCTURAL_ATTRS) delete node.attribs[key];
|
|
7557
|
+
let dataset;
|
|
7386
7558
|
for (const [key, value] of Object.entries({ ...node.attribs })) {
|
|
7387
7559
|
if (EVENT_BINDING_ATTRS.includes(key)) {
|
|
7388
7560
|
node.attribs["data-sim-tap"] = value;
|
|
7389
7561
|
continue;
|
|
7390
7562
|
}
|
|
7391
|
-
|
|
7563
|
+
const resolvedValue = typeof value === "string" ? resolveAttributeValue(value, scope) : value;
|
|
7564
|
+
node.attribs[key] = String(resolvedValue ?? "");
|
|
7565
|
+
if (isDatasetAttribute(key)) {
|
|
7566
|
+
dataset ??= {};
|
|
7567
|
+
dataset[toDatasetKey(key)] = resolvedValue;
|
|
7568
|
+
}
|
|
7392
7569
|
}
|
|
7570
|
+
node.dataset = dataset;
|
|
7393
7571
|
}
|
|
7394
7572
|
function evaluateConditionalBranch(node, scope) {
|
|
7395
7573
|
const condition = node.attribs?.["wx:if"] ?? node.attribs?.["wx:elif"];
|
|
@@ -7563,7 +7741,7 @@ function resolveComponentProperties(clonedNode, scope, definition) {
|
|
|
7563
7741
|
bindingExpressions
|
|
7564
7742
|
};
|
|
7565
7743
|
}
|
|
7566
|
-
function createRuntimeComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId) {
|
|
7744
|
+
function createRuntimeComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, beforeCreated) {
|
|
7567
7745
|
const componentProperties = Object.keys(componentEntry.definition.methods ?? {}).some((key) => key.startsWith("__weapp_vite_")) || Object.hasOwn(componentEntry.definition.properties ?? {}, "__wvSlotOwnerId") ? Object.fromEntries(Object.keys(componentEntry.definition.properties ?? {}).filter((key) => Object.hasOwn(nextProperties, key)).map((key) => [key, nextProperties[key]])) : nextProperties;
|
|
7568
7746
|
const componentInstance = createComponentInstance({
|
|
7569
7747
|
definition: componentEntry.definition,
|
|
@@ -7575,10 +7753,11 @@ function createRuntimeComponentInstance(componentScopeId, context, clonedNode, c
|
|
|
7575
7753
|
componentInstance.createIntersectionObserver = (options) => context.session.createIntersectionObserver(componentInstance, options);
|
|
7576
7754
|
componentInstance.createMediaQueryObserver = () => context.session.createMediaQueryObserver(componentInstance);
|
|
7577
7755
|
componentInstance.createSelectorQuery = () => context.moduleLoader.wx.createSelectorQuery().in(componentInstance);
|
|
7578
|
-
componentInstance.selectComponent = (selector) => context.session.selectComponentWithin(componentScopeId, selector);
|
|
7579
|
-
componentInstance.selectAllComponents = (selector) => context.session.selectAllComponentsWithin(componentScopeId, selector);
|
|
7756
|
+
componentInstance.selectComponent = (selector) => resolveNativeComponentSelection(context.session.selectComponentWithin(componentScopeId, selector));
|
|
7757
|
+
componentInstance.selectAllComponents = (selector) => context.session.selectAllComponentsWithin(componentScopeId, selector).map(resolveNativeComponentSelection);
|
|
7580
7758
|
componentInstance.selectOwnerComponent = () => ownerScopeId ? context.componentCache.get(ownerScopeId) ?? null : null;
|
|
7581
7759
|
context.componentCache.set(componentScopeId, componentInstance);
|
|
7760
|
+
beforeCreated?.(componentInstance);
|
|
7582
7761
|
runComponentLifecycle(componentInstance, "created");
|
|
7583
7762
|
componentInstance.__propertySnapshots = Object.fromEntries(Object.entries(componentInstance.properties).map(([key, propertyValue]) => [key, cloneValue(propertyValue)]));
|
|
7584
7763
|
syncComponentProperties(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, componentProperties, {}, []);
|
|
@@ -7714,12 +7893,16 @@ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, inst
|
|
|
7714
7893
|
const componentScopeId = `${instancePath}/${clonedNode.name}`;
|
|
7715
7894
|
const ownerScopeId = scope.getScopeId().includes("/") ? scope.getScopeId() : void 0;
|
|
7716
7895
|
const { nextProperties, bindingExpressions } = resolveComponentProperties(clonedNode, scope, componentEntry.definition);
|
|
7896
|
+
const genericComponents = resolveComponentGenerics(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
7897
|
+
const slots = collectComponentSlots(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
7717
7898
|
let componentInstance = context.componentCache.get(componentScopeId);
|
|
7718
|
-
if (!componentInstance) componentInstance = createRuntimeComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId)
|
|
7899
|
+
if (!componentInstance) componentInstance = createRuntimeComponentInstance(componentScopeId, context, clonedNode, componentEntry, nextProperties, ownerScopeId, (instance) => {
|
|
7900
|
+
const initialScope = createComponentScope(clonedNode, scope, componentScopeId, instance, genericComponents, slots);
|
|
7901
|
+
context.componentScopes.set(componentScopeId, initialScope);
|
|
7902
|
+
renderRuntimeComponentTemplate(context, componentEntry, renderNodeTree, initialScope, componentScopeId, /* @__PURE__ */ new Set());
|
|
7903
|
+
});
|
|
7719
7904
|
else syncComponentProperties(componentInstance, componentInstance.__definition__ ?? componentEntry.definition, nextProperties, bindingExpressions, context.changedPageKeys);
|
|
7720
7905
|
seenComponentScopes.add(componentScopeId);
|
|
7721
|
-
const genericComponents = resolveComponentGenerics(context, clonedNode, ownerJsonPath, ownerFilePath, componentEntry.filePath);
|
|
7722
|
-
const slots = collectComponentSlots(clonedNode, scope, ownerJsonPath, ownerFilePath, componentScopeId, templateRenderState);
|
|
7723
7906
|
const componentScope = createComponentScope(clonedNode, scope, componentScopeId, componentInstance, genericComponents, slots);
|
|
7724
7907
|
context.componentScopes.set(componentScopeId, componentScope);
|
|
7725
7908
|
const renderedComponentRoot = renderRuntimeComponentTemplate(context, componentEntry, renderNodeTree, componentScope, componentScopeId, seenComponentScopes);
|
|
@@ -7729,6 +7912,10 @@ function renderNodeTree(node, scope, context, ownerJsonPath, ownerFilePath, inst
|
|
|
7729
7912
|
...clonedNode.attribs,
|
|
7730
7913
|
...renderedComponentRoot.attribs
|
|
7731
7914
|
};
|
|
7915
|
+
if (clonedNode.dataset || renderedComponentRoot.dataset) renderedComponentRoot.dataset = {
|
|
7916
|
+
...clonedNode.dataset,
|
|
7917
|
+
...renderedComponentRoot.dataset
|
|
7918
|
+
};
|
|
7732
7919
|
renderedComponentRoot.attribs["data-sim-component"] = clonedNode.name;
|
|
7733
7920
|
renderedComponentRoot.attribs["data-sim-node"] = instancePath;
|
|
7734
7921
|
renderedComponentRoot.attribs["data-sim-scope"] = componentScopeId;
|
|
@@ -7772,7 +7959,8 @@ function renderRuntimePageTree(context, page) {
|
|
|
7772
7959
|
const pageAttaching = isComponentPageAttaching(page);
|
|
7773
7960
|
const attached = !pageAttaching && flushComponentAttachments([...seenComponentScopes].map((scopeId) => context.componentCache.get(scopeId)), (instance) => runComponentLifecycle(instance, "attached"));
|
|
7774
7961
|
const removed = [...context.componentCache].filter(([scopeId]) => scopeId.startsWith(`${pageScopeId}/`) && !seenComponentScopes.has(scopeId));
|
|
7775
|
-
for (const [, instance] of removed) runComponentLifecycle(instance, "detached");
|
|
7962
|
+
for (const [scopeId, instance] of removed) if (isComponentAttached(instance)) runComponentLifecycle(instance, "detached");
|
|
7963
|
+
else scheduleDiscardedComponentReady(context.componentCache, scopeId, instance);
|
|
7776
7964
|
const instances = [...seenComponentScopes].map((scopeId) => context.componentCache.get(scopeId));
|
|
7777
7965
|
const relationsChanged = !pageAttaching && !hasPendingComponentAttachments(instances) && syncComponentRelations(context.componentCache, seenComponentScopes, pageScopeId);
|
|
7778
7966
|
for (const [scopeId] of removed) {
|
|
@@ -7780,9 +7968,10 @@ function renderRuntimePageTree(context, page) {
|
|
|
7780
7968
|
context.componentScopes.delete(scopeId);
|
|
7781
7969
|
}
|
|
7782
7970
|
if (pageAttached || attached || relationsChanged) return renderRuntimePageTree(context, page);
|
|
7971
|
+
if (!isPageBeforeReady(page)) flushDiscardedComponentReady(context.componentCache, pageScopeId, (instance) => runComponentLifecycle(instance, "ready"));
|
|
7783
7972
|
if (!isPageBeforeReady(page) && flushComponentReady(instances, (instance) => {
|
|
7784
7973
|
runComponentLifecycle(instance, "ready");
|
|
7785
|
-
instance.__definition__?.pageLifetimes?.show?.call(instance);
|
|
7974
|
+
if (instance !== context.componentCache.get(customTabBarScopeId(route))) instance.__definition__?.pageLifetimes?.show?.call(instance);
|
|
7786
7975
|
})) return renderRuntimePageTree(context, page);
|
|
7787
7976
|
const treeRoot = roots.length > 1 ? {
|
|
7788
7977
|
type: "root",
|
|
@@ -8062,7 +8251,7 @@ function matchesComponentSelector(scope, selector) {
|
|
|
8062
8251
|
if (dataAttrMatch) {
|
|
8063
8252
|
const [, key, value] = dataAttrMatch;
|
|
8064
8253
|
const datasetKey = key.replace(DATASET_KEY_RE, (_match, char) => char.toUpperCase());
|
|
8065
|
-
return scope.dataset
|
|
8254
|
+
return scope.dataset != null && Object.hasOwn(scope.dataset, datasetKey) && String(scope.dataset[datasetKey] ?? "") === value;
|
|
8066
8255
|
}
|
|
8067
8256
|
return scope.alias === part;
|
|
8068
8257
|
});
|
|
@@ -8676,9 +8865,15 @@ var HeadlessSession = class {
|
|
|
8676
8865
|
};
|
|
8677
8866
|
if (Object.hasOwn(runtimeApp, "routes")) this.appInstance.routes = runtimeApp.routes;
|
|
8678
8867
|
}
|
|
8868
|
+
bootstrapNavigation(launchOptions = createAppLaunchOptions("", {})) {
|
|
8869
|
+
const isLaunching = !this.appInstance;
|
|
8870
|
+
this.bootstrap(launchOptions);
|
|
8871
|
+
return isLaunching ? this.currentPageInstance : null;
|
|
8872
|
+
}
|
|
8679
8873
|
reLaunch(url) {
|
|
8680
8874
|
const target = this.resolveNavigationTarget(url);
|
|
8681
|
-
this.
|
|
8875
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
8876
|
+
if (launchedPage) return launchedPage;
|
|
8682
8877
|
this.unloadAllPages();
|
|
8683
8878
|
const pageInstance = this.createFreshPage(target);
|
|
8684
8879
|
this.pages.push(pageInstance);
|
|
@@ -8688,7 +8883,8 @@ var HeadlessSession = class {
|
|
|
8688
8883
|
}
|
|
8689
8884
|
navigateTo(url) {
|
|
8690
8885
|
const target = this.resolveNavigationTarget(url);
|
|
8691
|
-
this.
|
|
8886
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
8887
|
+
if (launchedPage) return launchedPage;
|
|
8692
8888
|
if (this.pages.length >= PAGE_STACK_LIMIT) throw new Error(`Cannot navigateTo() beyond a ${PAGE_STACK_LIMIT}-page stack in headless runtime.`);
|
|
8693
8889
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.navigateTo() cannot open a tabBar page in headless runtime: ${url}`);
|
|
8694
8890
|
this.currentPageInstance?.onHide?.();
|
|
@@ -8701,7 +8897,8 @@ var HeadlessSession = class {
|
|
|
8701
8897
|
}
|
|
8702
8898
|
redirectTo(url) {
|
|
8703
8899
|
const target = this.resolveNavigationTarget(url);
|
|
8704
|
-
this.
|
|
8900
|
+
const launchedPage = this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
8901
|
+
if (launchedPage) return launchedPage;
|
|
8705
8902
|
if (this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.redirectTo() cannot open a tabBar page in headless runtime: ${url}`);
|
|
8706
8903
|
const current = this.currentPageInstance;
|
|
8707
8904
|
if (current) {
|
|
@@ -8715,7 +8912,8 @@ var HeadlessSession = class {
|
|
|
8715
8912
|
return pageInstance;
|
|
8716
8913
|
}
|
|
8717
8914
|
navigateBack(delta = 1) {
|
|
8718
|
-
this.
|
|
8915
|
+
const launchedPage = this.bootstrapNavigation();
|
|
8916
|
+
if (launchedPage) return launchedPage;
|
|
8719
8917
|
if (this.pages.length <= 1) return this.currentPageInstance;
|
|
8720
8918
|
const removableCount = Math.min(Number.isFinite(delta) ? Math.max(1, Math.trunc(delta)) : 1, this.pages.length - 1);
|
|
8721
8919
|
const removedPages = this.pages.splice(this.pages.length - removableCount, removableCount);
|
|
@@ -8734,7 +8932,7 @@ var HeadlessSession = class {
|
|
|
8734
8932
|
const target = this.resolveNavigationTarget(url);
|
|
8735
8933
|
if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in headless runtime: ${url}`);
|
|
8736
8934
|
if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in headless runtime: ${url}`);
|
|
8737
|
-
this.
|
|
8935
|
+
if (this.bootstrapNavigation(createAppLaunchOptions(target.normalizedRoute, target.query))) return () => () => {};
|
|
8738
8936
|
return () => this.commitSwitchTab(target);
|
|
8739
8937
|
}
|
|
8740
8938
|
commitSwitchTab(target) {
|
|
@@ -8871,8 +9069,8 @@ var HeadlessSession = class {
|
|
|
8871
9069
|
});
|
|
8872
9070
|
pageInstance.createIntersectionObserver = (options) => this.createIntersectionObserver(pageInstance, options);
|
|
8873
9071
|
pageInstance.createMediaQueryObserver = () => this.createMediaQueryObserver(pageInstance);
|
|
8874
|
-
pageInstance.selectComponent = (selector) => this.selectComponent(selector);
|
|
8875
|
-
pageInstance.selectAllComponents = (selector) => this.selectAllComponents(selector);
|
|
9072
|
+
pageInstance.selectComponent = (selector) => resolveNativeComponentSelection(this.selectComponent(selector));
|
|
9073
|
+
pageInstance.selectAllComponents = (selector) => this.selectAllComponents(selector).map(resolveNativeComponentSelection);
|
|
8876
9074
|
pageInstance.getTabBar = () => {
|
|
8877
9075
|
if (this.currentPageInstance === pageInstance) this.renderCurrentPage();
|
|
8878
9076
|
return this.componentCache.get(customTabBarScopeId(pageInstance.route)) ?? null;
|
|
@@ -8980,7 +9178,7 @@ var HeadlessSession = class {
|
|
|
8980
9178
|
runPageComponentLifetime(route, lifetimeName, payload) {
|
|
8981
9179
|
const prefix = `page:${stripLeadingSlash(route)}`;
|
|
8982
9180
|
for (const [scopeId, instance] of this.componentCache.entries()) {
|
|
8983
|
-
if (!scopeId.startsWith(prefix)) continue;
|
|
9181
|
+
if (!scopeId.startsWith(prefix) || scopeId === customTabBarScopeId(route)) continue;
|
|
8984
9182
|
instance.__definition__?.pageLifetimes?.[lifetimeName]?.call(instance, payload);
|
|
8985
9183
|
}
|
|
8986
9184
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mpcore/simulator",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
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",
|
|
8
|
+
"homepage": "https://github.com/weapp-vite/weapp-vite#readme",
|
|
8
9
|
"repository": {
|
|
9
10
|
"type": "git",
|
|
10
11
|
"url": "git+https://github.com/weapp-vite/weapp-vite.git",
|
|
@@ -14,11 +15,15 @@
|
|
|
14
15
|
"url": "https://github.com/weapp-vite/weapp-vite/issues"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [
|
|
18
|
+
"weapp-vite",
|
|
17
19
|
"weapp",
|
|
20
|
+
"wechat",
|
|
18
21
|
"mini-program",
|
|
22
|
+
"miniprogram",
|
|
19
23
|
"runtime",
|
|
20
24
|
"headless",
|
|
21
|
-
"testing"
|
|
25
|
+
"testing",
|
|
26
|
+
"vite"
|
|
22
27
|
],
|
|
23
28
|
"sideEffects": false,
|
|
24
29
|
"exports": {
|
|
@@ -38,10 +43,10 @@
|
|
|
38
43
|
"node": "^20.19.0 || >=22.12.0"
|
|
39
44
|
},
|
|
40
45
|
"dependencies": {
|
|
41
|
-
"@babel/parser": "^8.0.
|
|
42
|
-
"@weapp-core/constants": "0.2.
|
|
43
|
-
"css-select": "^
|
|
44
|
-
"css-what": "^
|
|
46
|
+
"@babel/parser": "^8.0.5",
|
|
47
|
+
"@weapp-core/constants": "0.2.4",
|
|
48
|
+
"css-select": "^7.0.0",
|
|
49
|
+
"css-what": "^8.0.0",
|
|
45
50
|
"fontoxpath": "^3.34.0",
|
|
46
51
|
"htmlparser2": "^12.0.0",
|
|
47
52
|
"pathe": "^2.0.3",
|
|
@@ -57,6 +62,7 @@
|
|
|
57
62
|
"test": "vitest run",
|
|
58
63
|
"test:all": "pnpm test && pnpm test:types && pnpm test:e2e",
|
|
59
64
|
"test:dev": "vitest",
|
|
65
|
+
"test:coverage": "vitest run --coverage.enabled=true",
|
|
60
66
|
"test:e2e": "vitest run -c ./vitest.e2e.config.ts",
|
|
61
67
|
"test:types": "pnpm build && tsd --files \"test-d/**/*.test-d.ts\"",
|
|
62
68
|
"typecheck": "tsc --noEmit"
|