diff --git a/packages/react-renderer/src/index.ts b/packages/react-renderer/src/index.ts index 97abc4027..68b0a6944 100644 --- a/packages/react-renderer/src/index.ts +++ b/packages/react-renderer/src/index.ts @@ -1,6 +1,6 @@ export * from './api/app'; export * from './api/component'; -export { definePlugin } from './plugin'; +export { defineRendererPlugin } from './plugin'; export * from './context/render'; export * from './context/router'; diff --git a/packages/react-renderer/src/plugin.ts b/packages/react-renderer/src/plugin.ts index e4dfc440c..c0f14bfee 100644 --- a/packages/react-renderer/src/plugin.ts +++ b/packages/react-renderer/src/plugin.ts @@ -45,6 +45,6 @@ export const rendererExtends: RendererExtends = { }, }; -export function definePlugin(plugin: Plugin) { +export function defineRendererPlugin(plugin: Plugin) { return plugin; } diff --git a/packages/renderer-core/package.json b/packages/renderer-core/package.json index 443ca2ac8..edda8eb8a 100644 --- a/packages/renderer-core/package.json +++ b/packages/renderer-core/package.json @@ -34,6 +34,9 @@ "devDependencies": { "@types/lodash-es": "^4.17.12" }, + "peerDependencies": { + "@alilc/lowcode-shared": "workspace:*" + }, "publishConfig": { "access": "public", "registry": "https://registry.npmjs.org/" diff --git a/packages/renderer-core/src/apiCreate.ts b/packages/renderer-core/src/apiCreate.ts index db090effa..1003fffc8 100644 --- a/packages/renderer-core/src/apiCreate.ts +++ b/packages/renderer-core/src/apiCreate.ts @@ -20,7 +20,7 @@ export function createRenderer( const rendererMain = instantiationService.createInstance(RendererMain); return async (options) => { - rendererMain.initialize(options); + await rendererMain.initialize(options); return rendererMain.startup(renderAdapter); }; diff --git a/packages/renderer-core/src/main.ts b/packages/renderer-core/src/main.ts index 13038fc2d..426d602c5 100644 --- a/packages/renderer-core/src/main.ts +++ b/packages/renderer-core/src/main.ts @@ -77,8 +77,11 @@ export class RendererMain { this.runtimeUtilService.add(util); } + const constants = this.schemaService.get('constants') ?? {}; + const globalScope = this.codeRuntimeService.getScope(); globalScope.setValue({ + constants, utils: this.runtimeUtilService.toExpose(), ...this.runtimeIntlService.toExpose(), }); diff --git a/packages/renderer-core/src/parts/code-runtime/codeScope.ts b/packages/renderer-core/src/parts/code-runtime/codeScope.ts index 3aa18eca6..c08edeeeb 100644 --- a/packages/renderer-core/src/parts/code-runtime/codeScope.ts +++ b/packages/renderer-core/src/parts/code-runtime/codeScope.ts @@ -54,7 +54,7 @@ export class CodeScope implements ICodeScope { if (this.__node.current[name] && !force) { return; } - this.__node.current.value[name] = value; + this.__node.current[name] = value; } setValue(value: PlainObject, replace = false) { diff --git a/packages/renderer-core/src/parts/schema/schemaService.ts b/packages/renderer-core/src/parts/schema/schemaService.ts index e7b873f50..41c262382 100644 --- a/packages/renderer-core/src/parts/schema/schemaService.ts +++ b/packages/renderer-core/src/parts/schema/schemaService.ts @@ -39,7 +39,7 @@ export class SchemaService implements ISchemaService { initialize(schema: unknown): void { if (!isObject(schema)) { - throw Error('schema muse a object'); + throw Error('schema must a object'); } Object.keys(schema).forEach((key) => { diff --git a/packages/renderer-core/src/parts/schema/validation.ts b/packages/renderer-core/src/parts/schema/validation.ts index c96b185fb..fa119ebcb 100644 --- a/packages/renderer-core/src/parts/schema/validation.ts +++ b/packages/renderer-core/src/parts/schema/validation.ts @@ -1,23 +1,54 @@ import { type Spec } from '@alilc/lowcode-shared'; -const SCHEMA_VALIDATIONS_OPTIONS: Partial< - Record< - keyof Spec.Project, - { - valid: (value: any) => boolean; - description: string; - } - > -> = {}; +interface ValidationRule { + valid: (value: T) => boolean; + description: string; +} + +type ValidOptionRecord = { + [K in keyof Spec.Project]: ValidationRule; +}; + +const SCHEMA_KEYS = [ + 'version', + 'componentsMap', + 'componentsTree', + 'utils', + 'i18n', + 'constants', + 'css', + 'config', + 'meta', + 'router', + 'pages', +]; + +const SCHEMA_VALIDATIONS_OPTIONS: Partial = { + componentsMap: { + valid(value) { + return Array.isArray(value); + }, + description: 'componentsMap 必须是一个数组', + }, + componentsTree: { + valid(value) { + return Array.isArray(value); + }, + description: 'componentsTree 必须是一个数组', + }, +}; export function schemaValidation(key: K, value: Spec.Project[K]) { + if (!SCHEMA_KEYS.includes(key)) { + return `schema 的字段名必须是${JSON.stringify(SCHEMA_KEYS)}中的一个`; + } + const validOption = SCHEMA_VALIDATIONS_OPTIONS[key]; if (validOption) { const result = validOption.valid(value); - if (!result) { - throw Error(validOption.description); + return validOption.description; } } diff --git a/packages/shared/package.json b/packages/shared/package.json index b2f17d027..62518bb2e 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -2,7 +2,7 @@ "name": "@alilc/lowcode-shared", "version": "1.0.0-alpha.0", "type": "module", - "main": "dist/low-code-shared.js", + "main": "dist/low-code-shared.cjs", "module": "dist/low-code-shared.js", "types": "dist/index.d.ts", "files": [ diff --git a/packages/shared/src/abilities/storage.ts b/packages/shared/src/abilities/storage.ts index bc811ffbe..80ea3522c 100644 --- a/packages/shared/src/abilities/storage.ts +++ b/packages/shared/src/abilities/storage.ts @@ -53,11 +53,10 @@ export class KeyValueStore { if (this.setterValidation) { const valid = this.setterValidation(key, value); - invariant( - valid === false || typeof valid === 'string', - `failed to config ${key.toString()}, only predefined options can be set under strict mode, predefined options: ${valid ? valid : ''}`, - 'KeyValueStore', - ); + if (valid !== true) { + console.warn(`failed to config ${key.toString()}, validation error: ${valid ? valid : ''}`); + return; + } } this.store.set(key, value); diff --git a/packages/shared/vite.config.ts b/packages/shared/vite.config.ts index 245b2cb50..b8184fd41 100644 --- a/packages/shared/vite.config.ts +++ b/packages/shared/vite.config.ts @@ -1,6 +1,9 @@ import { defineConfig } from 'vite'; -import baseConfigFn from '../../vite.base.config' +import baseConfigFn from '../../vite.base.config'; -export default defineConfig(async () => baseConfigFn({ - name: 'LowCodeShared', -})); +export default defineConfig(async () => + baseConfigFn({ + name: 'LowCodeShared', + defaultFormats: ['es', 'cjs'], + }), +);