diff --git a/packages/register/src/common.js b/packages/register/src/common.js index eee89b0e44..2b733b3397 100644 --- a/packages/register/src/common.js +++ b/packages/register/src/common.js @@ -142,6 +142,23 @@ const handleRegistryProp = (id, value) => { } } +export const preprocessRegistry = (registry) => { + // 元应用支持使用长度为2的数组来配置,第一个参数为元应用,第二个参数是额外的自定义配置。此函数判断数组是否属于这种配置格式 + const isArrayFormat = (arr) => Array.isArray(arr) && arr.length === 2 && arr[0].id + + 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) => { + 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) } diff --git a/packages/register/test/preprocessRegistry.test.js b/packages/register/test/preprocessRegistry.test.js new file mode 100644 index 0000000000..84c41da02d --- /dev/null +++ b/packages/register/test/preprocessRegistry.test.js @@ -0,0 +1,60 @@ +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 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: [] + }) + }) +})