Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/register/src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion packages/register/src/entryHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -79,6 +79,9 @@ export const defineEntry = (registry) => {
if (!registry) {
throw new Error('请传递正确的注册表')
}

preprocessRegistry(registry)

Comment thread
gene9831 marked this conversation as resolved.
lowcodeRegistry.registry = registry
generateRegistry(registry)
}
Expand Down
60 changes: 60 additions & 0 deletions packages/register/test/preprocessRegistry.test.js
Original file line number Diff line number Diff line change
@@ -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: []
})
})
})