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

View File

@ -1,21 +1,28 @@
// 当前的功能把所有的模块注入到windows对象当中 // 当前的功能把所有的模块注入到windows对象当中
import * as WebEnv from './web-env.ts'; import * as WebEnv from './web-env.ts';
import { QueryRouterServer } from '@kevisual/router/browser';
import * as Load from '@kevisual/load/browser'; import * as Load from '@kevisual/load/browser';
import { Page } from './page.ts'; import { Page } from './page.ts';
// bind to window, 必须要的获取全局的环境变量 export class PageInit {
const { useConfigKey, useContextKey } = WebEnv; static isInit = false;
window.useConfigKey = useConfigKey; static init(opts?: { load?: boolean; page?: boolean }) {
window.useContextKey = useContextKey; if (PageInit.isInit) {
// @ts-ignore return;
window.webEnv = WebEnv; }
// @ts-ignore const { load = true, page = false } = opts || {};
window.Load = Load; PageInit.isInit = true;
window.QueryRouterServer = QueryRouterServer; // bind to window, 必须要的获取全局的环境变量
const { useConfigKey, useContextKey } = WebEnv;
// bind to window, 获取路由对象 window.useConfigKey = useConfigKey;
useContextKey('app', () => new QueryRouterServer()); window.useContextKey = useContextKey;
useContextKey('page', () => { // @ts-ignore
return new Page(); 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 { getPathKey } from './utils/path-key.ts';
import { BaseLoad } from '@kevisual/load'; import { BaseLoad } from '@kevisual/load';
const gt = (globalThis as any) || window || self;
type GlobalEnv = { type GlobalEnv = {
name?: string; name?: string;
[key: string]: any; [key: string]: any;
}; };
// 从window对象中获取全局的环境变量如果没有则初始化一个 // 从window对象中获取全局的环境变量如果没有则初始化一个
export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => { export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
const env: GlobalEnv = (window as any)[initKey]; const env: GlobalEnv = gt[initKey];
const _env = env || initEnv; const _env = env || initEnv;
if (!env) { if (!env) {
if (_env) { if (_env) {
(window as any)[initKey] = _env; gt[initKey] = _env;
} else { } else {
(window as any)[initKey] = {}; gt[initKey] = {};
} }
} }
return window[initKey] as GlobalEnv; return gt[initKey] as GlobalEnv;
}; };
// 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象 // 从全局环境变量中获取指定的key值如果没有则初始化一个, key不存在返回Env对象
@ -113,3 +114,25 @@ export const usePageConfig = (init?: () => {}) => {
const { id } = getPathKey(); const { id } = getPathKey();
return useConfigKey(id, init); 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();