feat: add app fof iife

This commit is contained in:
熊潇 2024-12-22 14:49:48 +08:00
parent 3b19cd8581
commit 9b3de5eba3
4 changed files with 34 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/store",
"version": "0.0.1-alpha.7",
"version": "0.0.1-alpha.8",
"main": "dist/store.js",
"module": "dist/store.js",
"types": "dist/store.d.ts",
@ -69,6 +69,8 @@
}
},
"dependencies": {
"@kevisual/router": "0.0.6-alpha-4",
"@rollup/plugin-terser": "^0.4.4",
"eventemitter3": "^5.0.1",
"path-to-regexp": "^8.2.0"
}

View File

@ -4,6 +4,7 @@ import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { dts } from 'rollup-plugin-dts';
import terser from '@rollup/plugin-terser';
/**
* @type {import('rollup').RollupOptions}
@ -77,4 +78,20 @@ export default [
},
plugins: [dts()],
},
{
input: 'src/app.ts',
output: {
file: 'dist/app.js',
format: 'iife',
},
plugins: [resolve({ browser: true }), commonjs(), typescript(), terser()],
},
{
input: 'src/app.ts',
output: {
file: 'dist/app.d.ts',
format: 'es',
},
plugins: [dts()],
},
];

12
src/app.ts Normal file
View File

@ -0,0 +1,12 @@
// 当前的功能把所有的模块注入到windows对象当中
import { useConfigKey, useContextKey } from './web-env.ts';
import { QueryRouterServer } from '@kevisual/router/browser';
// bind to window, 必须要的获取全局的环境变量
window.useConfigKey = useConfigKey;
window.useContextKey = useContextKey;
window.QueryRouterServer = QueryRouterServer;
// bind to window, 获取路由对象
useContextKey('app', () => new QueryRouterServer());

View File

@ -4,6 +4,7 @@ type GlobalEnv = {
name?: string;
[key: string]: any;
};
// 从window对象中获取全局的环境变量如果没有则初始化一个
export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
const env: GlobalEnv = (window as any)[initKey];
const _env = env || initEnv;
@ -17,6 +18,7 @@ export const useEnv = (initEnv?: GlobalEnv, initKey = 'config') => {
return window[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 && _env[key]) {