feat: remove web-env
This commit is contained in:
parent
9d22faa8ba
commit
bbf826b765
14
package.json
14
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": {}
|
||||
}
|
||||
}
|
@ -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: {
|
||||
|
139
src/web-env.ts
139
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 = <T = any>(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 = <T = any>(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 = <T = any>(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';
|
||||
|
Loading…
x
Reference in New Issue
Block a user