From bbf826b765f841fb0f07b2180a6fe7f3de009116 Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 12 Jun 2025 15:01:59 +0800 Subject: [PATCH] feat: remove web-env --- package.json | 14 ++--- rollup.config.js | 32 +++++------ src/web-env.ts | 139 +---------------------------------------------- 3 files changed, 24 insertions(+), 161 deletions(-) diff --git a/package.json b/package.json index d39d6fe..c48aab9 100644 --- a/package.json +++ b/package.json @@ -26,26 +26,27 @@ "license": "ISC", "description": "", "devDependencies": { + "@kevisual/context": "^0.0.3", "@kevisual/load": "^0.0.6", + "@kevisual/router": "^0.0.21", "@kevisual/types": "link:../types", "@rollup/plugin-commonjs": "^28.0.3", "@rollup/plugin-node-resolve": "^16.0.1", + "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^12.1.2", "@types/lodash-es": "^4.17.12", + "eventemitter3": "^5.0.1", "fast-deep-equal": "^3.1.3", "immer": "^10.1.1", "lodash-es": "^4.17.21", "nanoid": "^5.1.5", + "path-to-regexp": "^8.2.0", "rollup": "^4.41.1", "rollup-plugin-dts": "^6.2.1", "ts-node": "^10.9.2", "tslib": "^2.8.1", "typescript": "^5.8.3", - "zustand": "^5.0.5", - "@kevisual/router": "^0.0.21", - "@rollup/plugin-terser": "^0.4.4", - "eventemitter3": "^5.0.1", - "path-to-regexp": "^8.2.0" + "zustand": "^5.0.5" }, "publishConfig": { "access": "public" @@ -75,6 +76,5 @@ "import": "./dist-react/store-react.js", "types": "./dist-react/index.d.ts" } - }, - "dependencies": {} + } } \ No newline at end of file diff --git a/rollup.config.js b/rollup.config.js index dec1ea2..1c97a1a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -30,22 +30,22 @@ export default [ }, plugins: [dts()], }, - { - input: 'src/web-env.ts', - output: { - file: 'dist/web-config.js', - format: 'es', - }, - plugins: [resolve({ browser: true }), commonjs(), typescript()], - }, - { - input: 'src/web-env.ts', - output: { - file: 'dist/web-config.d.ts', - format: 'es', - }, - plugins: [dts()], - }, + // { + // input: 'src/web-env.ts', + // output: { + // file: 'dist/web-config.js', + // format: 'es', + // }, + // plugins: [resolve({ browser: true }), commonjs(), typescript()], + // }, + // { + // input: 'src/web-env.ts', + // output: { + // file: 'dist/web-config.d.ts', + // format: 'es', + // }, + // plugins: [dts()], + // }, { input: 'src/page.ts', output: { diff --git a/src/web-env.ts b/src/web-env.ts index 0b46bd7..6685cd9 100644 --- a/src/web-env.ts +++ b/src/web-env.ts @@ -1,138 +1 @@ -import { getPathKey } from './utils/path-key.ts'; -import { BaseLoad } from '@kevisual/load'; - -const gt = (globalThis as any) || window || self; -type GlobalEnv = { - name?: string; - [key: string]: any; -}; -// 从window对象中获取全局的环境变量,如果没有则初始化一个 -export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => { - const env: GlobalEnv = gt[initKey]; - const _env = env || initEnv; - if (!env) { - if (_env) { - gt[initKey] = _env; - } else { - gt[initKey] = {}; - } - } - return gt[initKey] as GlobalEnv; -}; - -// 从全局环境变量中获取指定的key值,如果没有则初始化一个, key不存在,返回Env对象 -export const useEnvKey = (key: string, init?: () => T | null, initKey = 'config'): T => { - const _env = useEnv({}, initKey); - // 已经存在,直接返回 - if (key && typeof _env[key] !== 'undefined') { - return _env[key]; - } - // 不存在,但是有初始化函数,初始化的返回,同步函数,删除了重新加载? - if (key && init) { - _env[key] = init(); - return _env[key]; - } - if (key) { - // 加载 - const baseLoad = new BaseLoad(); - const voidFn = async () => { - return _env[key]; - }; - const checkFn = async () => { - const loadRes = await baseLoad.load(voidFn, { - key, - isReRun: true, - checkSuccess: () => _env[key], - timeout: 5 * 60 * 1000, - interval: 1000, - // - }); - if (loadRes.code !== 200) { - console.error('load key error'); - return null; - } - return _env[key]; - }; - return checkFn() as T; - } - // 不存在,没有初始化函数 - console.error('key is empty '); - return null; -}; - -export const usePageEnv = (init?: () => {}, initKey = 'conifg') => { - const { id } = getPathKey(); - return useEnvKey(id, init, initKey); -}; -export const useEnvKeyNew = (key: string, initKey = 'conifg', opts?: { getNew?: boolean; init?: () => {} }) => { - const _env = useEnv({}, initKey); - if (key) { - delete _env[key]; - } - if (opts?.getNew && opts.init) { - return useEnvKey(key, opts.init, initKey); - } else if (opts?.getNew) { - return useEnvKey(key, null, initKey); - } -}; -type GlobalContext = { - name?: string; - [key: string]: any; -}; -export const useContext = (initContext?: GlobalContext) => { - return useEnv(initContext, 'context'); -}; - -export const useContextKey = (key: string, init?: () => T, isNew?: boolean): T => { - if (isNew) { - return useEnvKeyNew(key, 'context', { getNew: true, init }); - } - return useEnvKey(key, init, 'context'); -}; - -export const usePageContext = (init?: () => {}) => { - const { id } = getPathKey(); - return useContextKey(id, init); -}; - -type GlobalConfig = { - name?: string; - [key: string]: any; -}; -export const useConfig = (initConfig?: GlobalConfig) => { - return useEnv(initConfig, 'config'); -}; - -export const useConfigKey = (key: string, init?: () => T, isNew?: boolean): T => { - if (isNew) { - return useEnvKeyNew(key, 'config', { getNew: true, init }); - } - return useEnvKey(key, init, 'config'); -}; - -export const usePageConfig = (init?: () => {}) => { - const { id } = getPathKey(); - return useConfigKey(id, init); -}; - -class InitEnv { - static isInit = false; - - static init(opts?: { load?: boolean; page?: boolean }) { - if (InitEnv.isInit) { - return; - } - const { load = true, page = false } = opts || {}; - InitEnv.isInit = true; - // bind to window, 必须要的获取全局的环境变量 - // @ts-ignore - gt.useConfigKey = useConfigKey; - // @ts-ignore - gt.useContextKey = useContextKey; - // @ts-ignore - gt.webEnv = { useConfigKey, useContextKey }; - // @ts-ignore - load && (gt.Load = BaseLoad); - } -} -InitEnv.init(); +export * from '@kevisual/context';