From a02b19e6e3b9d48affddcdee1f3e42421b731862 Mon Sep 17 00:00:00 2001 From: 1ncounter <1ncounter.100@gmail.com> Date: Tue, 30 Jul 2024 20:24:50 +0800 Subject: [PATCH] fix: codes refactor --- TODOS.md | 2 + eslint.config.js | 1 + package.json | 38 +++++++++---------- packages/react-renderer/src/api/app.tsx | 16 +++++--- packages/react-renderer/src/api/component.tsx | 23 +++++++---- scripts/build.js | 7 ++-- tsconfig.json | 3 +- vite.base.config.ts | 4 ++ 8 files changed, 56 insertions(+), 38 deletions(-) diff --git a/TODOS.md b/TODOS.md index 21e376496..95fda2cdf 100644 --- a/TODOS.md +++ b/TODOS.md @@ -10,3 +10,5 @@ 5. test 6. publish 7. github workflows + +lodaes replace diff --git a/eslint.config.js b/eslint.config.js index 8182c0931..2c8b31832 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -64,5 +64,6 @@ export default tseslint.config({ 'react-hooks/exhaustive-deps': 'off', // Checks effect dependencies 'no-inner-declarations': 'off', + 'no-constant-condition': 'off', }, }); diff --git a/package.json b/package.json index f471dd0ce..7bb4486f8 100644 --- a/package.json +++ b/package.json @@ -20,27 +20,27 @@ "prepare": "husky" }, "devDependencies": { - "@changesets/cli": "^2.27.1", - "@commitlint/cli": "^19.2.1", - "@commitlint/config-conventional": "^19.1.0", + "@changesets/cli": "^2.27.7", + "@commitlint/cli": "^19.3.0", + "@commitlint/config-conventional": "^19.2.2", "@eslint/js": "^8.57.0", - "@microsoft/api-extractor": "^7.43.0", - "@stylistic/eslint-plugin": "^1.7.0", - "@types/node": "^20.11.30", - "@vanilla-extract/vite-plugin": "^4.0.7", - "@vitejs/plugin-react": "^4.2.1", + "@microsoft/api-extractor": "^7.47.4", + "@stylistic/eslint-plugin": "^1.8.1", + "@types/node": "^22.0.0", + "@vanilla-extract/vite-plugin": "^4.0.13", + "@vitejs/plugin-react": "^4.3.1", "eslint": "^8.57.0", - "eslint-plugin-react": "^7.34.1", - "eslint-plugin-react-hooks": "^4.6.0", - "globals": "^15.0.0", - "husky": "^9.0.11", - "jsdom": "^24.1.0", - "lint-staged": "^15.2.2", - "prettier": "^3.2.5", - "rimraf": "^5.0.2", - "typescript": "^5.4.2", - "typescript-eslint": "^7.5.0", - "vite": "^5.2.9", + "eslint-plugin-react": "^7.35.0", + "eslint-plugin-react-hooks": "^4.6.2", + "globals": "^15.8.0", + "husky": "^9.1.3", + "jsdom": "^24.1.1", + "lint-staged": "^15.2.7", + "prettier": "^3.3.3", + "rimraf": "^6.0.1", + "typescript": "^5.5.4", + "typescript-eslint": "^7.17.0", + "vite": "^5.3.5", "vitest": "^1.6.0" }, "engines": { diff --git a/packages/react-renderer/src/api/app.tsx b/packages/react-renderer/src/api/app.tsx index 89bf14eec..bf0aac153 100644 --- a/packages/react-renderer/src/api/app.tsx +++ b/packages/react-renderer/src/api/app.tsx @@ -1,14 +1,19 @@ import { createRenderer } from '@alilc/lowcode-renderer-core'; import { type Root, createRoot } from 'react-dom/client'; -import { RendererContext, getRenderInstancesByAccessor } from './context'; +import { type IRendererContext, RendererContext, getRenderInstancesByAccessor } from './context'; import { ApplicationView, boosts } from '../app'; import { type ReactAppOptions } from './types'; export const createApp = async (options: ReactAppOptions) => { - return createRenderer(async (accessor) => { - const instances = getRenderInstancesByAccessor(accessor); + return createRenderer(async (service) => { + const contextValue: IRendererContext = service.invokeFunction((accessor) => { + return { + options, + ...getRenderInstancesByAccessor(accessor), + }; + }); - instances.boostsManager.extend(boosts.toExpose()); + contextValue.boostsManager.extend(boosts.toExpose()); let root: Root | undefined; @@ -16,9 +21,8 @@ export const createApp = async (options: ReactAppOptions) => { async mount(containerOrId) { if (root) return; - const defaultId = instances.schema.get('config')?.targetRootID ?? 'app'; + const defaultId = contextValue.schema.get('config.targetRootID', 'app'); const rootElement = normalizeContainer(containerOrId, defaultId); - const contextValue = { ...instances, options }; root = createRoot(rootElement); root.render( diff --git a/packages/react-renderer/src/api/component.tsx b/packages/react-renderer/src/api/component.tsx index 5bf0ef0fb..58e1fbdb1 100644 --- a/packages/react-renderer/src/api/component.tsx +++ b/packages/react-renderer/src/api/component.tsx @@ -1,10 +1,11 @@ import { createRenderer } from '@alilc/lowcode-renderer-core'; -import { FunctionComponent } from 'react'; +import { type ComponentTreeRoot } from '@alilc/lowcode-shared'; +import { type FunctionComponent } from 'react'; import { type LowCodeComponentProps, createComponent as createSchemaComponent, } from '../runtime/createComponent'; -import { RendererContext, getRenderInstancesByAccessor } from './context'; +import { type IRendererContext, RendererContext, getRenderInstancesByAccessor } from './context'; import { type ReactAppOptions } from './types'; interface Render { @@ -12,17 +13,25 @@ interface Render { } export async function createComponent(options: ReactAppOptions) { - const creator = createRenderer((accessor) => { - const instances = getRenderInstancesByAccessor(accessor); - const componentsTree = instances.schema.get('componentsTree')[0]; + const creator = createRenderer((service) => { + const contextValue: IRendererContext = service.invokeFunction((accessor) => { + return { + options, + ...getRenderInstancesByAccessor(accessor), + }; + }); + + const componentsTree = contextValue.schema.get('componentsTree.0'); + + if (!componentsTree) { + throw new Error('componentsTree is required'); + } const LowCodeComponent = createSchemaComponent(componentsTree, { displayName: componentsTree.componentName, ...options.component, }); - const contextValue = { ...instances, options }; - function Component(props: LowCodeComponentProps) { return ( diff --git a/scripts/build.js b/scripts/build.js index b04190a40..dc3e637ec 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -12,15 +12,14 @@ const buildTypes = args['types'] || args['t']; async function run() { const packages = await findWorkspacePackages(cwd()); const targetPackageName = `@alilc/lowcode-${targets[0]}`; - const finalName = packages - .filter((item) => item.manifest.name === targetPackageName) - .map(item => item.manifest.name); + const manifest = packages.filter((item) => item.manifest.name === targetPackageName)[0].manifest; - await execa('pnpm', ['--filter', finalName[0], 'build:target'], { + await execa('pnpm', ['--filter', manifest.name, 'build:target'], { stdio: 'inherit', env: { PROD: prod, FORMATS: formatArgs ? formatArgs : !prod ? 'es' : undefined, + VERSION: manifest.version, }, }); diff --git a/tsconfig.json b/tsconfig.json index f5483473b..d79108a43 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,6 @@ "importHelpers": false, // Enables experimental support for ES7 decorators. "experimentalDecorators": true, - "emitDecoratorMetadata": true, // Generates corresponding .map file. "sourceMap": true, // Disallow inconsistently-cased references to the same file. @@ -37,7 +36,7 @@ "paths": { "@alilc/lowcode-*": ["packages/*/src"] }, - "types": ["vite/client", "vitest/globals", "node"] + "types": ["vitest/globals", "node"] }, "include": [ "packages/global.d.ts", diff --git a/vite.base.config.ts b/vite.base.config.ts index 638326333..39230364e 100644 --- a/vite.base.config.ts +++ b/vite.base.config.ts @@ -32,6 +32,10 @@ export default async ({ } return defineConfig({ + define: { + __DEV__: JSON.stringify(!isProduction), + __VERSION__: JSON.stringify(env['VERSION']), + }, build: { lib: { entry: resolvePath(entry),