This commit is contained in:
熊潇 2025-05-24 16:58:59 +08:00
parent 3d50b64543
commit 02446fd60f
3 changed files with 65 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/store",
"version": "0.0.3",
"version": "0.0.6",
"main": "dist/store.js",
"module": "dist/store.js",
"types": "dist/store.d.ts",
@ -25,20 +25,24 @@
"devDependencies": {
"@kevisual/load": "workspace:*",
"@kevisual/types": "link:../types",
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-commonjs": "^28.0.3",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.2",
"@types/lodash-es": "^4.17.12",
"fast-deep-equal": "^3.1.3",
"immer": "^10.1.1",
"lodash-es": "^4.17.21",
"nanoid": "^5.1.2",
"rollup": "^4.34.9",
"rollup-plugin-dts": "^6.1.1",
"nanoid": "^5.1.5",
"rollup": "^4.41.1",
"rollup-plugin-dts": "^6.2.1",
"ts-node": "^10.9.2",
"tslib": "^2.8.1",
"typescript": "^5.8.2",
"zustand": "^5.0.3"
"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"
},
"publishConfig": {
"access": "public"
@ -57,8 +61,8 @@
"require": "./dist/web-config.js"
},
"./context": {
"import": "./dist/web-context.js",
"require": "./dist/web-context.js"
"import": "./dist/web-config.js",
"require": "./dist/web-config.js"
},
"./page": {
"import": "./dist/web-page.js",
@ -69,10 +73,5 @@
"require": "./dist/web.js"
}
},
"dependencies": {
"@kevisual/router": "workspace:*",
"@rollup/plugin-terser": "^0.4.4",
"eventemitter3": "^5.0.1",
"path-to-regexp": "^8.2.0"
}
"dependencies": {}
}

View File

@ -1,21 +1,28 @@
// 当前的功能把所有的模块注入到windows对象当中
import * as WebEnv from './web-env.ts';
import { QueryRouterServer } from '@kevisual/router/browser';
import * as Load from '@kevisual/load/browser';
import { Page } from './page.ts';
// bind to window, 必须要的获取全局的环境变量
const { useConfigKey, useContextKey } = WebEnv;
window.useConfigKey = useConfigKey;
window.useContextKey = useContextKey;
// @ts-ignore
window.webEnv = WebEnv;
// @ts-ignore
window.Load = Load;
window.QueryRouterServer = QueryRouterServer;
// bind to window, 获取路由对象
useContextKey('app', () => new QueryRouterServer());
useContextKey('page', () => {
return new Page();
});
export class PageInit {
static isInit = false;
static init(opts?: { load?: boolean; page?: boolean }) {
if (PageInit.isInit) {
return;
}
const { load = true, page = false } = opts || {};
PageInit.isInit = true;
// bind to window, 必须要的获取全局的环境变量
const { useConfigKey, useContextKey } = WebEnv;
window.useConfigKey = useConfigKey;
window.useContextKey = useContextKey;
// @ts-ignore
window.webEnv = WebEnv;
// @ts-ignore
load && (window.Load = Load);
page &&
useContextKey('page', () => {
return new Page();
});
}
}
PageInit.init();

View File

@ -1,22 +1,23 @@
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 = (window as any)[initKey];
const env: GlobalEnv = gt[initKey];
const _env = env || initEnv;
if (!env) {
if (_env) {
(window as any)[initKey] = _env;
gt[initKey] = _env;
} else {
(window as any)[initKey] = {};
gt[initKey] = {};
}
}
return window[initKey] as GlobalEnv;
return gt[initKey] as GlobalEnv;
};
// 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象
@ -113,3 +114,25 @@ 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();