From 863fdccb40531d53a3c4c16d7d4e6476b128eaeb Mon Sep 17 00:00:00 2001 From: gene9831 Date: Tue, 2 Jul 2024 23:46:30 -0700 Subject: [PATCH 1/5] refactor: metapp in registry support configuration in array format --- packages/register/src/common.js | 24 ++++++++++++++++++++++++ packages/register/src/entryHash.js | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/register/src/common.js b/packages/register/src/common.js index eee89b0e44..ba62ee25aa 100644 --- a/packages/register/src/common.js +++ b/packages/register/src/common.js @@ -142,6 +142,30 @@ const handleRegistryProp = (id, value) => { } } +export const preprocessRegistry = (registry) => { + // 元应用支持使用长度为2的数组来配置,第一个参数为元应用,第二个参数是额外的自定义配置。此函数判断数组是否属于这种配置格式 + const isArrayFormat = (arr) => arr.length === 2 && arr[0].id && !arr[1].id + + Object.entries(registry) + .filter(([, metaApps]) => Array.isArray(metaApps)) + .forEach(([type, metaApps]) => { + // normal: { layout: Layout } + // array format: { layout: [ Layout, { options: extraOptions } ] } + if (isArrayFormat(metaApps)) { + registry[type] = { ...metaApps[0], ...metaApps[1] } + return + } + + // normal: { plugins: [ Page, Block, ... ] } + // array format: { plugins: [ [ Page, { options: extraOptions } ], Block, ... ] } + metaApps.forEach((metaApp, index) => { + if (isArrayFormat(metaApp)) { + metaApps.splice(index, 1, { ...metaApp[0], ...metaApp[1] }) + } + }) + }) +} + export const generateRegistry = (registry) => { Object.entries(registry).forEach(([key, value]) => { if (typeof value === 'object' && value) { diff --git a/packages/register/src/entryHash.js b/packages/register/src/entryHash.js index 7f969b6801..181696bc75 100644 --- a/packages/register/src/entryHash.js +++ b/packages/register/src/entryHash.js @@ -12,7 +12,7 @@ import { merge } from 'lodash-es' import { utils } from '@opentiny/tiny-engine-utils' -import { generateRegistry, entryHashMap } from './common' +import { generateRegistry, entryHashMap, preprocessRegistry } from './common' const lowcodeRegistry = { registry: null } @@ -79,6 +79,9 @@ export const defineEntry = (registry) => { if (!registry) { throw new Error('请传递正确的注册表') } + + preprocessRegistry(registry) + lowcodeRegistry.registry = registry generateRegistry(registry) } From 5d3766fa6057078405d921e1bac584f0806ed908 Mon Sep 17 00:00:00 2001 From: gene9831 Date: Wed, 3 Jul 2024 00:09:18 -0700 Subject: [PATCH 2/5] feat: add unit tests for preprocessRegistry function --- .../register/test/preprocessRegistry.test.js | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 packages/register/test/preprocessRegistry.test.js diff --git a/packages/register/test/preprocessRegistry.test.js b/packages/register/test/preprocessRegistry.test.js new file mode 100644 index 0000000000..35677cdec0 --- /dev/null +++ b/packages/register/test/preprocessRegistry.test.js @@ -0,0 +1,75 @@ +import { describe, expect, it } from 'vitest' +import { preprocessRegistry } from '../src/common' + +describe('preprocessRegistry', () => { + it('should handle normal configuration without array format', () => { + const registry = { + layout: { id: 'layout1' }, + plugins: [{ id: 'plugin1' }, { id: 'plugin2' }] + } + + preprocessRegistry(registry) + + expect(registry).toEqual({ + layout: { id: 'layout1' }, + plugins: [{ id: 'plugin1' }, { id: 'plugin2' }] + }) + }) + + it('should transform array format configuration for single entry', () => { + const registry = { + layout: [{ id: 'layout1' }, { options: { extraOption: true } }] + } + + preprocessRegistry(registry) + + expect(registry).toEqual({ + layout: { + id: 'layout1', + options: { extraOption: true } + } + }) + }) + + it('should transform array format configuration for multiple entries', () => { + const registry = { + plugins: [[{ id: 'plugin1' }, { options: { extraOption1: true } }], { id: 'plugin2' }] + } + + preprocessRegistry(registry) + + expect(registry).toEqual({ + plugins: [ + { + id: 'plugin1', + options: { extraOption1: true } + }, + { id: 'plugin2' } + ] + }) + }) + + it('should not transform invalid array formats', () => { + const registry = { + layout: [{ id: 'layout1' }, { id: 'layout2' }] + } + + preprocessRegistry(registry) + + expect(registry).toEqual({ + layout: [{ id: 'layout1' }, { id: 'layout2' }] + }) + }) + + it('should handle empty arrays', () => { + const registry = { + layout: [] + } + + preprocessRegistry(registry) + + expect(registry).toEqual({ + layout: [] + }) + }) +}) From 9ea66e2457cdf57c41c26390e2ab67d043fa0155 Mon Sep 17 00:00:00 2001 From: gene9831 Date: Wed, 3 Jul 2024 19:17:11 -0700 Subject: [PATCH 3/5] feat: remove array format configuration for single entry --- packages/register/src/common.js | 13 +++---------- packages/register/test/preprocessRegistry.test.js | 15 --------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/packages/register/src/common.js b/packages/register/src/common.js index ba62ee25aa..943798b984 100644 --- a/packages/register/src/common.js +++ b/packages/register/src/common.js @@ -146,16 +146,9 @@ export const preprocessRegistry = (registry) => { // 元应用支持使用长度为2的数组来配置,第一个参数为元应用,第二个参数是额外的自定义配置。此函数判断数组是否属于这种配置格式 const isArrayFormat = (arr) => arr.length === 2 && arr[0].id && !arr[1].id - Object.entries(registry) - .filter(([, metaApps]) => Array.isArray(metaApps)) - .forEach(([type, metaApps]) => { - // normal: { layout: Layout } - // array format: { layout: [ Layout, { options: extraOptions } ] } - if (isArrayFormat(metaApps)) { - registry[type] = { ...metaApps[0], ...metaApps[1] } - return - } - + Object.values(registry) + .filter((metaApps) => Array.isArray(metaApps)) + .forEach((metaApps) => { // normal: { plugins: [ Page, Block, ... ] } // array format: { plugins: [ [ Page, { options: extraOptions } ], Block, ... ] } metaApps.forEach((metaApp, index) => { diff --git a/packages/register/test/preprocessRegistry.test.js b/packages/register/test/preprocessRegistry.test.js index 35677cdec0..84c41da02d 100644 --- a/packages/register/test/preprocessRegistry.test.js +++ b/packages/register/test/preprocessRegistry.test.js @@ -16,21 +16,6 @@ describe('preprocessRegistry', () => { }) }) - it('should transform array format configuration for single entry', () => { - const registry = { - layout: [{ id: 'layout1' }, { options: { extraOption: true } }] - } - - preprocessRegistry(registry) - - expect(registry).toEqual({ - layout: { - id: 'layout1', - options: { extraOption: true } - } - }) - }) - it('should transform array format configuration for multiple entries', () => { const registry = { plugins: [[{ id: 'plugin1' }, { options: { extraOption1: true } }], { id: 'plugin2' }] From f4b93245f22f18a4b5d8897eebf00453b91c5ad8 Mon Sep 17 00:00:00 2001 From: gene9831 Date: Wed, 3 Jul 2024 19:24:08 -0700 Subject: [PATCH 4/5] fix: remove redundant condition --- packages/register/src/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/register/src/common.js b/packages/register/src/common.js index 943798b984..27ba5349be 100644 --- a/packages/register/src/common.js +++ b/packages/register/src/common.js @@ -144,7 +144,7 @@ const handleRegistryProp = (id, value) => { export const preprocessRegistry = (registry) => { // 元应用支持使用长度为2的数组来配置,第一个参数为元应用,第二个参数是额外的自定义配置。此函数判断数组是否属于这种配置格式 - const isArrayFormat = (arr) => arr.length === 2 && arr[0].id && !arr[1].id + const isArrayFormat = (arr) => arr.length === 2 && arr[0].id Object.values(registry) .filter((metaApps) => Array.isArray(metaApps)) From 0ac3d14687774a2805d91055ed3a15020cdda4d3 Mon Sep 17 00:00:00 2001 From: gene9831 Date: Wed, 3 Jul 2024 19:59:01 -0700 Subject: [PATCH 5/5] fix: add check array condition --- packages/register/src/common.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/register/src/common.js b/packages/register/src/common.js index 27ba5349be..2b733b3397 100644 --- a/packages/register/src/common.js +++ b/packages/register/src/common.js @@ -144,7 +144,7 @@ const handleRegistryProp = (id, value) => { export const preprocessRegistry = (registry) => { // 元应用支持使用长度为2的数组来配置,第一个参数为元应用,第二个参数是额外的自定义配置。此函数判断数组是否属于这种配置格式 - const isArrayFormat = (arr) => arr.length === 2 && arr[0].id + const isArrayFormat = (arr) => Array.isArray(arr) && arr.length === 2 && arr[0].id Object.values(registry) .filter((metaApps) => Array.isArray(metaApps))