fox-mini-x 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +202 -0
- package/dist/index.cjs +517 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.d.cts +122 -0
- package/dist/index.d.d.ts +122 -0
- package/dist/index.js +485 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# fox-mini-x
|
|
2
|
+
|
|
3
|
+
[](https://github.com/OWNER/REPO/actions/workflows/ci.yml)
|
|
4
|
+
[](https://github.com/OWNER/REPO/actions/workflows/release.yml)
|
|
5
|
+
[](https://www.npmjs.com/package/fox-mini-x)
|
|
6
|
+
|
|
7
|
+
一个用 JavaScript 实现的轻量风格状态管理库,支持 `state/getters/mutations/actions/modules`、命名空间、插件、严格模式、辅助映射函数与动态模块。
|
|
8
|
+
|
|
9
|
+
## 功能特性
|
|
10
|
+
|
|
11
|
+
- `createStore`:创建全局状态仓库
|
|
12
|
+
- `commit / dispatch`:同步 mutation 与异步 action
|
|
13
|
+
- `getters`:派生状态
|
|
14
|
+
- `modules` + `namespaced`
|
|
15
|
+
- 插件系统:`subscribe`、`subscribeAction(before/after/error)`
|
|
16
|
+
- 严格模式:禁止在 mutation 外修改状态
|
|
17
|
+
- 动态模块:`registerModule / unregisterModule / hasModule / hotUpdate`
|
|
18
|
+
- 辅助函数:`mapState / mapGetters / mapMutations / mapActions`
|
|
19
|
+
- `createNamespacedHelpers`
|
|
20
|
+
|
|
21
|
+
## 安装
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install fox-mini-x
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 快速开始
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
import { createStore } from "fox-mini-x";
|
|
31
|
+
|
|
32
|
+
const store = createStore({
|
|
33
|
+
strict: true,
|
|
34
|
+
state: () => ({ count: 0 }),
|
|
35
|
+
getters: {
|
|
36
|
+
doubleCount: (state) => state.count * 2,
|
|
37
|
+
},
|
|
38
|
+
mutations: {
|
|
39
|
+
increment(state, payload = 1) {
|
|
40
|
+
state.count += payload;
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
actions: {
|
|
44
|
+
async incrementAsync({ commit }, payload = 1) {
|
|
45
|
+
await Promise.resolve();
|
|
46
|
+
commit("increment", payload);
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
store.commit("increment", 2);
|
|
52
|
+
await store.dispatch("incrementAsync", 3);
|
|
53
|
+
console.log(store.state.count); // 5
|
|
54
|
+
console.log(store.getters.doubleCount); // 10
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## API 文档
|
|
58
|
+
|
|
59
|
+
### `createStore(options)`
|
|
60
|
+
|
|
61
|
+
创建 store 实例。
|
|
62
|
+
|
|
63
|
+
主要 options:
|
|
64
|
+
|
|
65
|
+
- `state`: `object | () => object`
|
|
66
|
+
- `getters`: `Record<string, fn>`
|
|
67
|
+
- `mutations`: `Record<string, fn>`
|
|
68
|
+
- `actions`: `Record<string, fn | { root?: boolean, handler: fn }>`
|
|
69
|
+
- `modules`: `Record<string, module>`
|
|
70
|
+
- `plugins`: `((store) => void)[]`
|
|
71
|
+
- `strict`: `boolean`
|
|
72
|
+
|
|
73
|
+
### `store` 实例 API
|
|
74
|
+
|
|
75
|
+
- `store.state`
|
|
76
|
+
- `store.getters`
|
|
77
|
+
- `store.commit(type, payload?)`
|
|
78
|
+
- `store.dispatch(type, payload?)`
|
|
79
|
+
- `store.subscribe((mutation, state) => void)`
|
|
80
|
+
- `store.subscribeAction(fn | { before, after, error })`
|
|
81
|
+
- `store.replaceState(newState)`
|
|
82
|
+
- `store.registerModule(path, module, options?)`
|
|
83
|
+
- `store.unregisterModule(path)`
|
|
84
|
+
- `store.hasModule(path)`
|
|
85
|
+
- `store.hotUpdate(newOptions)`
|
|
86
|
+
|
|
87
|
+
### `createLogger(options?)`
|
|
88
|
+
|
|
89
|
+
创建调试插件,记录 mutation/action:
|
|
90
|
+
|
|
91
|
+
- action 生命周期日志:`before / after / error`
|
|
92
|
+
- 可配置:
|
|
93
|
+
- `logActions`(默认 `true`)
|
|
94
|
+
- `logMutations`(默认 `true`)
|
|
95
|
+
- `logger`(默认 `console`)
|
|
96
|
+
|
|
97
|
+
### 映射辅助函数
|
|
98
|
+
|
|
99
|
+
- `mapState`
|
|
100
|
+
- `mapGetters`
|
|
101
|
+
- `mapMutations`
|
|
102
|
+
- `mapActions`
|
|
103
|
+
|
|
104
|
+
支持两种调用方式:
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
mapState(["count"]);
|
|
108
|
+
mapState("user", ["name"]);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `createNamespacedHelpers(namespace)`
|
|
112
|
+
|
|
113
|
+
返回绑定命名空间的辅助函数:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
import { createNamespacedHelpers } from "fox-mini-x";
|
|
117
|
+
|
|
118
|
+
const { mapState, mapGetters, mapMutations, mapActions } =
|
|
119
|
+
createNamespacedHelpers("user");
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## 模块与命名空间示例
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
const store = createStore({
|
|
126
|
+
modules: {
|
|
127
|
+
user: {
|
|
128
|
+
namespaced: true,
|
|
129
|
+
state: () => ({ name: "Guest" }),
|
|
130
|
+
mutations: {
|
|
131
|
+
setName(state, name) {
|
|
132
|
+
state.name = name;
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
actions: {
|
|
136
|
+
async setNameAsync({ commit }, name) {
|
|
137
|
+
await Promise.resolve();
|
|
138
|
+
commit("setName", name);
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
getters: {
|
|
142
|
+
displayName: (state) => `@${state.name}`,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
store.commit("user/setName", "Alice");
|
|
149
|
+
console.log(store.getters["user/displayName"]); // @Alice
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## 插件与 action 生命周期示例
|
|
153
|
+
|
|
154
|
+
```js
|
|
155
|
+
const tracker = (store) => {
|
|
156
|
+
store.subscribeAction({
|
|
157
|
+
before: (action) => console.log("before:", action.type),
|
|
158
|
+
after: (action) => console.log("after:", action.type),
|
|
159
|
+
error: (action, _state, err) => console.error("error:", action.type, err),
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 开发命令
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
# 运行示例
|
|
168
|
+
npm start
|
|
169
|
+
|
|
170
|
+
# 测试
|
|
171
|
+
npm test
|
|
172
|
+
|
|
173
|
+
# 构建(输出 ESM + CJS + d.ts)
|
|
174
|
+
npm run build
|
|
175
|
+
|
|
176
|
+
# 生成 changeset
|
|
177
|
+
npm run changeset
|
|
178
|
+
|
|
179
|
+
# 应用版本变更(修改 package.json + CHANGELOG)
|
|
180
|
+
npm run version-packages
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## 发布说明
|
|
184
|
+
|
|
185
|
+
### 一次标准发布流程
|
|
186
|
+
|
|
187
|
+
1. 开发完成后执行 `npm run changeset`,选择版本语义(patch/minor/major)并填写说明。
|
|
188
|
+
2. 推送分支后,GitHub Actions 会通过 `changesets` 工作流自动维护版本 PR。
|
|
189
|
+
3. 合并版本 PR 后,创建并推送 tag(例如 `v1.2.0`)。
|
|
190
|
+
4. `release.yml` 会在 tag push 时自动执行测试、构建并发布到 npm。
|
|
191
|
+
|
|
192
|
+
### 发布前准备
|
|
193
|
+
|
|
194
|
+
- 在 GitHub 仓库 `Settings -> Secrets and variables -> Actions` 配置:
|
|
195
|
+
- `NPM_TOKEN`:npm automation token
|
|
196
|
+
- 确保仓库权限允许 `contents: write`(用于创建 GitHub Release)
|
|
197
|
+
|
|
198
|
+
> 说明:README 徽章中的 `OWNER/REPO` 需要替换为你的真实仓库路径。
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT
|