@umijs/plugins 4.0.0-beta.8 → 4.0.0-canary-20240513.3
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/LICENSE +21 -0
- package/README.md +4 -1
- package/dist/access.d.ts +1 -1
- package/dist/access.js +194 -4
- package/dist/{sass.d.ts → analytics.d.ts} +1 -1
- package/dist/analytics.js +118 -0
- package/dist/antd.d.ts +1 -1
- package/dist/antd.js +345 -157
- package/dist/{icons.d.ts → confetti.d.ts} +1 -1
- package/dist/confetti.js +64 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +30 -0
- package/dist/dva.d.ts +4 -1
- package/dist/dva.js +302 -8
- package/dist/initial-state.d.ts +1 -1
- package/dist/initial-state.js +147 -4
- package/dist/layout.d.ts +1 -1
- package/dist/layout.js +754 -4
- package/dist/locale.d.ts +2 -1
- package/dist/locale.js +257 -4
- package/dist/mf.d.ts +2 -0
- package/dist/mf.js +358 -0
- package/dist/model.d.ts +1 -1
- package/dist/model.js +109 -4
- package/dist/moment2dayjs.d.ts +3 -0
- package/dist/moment2dayjs.js +125 -0
- package/dist/qiankun/constants.d.ts +5 -0
- package/dist/qiankun/constants.js +41 -0
- package/dist/qiankun/master.d.ts +6 -0
- package/dist/qiankun/master.js +236 -0
- package/dist/qiankun/slave.d.ts +3 -0
- package/dist/qiankun/slave.js +345 -0
- package/dist/qiankun.d.ts +1 -1
- package/dist/qiankun.js +51 -4
- package/dist/react-query.d.ts +3 -0
- package/dist/react-query.js +193 -0
- package/dist/request.d.ts +1 -1
- package/dist/request.js +384 -4
- package/dist/styled-components.d.ts +3 -0
- package/dist/styled-components.js +150 -0
- package/dist/tailwindcss.d.ts +3 -0
- package/dist/tailwindcss.js +106 -0
- package/dist/unocss.d.ts +3 -0
- package/dist/unocss.js +71 -0
- package/dist/utils/astUtils.d.ts +3 -0
- package/dist/utils/astUtils.js +49 -0
- package/dist/utils/localeUtils.d.ts +33 -0
- package/dist/utils/localeUtils.js +139 -0
- package/dist/utils/mfUtils.d.ts +14 -0
- package/dist/utils/mfUtils.js +56 -0
- package/dist/utils/modelUtils.d.ts +40 -0
- package/dist/utils/modelUtils.js +286 -0
- package/dist/utils/resolveProjectDep.d.ts +5 -0
- package/dist/utils/resolveProjectDep.js +40 -0
- package/dist/utils/withTmpPath.d.ts +6 -0
- package/dist/utils/withTmpPath.js +39 -0
- package/dist/valtio.d.ts +3 -0
- package/dist/valtio.js +81 -0
- package/libs/dva.tsx +45 -0
- package/libs/model.tsx +180 -0
- package/libs/qiankun/master/AntdErrorBoundary.tsx +34 -0
- package/libs/qiankun/master/AntdLoader.tsx +15 -0
- package/libs/qiankun/master/ErrorBoundary.tsx +7 -0
- package/libs/qiankun/master/MicroApp.tsx +327 -0
- package/libs/qiankun/master/MicroAppWithMemoHistory.tsx +43 -0
- package/libs/qiankun/master/common.ts +172 -0
- package/libs/qiankun/master/constants.ts +19 -0
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +45 -0
- package/libs/qiankun/master/masterRuntimePlugin.tsx +161 -0
- package/libs/qiankun/master/routeUtils.ts +26 -0
- package/libs/qiankun/master/types.ts +54 -0
- package/libs/qiankun/slave/MicroAppLink.tsx +122 -0
- package/libs/qiankun/slave/connectMaster.tsx +14 -0
- package/libs/qiankun/slave/constants.ts +5 -0
- package/libs/qiankun/slave/lifecycles.ts +164 -0
- package/libs/qiankun/slave/qiankunModel.ts +18 -0
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +36 -0
- package/package.json +54 -24
- package/templates/antd/runtime.ts.tpl +170 -0
- package/templates/antd/types.d.ts.tpl +17 -0
- package/templates/locale/SelectLang.tpl +502 -0
- package/templates/locale/locale.tpl +82 -0
- package/templates/locale/localeExports.tpl +307 -0
- package/templates/locale/runtime.tpl +34 -0
- package/templates/mf/runtime.ts.tpl +149 -0
- package/dist/access.d.ts.map +0 -1
- package/dist/antd.d.ts.map +0 -1
- package/dist/dva.d.ts.map +0 -1
- package/dist/icons.d.ts.map +0 -1
- package/dist/icons.js +0 -5
- package/dist/initial-state.d.ts.map +0 -1
- package/dist/layout.d.ts.map +0 -1
- package/dist/locale.d.ts.map +0 -1
- package/dist/model.d.ts.map +0 -1
- package/dist/qiankun.d.ts.map +0 -1
- package/dist/request.d.ts.map +0 -1
- package/dist/sass.d.ts.map +0 -1
- package/dist/sass.js +0 -5
package/dist/dva.js
CHANGED
|
@@ -1,9 +1,303 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
10
|
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/dva.ts
|
|
30
|
+
var dva_exports = {};
|
|
31
|
+
__export(dva_exports, {
|
|
32
|
+
default: () => dva_default,
|
|
33
|
+
getAllModels: () => getAllModels,
|
|
34
|
+
getModelUtil: () => getModelUtil
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(dva_exports);
|
|
37
|
+
var t = __toESM(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
38
|
+
var import_utils = require("@umijs/utils");
|
|
39
|
+
var import_path = require("path");
|
|
40
|
+
var import_umi = require("umi");
|
|
41
|
+
var import_plugin_utils = require("umi/plugin-utils");
|
|
42
|
+
var import_modelUtils = require("./utils/modelUtils");
|
|
43
|
+
var import_withTmpPath = require("./utils/withTmpPath");
|
|
44
|
+
var dva_default = (api) => {
|
|
45
|
+
const pkgPath = (0, import_path.join)(__dirname, "../libs/dva.tsx");
|
|
46
|
+
api.describe({
|
|
47
|
+
config: {
|
|
48
|
+
schema({ zod }) {
|
|
49
|
+
return zod.object({
|
|
50
|
+
extraModels: zod.array(zod.string()),
|
|
51
|
+
immer: zod.record(zod.any()),
|
|
52
|
+
skipModelValidate: zod.boolean()
|
|
53
|
+
}).deepPartial();
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
enableBy: api.EnableBy.config
|
|
57
|
+
});
|
|
58
|
+
api.modifyAppData((memo) => {
|
|
59
|
+
const models = getAllModels(api);
|
|
60
|
+
memo.pluginDva = {
|
|
61
|
+
pkgPath,
|
|
62
|
+
models
|
|
63
|
+
};
|
|
64
|
+
return memo;
|
|
65
|
+
});
|
|
66
|
+
api.modifyConfig((memo) => {
|
|
67
|
+
memo.alias["dva$"] = pkgPath;
|
|
68
|
+
return memo;
|
|
69
|
+
});
|
|
70
|
+
api.onGenerateFiles((args) => {
|
|
71
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
72
|
+
const models = args.isFirstTime ? api.appData.pluginDva.models : getAllModels(api);
|
|
73
|
+
api.writeTmpFile({
|
|
74
|
+
path: "models.ts",
|
|
75
|
+
content: import_modelUtils.ModelUtils.getModelsContent(models)
|
|
76
|
+
});
|
|
77
|
+
api.writeTmpFile({
|
|
78
|
+
path: import_umi.RUNTIME_TYPE_FILE_NAME,
|
|
79
|
+
content: `
|
|
80
|
+
export interface IRuntimeConfig {
|
|
81
|
+
dva?: {
|
|
82
|
+
config?: {
|
|
83
|
+
initialState?: Record<string, any>;
|
|
84
|
+
onError?: any;
|
|
85
|
+
onStateChange?: any;
|
|
86
|
+
onAction?: any;
|
|
87
|
+
onHmr?: any;
|
|
88
|
+
onReducer?: any;
|
|
89
|
+
onEffect?: any;
|
|
90
|
+
extraReducers?: any;
|
|
91
|
+
extraEnhancers?: any;
|
|
92
|
+
[key: string]: any;
|
|
93
|
+
},
|
|
94
|
+
plugins?: string[];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
`
|
|
98
|
+
});
|
|
99
|
+
api.writeTmpFile({
|
|
100
|
+
path: "dva.tsx",
|
|
101
|
+
tpl: `
|
|
102
|
+
// It's faked dva
|
|
103
|
+
// aliased to @umijs/plugins/templates/dva
|
|
104
|
+
import { create, Provider } from '${(0, import_utils.winPath)(pkgPath)}';
|
|
105
|
+
import createLoading from '${(0, import_utils.winPath)(require.resolve("dva-loading"))}';
|
|
106
|
+
${((_a = api.config.dva) == null ? void 0 : _a.immer) ? `
|
|
107
|
+
import dvaImmer, { enableES5, enableAllPlugins } from '${(0, import_utils.winPath)(
|
|
108
|
+
require.resolve("dva-immer")
|
|
109
|
+
)}';
|
|
110
|
+
` : ""}
|
|
111
|
+
import React, { useRef } from 'react';
|
|
112
|
+
import { history, ApplyPluginsType, useAppData } from 'umi';
|
|
113
|
+
import { models } from './models';
|
|
114
|
+
|
|
115
|
+
let dvaApp: any;
|
|
116
|
+
|
|
117
|
+
export function RootContainer(props: any) {
|
|
118
|
+
const { pluginManager } = useAppData();
|
|
119
|
+
const app = useRef<any>();
|
|
120
|
+
const runtimeDva = pluginManager.applyPlugins({
|
|
121
|
+
key: 'dva',
|
|
122
|
+
type: ApplyPluginsType.modify,
|
|
123
|
+
initialValue: {},
|
|
124
|
+
});
|
|
125
|
+
if (!app.current) {
|
|
126
|
+
app.current = create(
|
|
127
|
+
{
|
|
128
|
+
history,
|
|
129
|
+
...(runtimeDva.config || {}),
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
initialReducer: {},
|
|
133
|
+
setupMiddlewares(middlewares: Function[]) {
|
|
134
|
+
return [...middlewares];
|
|
135
|
+
},
|
|
136
|
+
setupApp(app: IDvaApp) {
|
|
137
|
+
app._history = history;
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
dvaApp = app.current;
|
|
142
|
+
app.current.use(createLoading());
|
|
143
|
+
${((_b = api.config.dva) == null ? void 0 : _b.immer) ? `app.current.use(dvaImmer());` : ""}
|
|
144
|
+
${((_d = (_c = api.config.dva) == null ? void 0 : _c.immer) == null ? void 0 : _d.enableES5) ? `enableES5();` : ""}
|
|
145
|
+
${((_f = (_e = api.config.dva) == null ? void 0 : _e.immer) == null ? void 0 : _f.enableAllPlugins) ? `enableAllPlugins();` : ""}
|
|
146
|
+
(runtimeDva.plugins || []).forEach((p) => {
|
|
147
|
+
app.current.use(p);
|
|
148
|
+
});
|
|
149
|
+
for (const id of Object.keys(models)) {
|
|
150
|
+
app.current.model({
|
|
151
|
+
namespace: models[id].namespace,
|
|
152
|
+
...models[id].model,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
app.current.start();
|
|
156
|
+
}
|
|
157
|
+
return <Provider store={app.current!._store}>{props.children}</Provider>;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function getDvaApp() {
|
|
161
|
+
return dvaApp;
|
|
162
|
+
}
|
|
163
|
+
`,
|
|
164
|
+
context: {}
|
|
165
|
+
});
|
|
166
|
+
api.writeTmpFile({
|
|
167
|
+
path: "runtime.tsx",
|
|
168
|
+
content: `
|
|
169
|
+
import React from 'react';
|
|
170
|
+
import { RootContainer } from './dva';
|
|
171
|
+
|
|
172
|
+
export function dataflowProvider(container, opts) {
|
|
173
|
+
return React.createElement(RootContainer, opts, container);
|
|
174
|
+
}
|
|
175
|
+
`
|
|
176
|
+
});
|
|
177
|
+
api.writeTmpFile({
|
|
178
|
+
path: "index.ts",
|
|
179
|
+
content: `
|
|
180
|
+
export { connect, useDispatch, useStore, useSelector } from '${(0, import_utils.winPath)(
|
|
181
|
+
pkgPath
|
|
182
|
+
)}';
|
|
183
|
+
export { getDvaApp } from './dva';
|
|
184
|
+
`
|
|
185
|
+
});
|
|
186
|
+
api.writeTmpFile({
|
|
187
|
+
path: "types.d.ts",
|
|
188
|
+
tpl: `
|
|
189
|
+
import type { History } from '${api.appData.umi.importSource}';
|
|
190
|
+
|
|
191
|
+
export interface ConnectProps {
|
|
192
|
+
dispatch?: Dispatch;
|
|
193
|
+
}
|
|
194
|
+
type RequiredConnectProps = Required<ConnectProps>
|
|
195
|
+
export type ConnectRC<
|
|
196
|
+
T = {},
|
|
197
|
+
> = React.ForwardRefRenderFunction<any, T & RequiredConnectProps>;
|
|
198
|
+
interface Action<T = any> {
|
|
199
|
+
type: T
|
|
200
|
+
}
|
|
201
|
+
interface AnyAction extends Action {
|
|
202
|
+
// Allows any extra properties to be defined in an action.
|
|
203
|
+
[extraProps: string]: any
|
|
204
|
+
}
|
|
205
|
+
interface Dispatch<A extends Action = AnyAction> {
|
|
206
|
+
<T extends A>(action: T): T
|
|
207
|
+
}
|
|
208
|
+
interface EffectsCommandMap {
|
|
209
|
+
put: <A extends AnyAction>(action: A) => any,
|
|
210
|
+
call: Function,
|
|
211
|
+
select: Function,
|
|
212
|
+
take: Function,
|
|
213
|
+
cancel: Function,
|
|
214
|
+
[key: string]: any,
|
|
215
|
+
}
|
|
216
|
+
interface Action<T = any> {
|
|
217
|
+
type: T
|
|
218
|
+
}
|
|
219
|
+
export type Reducer<S = any, A extends Action = AnyAction> = (prevState: S, action: A) => ${((_g = api.config.dva) == null ? void 0 : _g.immer) ? "S | void" : "S"};
|
|
220
|
+
export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void;
|
|
221
|
+
type EffectType = 'takeEvery' | 'takeLatest' | 'watcher' | 'throttle';
|
|
222
|
+
type EffectWithType = [Effect, { type: EffectType }];
|
|
223
|
+
export type Subscription = (api: SubscriptionAPI, done: Function) => void;
|
|
224
|
+
|
|
225
|
+
export interface ReducersMapObject<T> {
|
|
226
|
+
[key: string]: Reducer<T>,
|
|
227
|
+
}
|
|
228
|
+
export interface EffectsMapObject {
|
|
229
|
+
[key: string]: Effect | EffectWithType,
|
|
230
|
+
}
|
|
231
|
+
export interface SubscriptionAPI {
|
|
232
|
+
dispatch: Dispatch<any>,
|
|
233
|
+
history: History,
|
|
234
|
+
}
|
|
235
|
+
export interface SubscriptionsMapObject {
|
|
236
|
+
[key: string]: Subscription,
|
|
237
|
+
}
|
|
238
|
+
export interface DvaModel<T, E = EffectsMapObject, R = ReducersMapObject<T>> {
|
|
239
|
+
namespace: string,
|
|
240
|
+
state?: T,
|
|
241
|
+
reducers?: R,
|
|
242
|
+
effects?: E,
|
|
243
|
+
subscriptions?: SubscriptionsMapObject,
|
|
244
|
+
}
|
|
245
|
+
`,
|
|
246
|
+
context: {}
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
api.addTmpGenerateWatcherPaths(() => {
|
|
250
|
+
return [(0, import_path.join)(api.paths.absSrcPath, "models")];
|
|
251
|
+
});
|
|
252
|
+
api.addRuntimePlugin(() => {
|
|
253
|
+
return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })];
|
|
254
|
+
});
|
|
255
|
+
api.addRuntimePluginKey(() => ["dva"]);
|
|
256
|
+
api.registerCommand({
|
|
257
|
+
name: "dva",
|
|
258
|
+
fn() {
|
|
259
|
+
api.logger.info(import_plugin_utils.chalk.green.bold("dva models"));
|
|
260
|
+
api.appData.pluginDva.models.forEach((model) => {
|
|
261
|
+
api.logger.info(` - ${(0, import_path.relative)(api.cwd, model.file)}`);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
function getModelUtil(api) {
|
|
267
|
+
return new import_modelUtils.ModelUtils(api, {
|
|
268
|
+
contentTest(content) {
|
|
269
|
+
if (api == null ? void 0 : api.config.dva.skipModelValidate)
|
|
270
|
+
return true;
|
|
271
|
+
return content.startsWith("// @dva-model");
|
|
272
|
+
},
|
|
273
|
+
astTest({ node, content }) {
|
|
274
|
+
if (isModelObject(node)) {
|
|
275
|
+
return true;
|
|
276
|
+
} else if (content.includes("dva-model-extend") && t.isCallExpression(node) && node.arguments.length === 2 && isModelObject(node.arguments[1])) {
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
function getAllModels(api) {
|
|
284
|
+
return getModelUtil(api).getAllModels({
|
|
285
|
+
extraModels: [...api.config.dva.extraModels || []]
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
function isModelObject(node) {
|
|
289
|
+
return t.isObjectExpression(node) && node.properties.some((property) => {
|
|
290
|
+
return [
|
|
291
|
+
"state",
|
|
292
|
+
"reducers",
|
|
293
|
+
"subscriptions",
|
|
294
|
+
"effects",
|
|
295
|
+
"namespace"
|
|
296
|
+
].includes(property.key.name);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
300
|
+
0 && (module.exports = {
|
|
301
|
+
getAllModels,
|
|
302
|
+
getModelUtil
|
|
303
|
+
});
|
package/dist/initial-state.d.ts
CHANGED
package/dist/initial-state.js
CHANGED
|
@@ -1,5 +1,148 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/initial-state.ts
|
|
20
|
+
var initial_state_exports = {};
|
|
21
|
+
__export(initial_state_exports, {
|
|
22
|
+
default: () => initial_state_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(initial_state_exports);
|
|
25
|
+
var import_umi = require("umi");
|
|
26
|
+
var import_withTmpPath = require("./utils/withTmpPath");
|
|
27
|
+
var initial_state_default = (api) => {
|
|
28
|
+
api.describe({
|
|
29
|
+
config: {
|
|
30
|
+
schema({ zod }) {
|
|
31
|
+
return zod.object({
|
|
32
|
+
loading: zod.string()
|
|
33
|
+
}).partial();
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
enableBy: api.EnableBy.config
|
|
37
|
+
});
|
|
38
|
+
api.register({
|
|
39
|
+
key: "addExtraModels",
|
|
40
|
+
fn: () => [
|
|
41
|
+
(0, import_withTmpPath.withTmpPath)({
|
|
42
|
+
api,
|
|
43
|
+
path: '@@initialState.ts#{"namespace":"@@initialState"}'
|
|
44
|
+
})
|
|
45
|
+
]
|
|
46
|
+
});
|
|
47
|
+
api.addRuntimePluginKey(() => ["getInitialState"]);
|
|
48
|
+
api.addRuntimePlugin(() => {
|
|
49
|
+
return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })];
|
|
50
|
+
});
|
|
51
|
+
api.onGenerateFiles(() => {
|
|
52
|
+
var _a;
|
|
53
|
+
const { loading } = api.config.initialState;
|
|
54
|
+
api.writeTmpFile({
|
|
55
|
+
path: "Provider.tsx",
|
|
56
|
+
content: `
|
|
57
|
+
import React from 'react';
|
|
58
|
+
import { useModel } from '@@/plugin-model';
|
|
59
|
+
${loading ? `import Loading from '${loading}'` : `function Loading() { return <div />; }`}
|
|
60
|
+
export default function InitialStateProvider(props: any) {
|
|
61
|
+
const appLoaded = React.useRef(false);
|
|
62
|
+
const { loading = false } = useModel("@@initialState") || {};
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
if (!loading) {
|
|
65
|
+
appLoaded.current = true;
|
|
66
|
+
}
|
|
67
|
+
}, [loading]);
|
|
68
|
+
if (loading && !appLoaded.current && typeof window !== 'undefined') {
|
|
69
|
+
return <Loading />;
|
|
70
|
+
}
|
|
71
|
+
return props.children;
|
|
72
|
+
}
|
|
73
|
+
`
|
|
74
|
+
});
|
|
75
|
+
api.writeTmpFile({
|
|
76
|
+
path: "@@initialState.ts",
|
|
77
|
+
content: ((_a = api.appData.appJS) == null ? void 0 : _a.exports.includes("getInitialState")) ? `
|
|
78
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
79
|
+
import { getInitialState } from '@/app';
|
|
80
|
+
|
|
81
|
+
export type InitialStateType = Awaited<ReturnType<typeof getInitialState>> | undefined;
|
|
82
|
+
|
|
83
|
+
const initState = {
|
|
84
|
+
initialState: undefined as InitialStateType,
|
|
85
|
+
loading: true,
|
|
86
|
+
error: undefined,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default () => {
|
|
90
|
+
const [state, setState] = useState(initState);
|
|
91
|
+
const refresh = useCallback(async () => {
|
|
92
|
+
setState((s) => ({ ...s, loading: true, error: undefined }));
|
|
93
|
+
try {
|
|
94
|
+
const ret = await getInitialState();
|
|
95
|
+
setState((s) => ({ ...s, initialState: ret, loading: false }));
|
|
96
|
+
} catch (e) {
|
|
97
|
+
setState((s) => ({ ...s, error: e, loading: false }));
|
|
98
|
+
}
|
|
99
|
+
}, []);
|
|
100
|
+
|
|
101
|
+
const setInitialState = useCallback(
|
|
102
|
+
async (
|
|
103
|
+
initialState: InitialStateType | ((initialState: InitialStateType) => InitialStateType),
|
|
104
|
+
) => {
|
|
105
|
+
setState((s) => {
|
|
106
|
+
if (typeof initialState === 'function') {
|
|
107
|
+
return { ...s, initialState: initialState(s.initialState), loading: false };
|
|
108
|
+
}
|
|
109
|
+
return { ...s, initialState, loading: false };
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
[],
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
refresh();
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
...state,
|
|
121
|
+
refresh,
|
|
122
|
+
setInitialState,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
` : `
|
|
126
|
+
export default () => ({ loading: false, refresh: () => {} })
|
|
127
|
+
`
|
|
128
|
+
});
|
|
129
|
+
api.writeTmpFile({
|
|
130
|
+
path: "runtime.tsx",
|
|
131
|
+
content: `
|
|
132
|
+
import React from 'react';
|
|
133
|
+
import Provider from './Provider';
|
|
134
|
+
export function dataflowProvider(container) {
|
|
135
|
+
return <Provider>{ container }</Provider>;
|
|
136
|
+
}
|
|
137
|
+
`
|
|
138
|
+
});
|
|
139
|
+
api.writeTmpFile({
|
|
140
|
+
path: import_umi.RUNTIME_TYPE_FILE_NAME,
|
|
141
|
+
content: `
|
|
142
|
+
export interface IRuntimeConfig {
|
|
143
|
+
getInitialState?: () => Promise<Record<string, any>>
|
|
144
|
+
}
|
|
145
|
+
`
|
|
146
|
+
});
|
|
147
|
+
});
|
|
5
148
|
};
|
package/dist/layout.d.ts
CHANGED