From c44262e3cd3146e045d6ac93522abe5f040baf73 Mon Sep 17 00:00:00 2001 From: Malash Date: Thu, 24 Nov 2022 13:32:49 +0800 Subject: [PATCH] refactor fields in template & json data --- packages/core/src/__tests__/helpers/index.ts | 6 +- packages/core/src/__tests__/updates.test.tsx | 12 +-- .../__snapshots__/wechat.test.tsx.snap | 26 ++--- packages/core/src/constants.ts | 33 +++++++ packages/core/src/container.tsx | 6 +- packages/core/src/index.ts | 2 + .../universal/__tests__/hooks.test.tsx | 12 +-- .../core/src/portal/__tests__/index.test.tsx | 23 +++-- .../reconciler/__tests__/instance.test.tsx | 23 +++-- packages/core/src/reconciler/instance.ts | 71 +++++++++----- packages/core/src/utils/diff.ts | 4 +- packages/webpack-plugin/src/index.ts | 3 +- .../src/plugins/__tests__/based.test.ts | 1 + packages/webpack-plugin/src/plugins/bridge.ts | 6 +- .../__tests__/nativeComponentJson.test.ts | 4 +- .../commons/__tests__/wxmlElement.test.ts | 55 +++++++++-- .../src/templates/commons/wrapped.ts | 35 ++++--- .../src/templates/commons/wxmlElement.ts | 10 +- .../__snapshots__/children.wxml.test.tsx.snap | 6 +- .../__tests__/children.wxml.test.tsx | 8 +- .../__tests__/components.wxml.test.tsx | 83 ++++++++++------ .../src/templates/components/children.wxml.ts | 20 ++-- .../components/components.wxml/flatten.tsx | 95 ++++++++++--------- .../components/components.wxml/index.tsx | 35 ++++--- .../src/templates/components/item.wxml.ts | 20 ++-- .../src/templates/components/subtree.wxml.ts | 11 ++- .../src/templates/components/wrapped.wxml.ts | 8 +- .../src/templates/helpers/context.ts | 1 + .../src/templates/helpers/ids.ts | 7 ++ .../webpack-plugin/src/templates/index.ts | 2 +- packages/webpack-plugin/src/types/index.ts | 2 + 31 files changed, 398 insertions(+), 232 deletions(-) create mode 100644 packages/webpack-plugin/src/templates/helpers/ids.ts diff --git a/packages/core/src/__tests__/helpers/index.ts b/packages/core/src/__tests__/helpers/index.ts index d6842192..72c446aa 100644 --- a/packages/core/src/__tests__/helpers/index.ts +++ b/packages/core/src/__tests__/helpers/index.ts @@ -1,11 +1,10 @@ import { ReactElement } from 'react'; import { TestingAdaptor, TestingAdaptorInstance } from './adaptor'; -import { ElementNode } from '../../reconciler/instance'; export class RenderResult { public constructor(private adaptorInstance: TestingAdaptorInstance) {} - public getContainer = (): ElementNode => this.adaptorInstance.data; + public getContainer = () => this.adaptorInstance.data; // ByText @@ -21,7 +20,8 @@ export class RenderResult { // TODO: }; - public resolveUpdateCallback = (renderId?: string) => this.adaptorInstance.resolveUpdateCallback(renderId); + public resolveUpdateCallback = (renderId?: string) => + this.adaptorInstance.resolveUpdateCallback(renderId); public setManuallyResolvedUpdateCallback = (enabled: boolean) => { this.adaptorInstance.setManuallyResolvedUpdateCallback(enabled); diff --git a/packages/core/src/__tests__/updates.test.tsx b/packages/core/src/__tests__/updates.test.tsx index 721b4368..c0fc50d1 100644 --- a/packages/core/src/__tests__/updates.test.tsx +++ b/packages/core/src/__tests__/updates.test.tsx @@ -35,7 +35,7 @@ describe('updates', () => { adaptor.run(); updater(3); - expect(diff).toEqual({ 'c[0].c[0].text': '3' }); + expect(diff).toEqual({ 'meta.children[0].children[0].text': '3' }); }); it('create or remove instance, re-render the whole sub-tree', () => { @@ -55,13 +55,13 @@ describe('updates', () => { adaptor.run(); updater(true); - expect(Object.keys(diff)).toEqual(['c[0].c[1]']); - expect(diff['c[0].c[1]'].c.length).toEqual(1); + expect(Object.keys(diff)).toEqual(['meta.children[0].children[1]']); + expect(diff['meta.children[0].children[1]'].children.length).toEqual(1); updater(false); - expect(Object.keys(diff)).toEqual(['c[0].c[1]']); - expect(diff['c[0].c[1]'].c.length).toEqual(0); + expect(Object.keys(diff)).toEqual(['meta.children[0].children[1]']); + expect(diff['meta.children[0].children[1]'].children.length).toEqual(0); }); it('only update events, should not trigger setData', () => { @@ -102,6 +102,6 @@ describe('updates', () => { // @ts-expect-error updater(false); - expect(Object.keys(diff)).toEqual(['c[0].sid']); + expect(Object.keys(diff)).toEqual(['meta.children[0].simplifiedId']); }); }); diff --git a/packages/core/src/adaptor/__tests__/__snapshots__/wechat.test.tsx.snap b/packages/core/src/adaptor/__tests__/__snapshots__/wechat.test.tsx.snap index 341e5e6d..8a70ae50 100644 --- a/packages/core/src/adaptor/__tests__/__snapshots__/wechat.test.tsx.snap +++ b/packages/core/src/adaptor/__tests__/__snapshots__/wechat.test.tsx.snap @@ -2,17 +2,19 @@ exports[`WeChatAdaptor works with Page 1`] = ` Object { - "c": Array [ - Object { - "c": Array [], - "id": 2, - "props": Object {}, - "sid": 0, - "type": "view", - }, - ], - "id": 1, - "props": Object {}, - "type": "GOJI_VIRTUAL_ROOT", + "meta": Object { + "children": Array [ + Object { + "children": Array [], + "gojiId": 2, + "props": Object {}, + "simplifiedId": 0, + "type": "view", + }, + ], + "gojiId": 1, + "props": Object {}, + "type": "GOJI_VIRTUAL_ROOT", + }, } `; diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 0f489535..e628ee8e 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -19,3 +19,36 @@ export const SIMPLIFY_COMPONENTS: Array = [ events: [], }, ]; + +/** + * Get identifiers used by template. For production we use shorter identifiers for less bundle size. + * This function is shared by both `@goji/core` and `@goji/webpack-plugin`. + * + * meta: for element data source, e.g. `