@mooljs/plugin-store
Advanced tools
+14
-20
@@ -1,6 +0,6 @@ | ||
| import { effectScope as o } from "vue"; | ||
| import { effectScope as c } from "vue"; | ||
| function u(s) { | ||
| let e = !1, t; | ||
| const r = o(!0); | ||
| return (...l) => (e || (t = r.run(() => s(...l)), e = !0), t); | ||
| let t = !1, e; | ||
| const n = c(!0); | ||
| return (...l) => (t || (e = n.run(() => s(...l)), t = !0), e); | ||
| } | ||
@@ -19,14 +19,8 @@ typeof WorkerGlobalScope < "u" && globalThis instanceof WorkerGlobalScope; | ||
| */ | ||
| async initialize(e, t) { | ||
| const moduleFiles = import.meta.glob( | ||
| "/src/store/*.ts", | ||
| { | ||
| eager: true, | ||
| }, | ||
| ); | ||
| Object.entries(moduleFiles).forEach(([l, c]) => { | ||
| var a; | ||
| const n = (a = l.match(/([^/]+)\.ts$/)) == null ? void 0 : a[1]; | ||
| n && this.register(n, c.default); | ||
| }), t != null && t.namespace && t.initialState && this.register(t.namespace, () => t.initialState), e.provide(f, this.modules); | ||
| async initialize(t, e) { | ||
| Object.entries(/* @__PURE__ */ Object.assign({})).forEach(([l, a]) => { | ||
| var o; | ||
| const r = (o = l.match(/([^/]+)\.ts$/)) == null ? void 0 : o[1]; | ||
| r && this.register(r, a.default); | ||
| }), e != null && e.initialState && this.register("@@initialState", () => e.initialState), t.provide(f, this.modules); | ||
| } | ||
@@ -36,4 +30,4 @@ /** | ||
| */ | ||
| register(e, t) { | ||
| this.modules[e] && console.warn(`[StoreManager] 模块 ${e} 已存在,将被覆盖`), this.modules[e] = u(t); | ||
| register(t, e) { | ||
| this.modules[t] && console.warn(`[StoreManager] 模块 ${t} 已存在,将被覆盖`), this.modules[t] = u(e); | ||
| } | ||
@@ -47,5 +41,5 @@ /** | ||
| } | ||
| const m = (s) => { | ||
| const m = (s, t) => { | ||
| const e = i.getInstance(); | ||
| return e.initialize(s), e; | ||
| return e.initialize(s, t), e; | ||
| }; | ||
@@ -52,0 +46,0 @@ export { |
+18
-3
@@ -1,2 +0,2 @@ | ||
| const { readFileSync, existsSync } = require("node:fs"); | ||
| const { readFileSync, existsSync, readdirSync } = require("node:fs"); | ||
| module.exports = (api, options) => { | ||
@@ -13,3 +13,5 @@ api.applyPlugins((config) => { | ||
| runtime: (ctx) => ` | ||
| app.use(setupStore); | ||
| app.use(setupStore,{ | ||
| initialState:(await config.getInitialState?.()) ?? {} | ||
| }); | ||
| `, | ||
@@ -25,6 +27,19 @@ // 虚拟模块定义 | ||
| injectMool: () => { | ||
| return [`import { STORE_KEY } from 'virtual:store';`]; | ||
| return [`import { STORE_KEY } from 'virtual:store';export const useStore = (namespace) => {const modules = inject(STORE_KEY,'');if (!modules?.[namespace]) {throw new Error(\`[useStore] 未找到命名空间为 \${namespace} 的模块\`)} return modules[namespace]()}`]; | ||
| }, | ||
| injectModuleType: () => { | ||
| const storeFiles = readdirSync(api.resolve('src/store')) | ||
| .filter((file) => file.endsWith(".ts")) | ||
| .map((file) => file.replace(".ts", "")); | ||
| const moduleTypes = storeFiles | ||
| .map( | ||
| (module) => ` ${module}: typeof import("src/store/${module}")['default'];\n`, | ||
| ).concat([` '@@initialState':(typeof import("src/app.ts"))["getInitialState"]|(typeof import("src/app.tsx"))["getInitialState"];`]) | ||
| .join(""); | ||
| return [ | ||
| ` interface StoreModule {\n${moduleTypes}\n }\n export const useStore: <K extends keyof StoreModule>(_namespace: K) => StoreModule[K]` | ||
| ] | ||
| } | ||
| }); | ||
| }); | ||
| }; |
+1
-1
| { | ||
| "name": "@mooljs/plugin-store", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "description": " global data store for mooljs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
5230
21.57%90
11.11%