@umijs/plugins 4.0.0-beta.12 → 4.0.0-beta.16
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 +4 -1
- package/dist/access.js +73 -1
- package/dist/{dayjs.d.ts → analytics.d.ts} +0 -0
- package/dist/analytics.js +67 -0
- package/dist/antd.js +0 -25
- package/dist/dva.d.ts +3 -9
- package/dist/dva.js +59 -107
- package/dist/initial-state.js +112 -1
- package/dist/layout.js +479 -1
- package/dist/locale.d.ts +1 -0
- package/dist/locale.js +199 -1
- package/dist/model.d.ts +0 -9
- package/dist/model.js +71 -74
- package/dist/moment2dayjs.d.ts +3 -0
- package/dist/moment2dayjs.js +96 -0
- package/dist/qiankun/constants.d.ts +5 -0
- package/dist/qiankun/constants.js +8 -0
- package/dist/qiankun/master.d.ts +6 -0
- package/dist/qiankun/master.js +114 -0
- package/dist/qiankun/slave.d.ts +3 -0
- package/dist/qiankun/slave.js +141 -0
- package/dist/qiankun.js +15 -1
- package/dist/request.js +297 -1
- package/dist/utils/{getIdentifierDeclaration.d.ts → astUtils.d.ts} +0 -0
- package/dist/utils/{getIdentifierDeclaration.js → astUtils.js} +0 -0
- package/dist/utils/localeUtils.d.ts +33 -0
- package/dist/utils/localeUtils.js +135 -0
- package/dist/utils/modelUtils.d.ts +35 -0
- package/dist/utils/modelUtils.js +145 -0
- package/libs/dva.ts +10 -0
- package/libs/locale/SelectLang.tpl +478 -0
- package/libs/locale/locale.tpl +82 -0
- package/libs/locale/localeExports.tpl +271 -0
- package/libs/locale/runtime.tpl +33 -0
- package/libs/model.tsx +140 -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 +262 -0
- package/libs/qiankun/master/MicroAppWithMemoHistory.tsx +43 -0
- package/libs/qiankun/master/common.ts +133 -0
- package/libs/qiankun/master/constants.ts +7 -0
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +45 -0
- package/libs/qiankun/master/masterRuntimePlugin.tsx +130 -0
- package/libs/qiankun/master/types.ts +44 -0
- package/libs/qiankun/slave/connectMaster.tsx +15 -0
- package/libs/qiankun/slave/lifecycles.ts +149 -0
- package/libs/qiankun/slave/qiankunModel.ts +19 -0
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +21 -0
- package/package.json +20 -6
- package/dist/dayjs.js +0 -5
package/README.md
CHANGED
package/dist/access.js
CHANGED
|
@@ -1,5 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const path_1 = require("path");
|
|
13
|
+
const withTmpPath_1 = require("./utils/withTmpPath");
|
|
3
14
|
exports.default = (api) => {
|
|
4
|
-
|
|
15
|
+
// TODO: route access
|
|
16
|
+
api.describe({
|
|
17
|
+
config: {
|
|
18
|
+
schema(joi) {
|
|
19
|
+
return joi.object();
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
enableBy: api.EnableBy.config,
|
|
23
|
+
});
|
|
24
|
+
api.onGenerateFiles(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
// runtime.tsx
|
|
26
|
+
api.writeTmpFile({
|
|
27
|
+
path: 'runtime.tsx',
|
|
28
|
+
content: `
|
|
29
|
+
import React from 'react';
|
|
30
|
+
import accessFactory from '@/access';
|
|
31
|
+
import { useModel } from '@@/plugin-model';
|
|
32
|
+
import { AccessContext } from './context';
|
|
33
|
+
|
|
34
|
+
function Provider(props) {
|
|
35
|
+
const { initialState } = useModel('@@initialState');
|
|
36
|
+
const access = React.useMemo(() => accessFactory(initialState), [initialState]);
|
|
37
|
+
return (
|
|
38
|
+
<AccessContext.Provider value={access}>
|
|
39
|
+
{ props.children }
|
|
40
|
+
</AccessContext.Provider>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function accessProvider(container) {
|
|
45
|
+
return <Provider>{ container }</Provider>;
|
|
46
|
+
}
|
|
47
|
+
`,
|
|
48
|
+
});
|
|
49
|
+
// index.ts
|
|
50
|
+
api.writeTmpFile({
|
|
51
|
+
path: 'index.ts',
|
|
52
|
+
content: `
|
|
53
|
+
import React from 'react';
|
|
54
|
+
import { AccessContext } from './context';
|
|
55
|
+
|
|
56
|
+
export const useAccess = () => {
|
|
57
|
+
return React.useContext(AccessContext);
|
|
58
|
+
};
|
|
59
|
+
`,
|
|
60
|
+
});
|
|
61
|
+
// context.ts
|
|
62
|
+
api.writeTmpFile({
|
|
63
|
+
path: 'context.ts',
|
|
64
|
+
content: `
|
|
65
|
+
import React from 'react';
|
|
66
|
+
export const AccessContext = React.createContext<any>(null);
|
|
67
|
+
`,
|
|
68
|
+
});
|
|
69
|
+
}));
|
|
70
|
+
api.addRuntimePlugin(() => {
|
|
71
|
+
return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
|
|
72
|
+
});
|
|
73
|
+
api.addTmpGenerateWatcherPaths(() => [
|
|
74
|
+
(0, path_1.join)(api.paths.absSrcPath, 'access.ts'),
|
|
75
|
+
(0, path_1.join)(api.paths.absSrcPath, 'access.js'),
|
|
76
|
+
]);
|
|
5
77
|
};
|
|
File without changes
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (api) => {
|
|
4
|
+
const GA_KEY = process.env.GA_KEY;
|
|
5
|
+
const enableBy = (opts) => {
|
|
6
|
+
return opts.config.analytics || GA_KEY;
|
|
7
|
+
};
|
|
8
|
+
api.describe({
|
|
9
|
+
key: 'analytics',
|
|
10
|
+
config: {
|
|
11
|
+
schema(joi) {
|
|
12
|
+
return joi.object();
|
|
13
|
+
},
|
|
14
|
+
onChange: api.ConfigChangeType.reload,
|
|
15
|
+
},
|
|
16
|
+
enableBy,
|
|
17
|
+
});
|
|
18
|
+
// https://tongji.baidu.com/web/help/article?id=174&type=0
|
|
19
|
+
const baiduTpl = (code) => {
|
|
20
|
+
return `
|
|
21
|
+
(function() {
|
|
22
|
+
var hm = document.createElement('script');
|
|
23
|
+
hm.src = 'https://hm.baidu.com/hm.js?${code}';
|
|
24
|
+
var s = document.getElementsByTagName('script')[0];
|
|
25
|
+
s.parentNode.insertBefore(hm, s);
|
|
26
|
+
})();
|
|
27
|
+
`;
|
|
28
|
+
};
|
|
29
|
+
const gaTpl = (code) => {
|
|
30
|
+
return `
|
|
31
|
+
(function(){
|
|
32
|
+
if (!location.port) {
|
|
33
|
+
(function (i, s, o, g, r, a, m) {
|
|
34
|
+
i['GoogleAnalyticsObject'] = r;
|
|
35
|
+
i[r] = i[r] || function () {
|
|
36
|
+
(i[r].q = i[r].q || []).push(arguments)
|
|
37
|
+
}, i[r].l = 1 * new Date();
|
|
38
|
+
a = s.createElement(o),
|
|
39
|
+
m = s.getElementsByTagName(o)[0];
|
|
40
|
+
a.async = 1;
|
|
41
|
+
a.src = g;
|
|
42
|
+
m.parentNode.insertBefore(a, m)
|
|
43
|
+
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
|
|
44
|
+
ga('create', '${code}', 'auto');
|
|
45
|
+
ga('send', 'pageview');
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
48
|
+
`;
|
|
49
|
+
};
|
|
50
|
+
api.addHTMLHeadScripts(() => {
|
|
51
|
+
const { analytics = {} } = api.config;
|
|
52
|
+
const { ga = GA_KEY, baidu } = analytics;
|
|
53
|
+
return [
|
|
54
|
+
baidu && {
|
|
55
|
+
content: 'var _hmt = _hmt || [];',
|
|
56
|
+
},
|
|
57
|
+
api.env !== 'development' &&
|
|
58
|
+
baidu && {
|
|
59
|
+
content: baiduTpl(baidu),
|
|
60
|
+
},
|
|
61
|
+
api.env !== 'development' &&
|
|
62
|
+
ga && {
|
|
63
|
+
content: gaTpl(ga),
|
|
64
|
+
},
|
|
65
|
+
].filter(Boolean);
|
|
66
|
+
});
|
|
67
|
+
};
|
package/dist/antd.js
CHANGED
|
@@ -65,31 +65,6 @@ exports.default = (api) => {
|
|
|
65
65
|
]
|
|
66
66
|
: [];
|
|
67
67
|
});
|
|
68
|
-
// if (dayjs !== false) {
|
|
69
|
-
// api.onGenerateFiles({
|
|
70
|
-
// fn: () => {
|
|
71
|
-
// const { plugins } = getConfig(api);
|
|
72
|
-
//
|
|
73
|
-
// const runtimeTpl = readFileSync(
|
|
74
|
-
// join(__dirname, '../templates/antd/dayjs.tpl'),
|
|
75
|
-
// 'utf-8',
|
|
76
|
-
// );
|
|
77
|
-
// api.writeTmpFile({
|
|
78
|
-
// path: 'plugin-antd/dayjs.tsx',
|
|
79
|
-
// content: Mustache.render(runtimeTpl, {
|
|
80
|
-
// plugins,
|
|
81
|
-
// dayjsPath: dirname(require.resolve('dayjs/package.json')),
|
|
82
|
-
// dayjsPluginPath: dirname(
|
|
83
|
-
// require.resolve('antd-dayjs-webpack-plugin/package.json'),
|
|
84
|
-
// ),
|
|
85
|
-
// }),
|
|
86
|
-
// });
|
|
87
|
-
// },
|
|
88
|
-
// });
|
|
89
|
-
// api.addEntryCodeAhead(() => {
|
|
90
|
-
// return [`import './${DIR_NAME}/dayjs.tsx'`];
|
|
91
|
-
// });
|
|
92
|
-
// }
|
|
93
68
|
// antd config provider
|
|
94
69
|
api.onGenerateFiles(() => {
|
|
95
70
|
if (!api.config.antd.config)
|
package/dist/dva.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { IApi } from 'umi';
|
|
2
|
+
import { Model, ModelUtils } from './utils/modelUtils';
|
|
2
3
|
declare const _default: (api: IApi) => void;
|
|
3
4
|
export default _default;
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
6
|
-
base: string;
|
|
7
|
-
pattern?: string;
|
|
8
|
-
}): string[];
|
|
9
|
-
export declare function isModelValid(opts: {
|
|
10
|
-
content: string;
|
|
11
|
-
file: string;
|
|
12
|
-
}): boolean;
|
|
5
|
+
export declare function getModelUtil(api: IApi | null): ModelUtils;
|
|
6
|
+
export declare function getAllModels(api: IApi): Model[];
|
package/dist/dva.js
CHANGED
|
@@ -18,84 +18,82 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
-
};
|
|
24
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.
|
|
26
|
-
const parser = __importStar(require("@umijs/bundler-utils/compiled/babel/parser"));
|
|
27
|
-
const traverse_1 = __importDefault(require("@umijs/bundler-utils/compiled/babel/traverse"));
|
|
22
|
+
exports.getAllModels = exports.getModelUtil = void 0;
|
|
28
23
|
const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
29
|
-
const esbuild_1 = require("@umijs/bundler-utils/compiled/esbuild");
|
|
30
|
-
const fs_1 = require("fs");
|
|
31
24
|
const path_1 = require("path");
|
|
32
25
|
const plugin_utils_1 = require("umi/plugin-utils");
|
|
33
|
-
const
|
|
34
|
-
const resolveProjectDep_1 = require("./utils/resolveProjectDep");
|
|
26
|
+
const modelUtils_1 = require("./utils/modelUtils");
|
|
35
27
|
const withTmpPath_1 = require("./utils/withTmpPath");
|
|
36
28
|
exports.default = (api) => {
|
|
37
|
-
const pkgPath = (0,
|
|
38
|
-
pkg: api.pkg,
|
|
39
|
-
cwd: api.cwd,
|
|
40
|
-
dep: 'dva',
|
|
41
|
-
}) || (0, path_1.dirname)(require.resolve('dva/package.json'));
|
|
29
|
+
const pkgPath = (0, path_1.join)(__dirname, '../libs/dva.ts');
|
|
42
30
|
api.describe({
|
|
43
31
|
config: {
|
|
44
32
|
schema(Joi) {
|
|
45
|
-
return Joi.object(
|
|
33
|
+
return Joi.object({
|
|
34
|
+
extraModels: Joi.array().items(Joi.string()),
|
|
35
|
+
});
|
|
46
36
|
},
|
|
47
37
|
},
|
|
48
38
|
enableBy: api.EnableBy.config,
|
|
49
39
|
});
|
|
50
40
|
api.modifyAppData((memo) => {
|
|
51
|
-
const
|
|
52
|
-
memo.
|
|
41
|
+
const models = getAllModels(api);
|
|
42
|
+
memo.pluginDva = {
|
|
53
43
|
pkgPath,
|
|
54
|
-
|
|
44
|
+
models,
|
|
55
45
|
};
|
|
56
46
|
return memo;
|
|
57
47
|
});
|
|
58
48
|
api.modifyConfig((memo) => {
|
|
59
49
|
// import from dva
|
|
60
|
-
memo.alias
|
|
61
|
-
return memo;
|
|
62
|
-
});
|
|
63
|
-
api.modifyAppData((memo) => {
|
|
64
|
-
const models = getAllModels(api);
|
|
65
|
-
memo.pluginDva = {
|
|
66
|
-
pkgPath,
|
|
67
|
-
models,
|
|
68
|
-
};
|
|
50
|
+
memo.alias['dva$'] = pkgPath;
|
|
69
51
|
return memo;
|
|
70
52
|
});
|
|
71
53
|
api.onGenerateFiles((args) => {
|
|
72
54
|
const models = args.isFirstTime
|
|
73
55
|
? api.appData.pluginDva.models
|
|
74
56
|
: getAllModels(api);
|
|
75
|
-
models
|
|
57
|
+
// models.ts
|
|
58
|
+
api.writeTmpFile({
|
|
59
|
+
path: 'models.ts',
|
|
60
|
+
content: modelUtils_1.ModelUtils.getModelsContent(models),
|
|
61
|
+
});
|
|
76
62
|
// dva.tsx
|
|
77
63
|
api.writeTmpFile({
|
|
78
64
|
path: 'dva.tsx',
|
|
79
65
|
tpl: `
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
import {
|
|
66
|
+
// It's faked dva
|
|
67
|
+
// aliased to @umijs/plugins/templates/dva
|
|
68
|
+
import { create, Provider } from 'dva';
|
|
69
|
+
import React, { useRef } from 'react';
|
|
70
|
+
import { useAppData } from 'umi';
|
|
71
|
+
import { models } from './models';
|
|
83
72
|
|
|
84
73
|
export function RootContainer(props: any) {
|
|
85
|
-
const { navigator } =
|
|
86
|
-
const app = useRef();
|
|
74
|
+
const { navigator } = useAppData();
|
|
75
|
+
const app = useRef<any>();
|
|
87
76
|
if (!app.current) {
|
|
88
|
-
app.current =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
77
|
+
app.current = create(
|
|
78
|
+
{
|
|
79
|
+
history: navigator,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
initialReducer: {},
|
|
83
|
+
setupMiddlewares(middlewares: Function[]) {
|
|
84
|
+
return [...middlewares];
|
|
85
|
+
},
|
|
86
|
+
setupApp(app: IDvaApp) {
|
|
87
|
+
app._history = navigator;
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
for (const id of Object.keys(models)) {
|
|
92
|
+
app.current.model(models[id].model);
|
|
93
|
+
}
|
|
94
|
+
app.current.start();
|
|
97
95
|
}
|
|
98
|
-
return app.current.
|
|
96
|
+
return <Provider store={app.current!._store}>{props.children}</Provider>;
|
|
99
97
|
}
|
|
100
98
|
`,
|
|
101
99
|
context: {},
|
|
@@ -131,83 +129,37 @@ export { connect, useDispatch, useStore, useSelector } from 'dva';`,
|
|
|
131
129
|
fn() {
|
|
132
130
|
api.logger.info(plugin_utils_1.chalk.green.bold('dva models'));
|
|
133
131
|
api.appData.pluginDva.models.forEach((model) => {
|
|
134
|
-
api.logger.info(` - ${model}`);
|
|
132
|
+
api.logger.info(` - ${(0, path_1.relative)(api.cwd, model.file)}`);
|
|
135
133
|
});
|
|
136
134
|
},
|
|
137
135
|
});
|
|
138
136
|
};
|
|
139
|
-
function
|
|
140
|
-
return
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
})
|
|
145
|
-
getModels({
|
|
146
|
-
base: (0, path_1.join)(api.paths.absPagesPath),
|
|
147
|
-
pattern: '**/models/**/*.{ts,tsx,js,jsx}',
|
|
148
|
-
}),
|
|
149
|
-
getModels({
|
|
150
|
-
base: (0, path_1.join)(api.paths.absPagesPath),
|
|
151
|
-
pattern: '**/model.{ts,tsx,js,jsx}',
|
|
152
|
-
}),
|
|
153
|
-
];
|
|
154
|
-
}
|
|
155
|
-
exports.getAllModels = getAllModels;
|
|
156
|
-
function getModels(opts) {
|
|
157
|
-
return plugin_utils_1.glob
|
|
158
|
-
.sync(opts.pattern || '**/*.{ts,js}', {
|
|
159
|
-
cwd: opts.base,
|
|
160
|
-
absolute: true,
|
|
161
|
-
})
|
|
162
|
-
.map(plugin_utils_1.winPath)
|
|
163
|
-
.filter((file) => {
|
|
164
|
-
if (/\.d.ts$/.test(file))
|
|
165
|
-
return false;
|
|
166
|
-
if (/\.(test|e2e|spec).([jt])sx?$/.test(file))
|
|
167
|
-
return false;
|
|
168
|
-
const content = (0, fs_1.readFileSync)(file, 'utf-8');
|
|
169
|
-
return isModelValid({ content, file });
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
exports.getModels = getModels;
|
|
173
|
-
function isModelValid(opts) {
|
|
174
|
-
const { file, content } = opts;
|
|
175
|
-
// 标注式声明
|
|
176
|
-
if (content.startsWith('// @dva-model'))
|
|
177
|
-
return true;
|
|
178
|
-
// transform with esbuild first
|
|
179
|
-
// to reduce unexpected ast problem
|
|
180
|
-
const loader = (0, path_1.extname)(file).slice(1);
|
|
181
|
-
const result = (0, esbuild_1.transformSync)(content, {
|
|
182
|
-
loader,
|
|
183
|
-
sourcemap: false,
|
|
184
|
-
minify: false,
|
|
185
|
-
});
|
|
186
|
-
// transform with babel
|
|
187
|
-
let ret = false;
|
|
188
|
-
const ast = parser.parse(result.code, {
|
|
189
|
-
sourceType: 'module',
|
|
190
|
-
sourceFilename: file,
|
|
191
|
-
plugins: [],
|
|
192
|
-
});
|
|
193
|
-
(0, traverse_1.default)(ast, {
|
|
194
|
-
ExportDefaultDeclaration(path) {
|
|
195
|
-
let node = path.node.declaration;
|
|
196
|
-
node = (0, getIdentifierDeclaration_1.getIdentifierDeclaration)(node, path);
|
|
137
|
+
function getModelUtil(api) {
|
|
138
|
+
return new modelUtils_1.ModelUtils(api, {
|
|
139
|
+
contentTest(content) {
|
|
140
|
+
return content.startsWith('// @dva-model');
|
|
141
|
+
},
|
|
142
|
+
astTest({ node, content }) {
|
|
197
143
|
if (isModelObject(node)) {
|
|
198
|
-
|
|
144
|
+
return true;
|
|
199
145
|
}
|
|
200
146
|
else if (content.includes('dva-model-extend') &&
|
|
201
147
|
t.isCallExpression(node) &&
|
|
202
148
|
node.arguments.length === 2 &&
|
|
203
149
|
isModelObject(node.arguments[1])) {
|
|
204
|
-
|
|
150
|
+
return true;
|
|
205
151
|
}
|
|
152
|
+
return false;
|
|
206
153
|
},
|
|
207
154
|
});
|
|
208
|
-
return ret;
|
|
209
155
|
}
|
|
210
|
-
exports.
|
|
156
|
+
exports.getModelUtil = getModelUtil;
|
|
157
|
+
function getAllModels(api) {
|
|
158
|
+
return getModelUtil(api).getAllModels({
|
|
159
|
+
extraModels: [...(api.config.dva.extraModels || [])],
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
exports.getAllModels = getAllModels;
|
|
211
163
|
function isModelObject(node) {
|
|
212
164
|
return (t.isObjectExpression(node) &&
|
|
213
165
|
node.properties.some((property) => {
|
package/dist/initial-state.js
CHANGED
|
@@ -1,5 +1,116 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const withTmpPath_1 = require("./utils/withTmpPath");
|
|
3
4
|
exports.default = (api) => {
|
|
4
|
-
api
|
|
5
|
+
api.describe({
|
|
6
|
+
config: {
|
|
7
|
+
schema(Joi) {
|
|
8
|
+
return Joi.object({
|
|
9
|
+
loading: Joi.string(),
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
enableBy: api.EnableBy.config,
|
|
14
|
+
});
|
|
15
|
+
api.register({
|
|
16
|
+
key: 'addExtraModels',
|
|
17
|
+
fn: () => [(0, withTmpPath_1.withTmpPath)({ api, path: '@@initialState.ts' })],
|
|
18
|
+
});
|
|
19
|
+
api.addRuntimePluginKey(() => ['getInitialState']);
|
|
20
|
+
api.addRuntimePlugin(() => {
|
|
21
|
+
return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
|
|
22
|
+
});
|
|
23
|
+
api.onGenerateFiles(() => {
|
|
24
|
+
var _a;
|
|
25
|
+
const { loading } = api.config.initialState;
|
|
26
|
+
// Provider.tsx
|
|
27
|
+
api.writeTmpFile({
|
|
28
|
+
path: 'Provider.tsx',
|
|
29
|
+
content: `
|
|
30
|
+
import React from 'react';
|
|
31
|
+
import { useModel } from '@@/plugin-model';
|
|
32
|
+
${loading
|
|
33
|
+
? `import Loading from ${loading}`
|
|
34
|
+
: `function Loading() { return <div>loading</div>; }`}
|
|
35
|
+
export default function InitialStateProvider(props: any) {
|
|
36
|
+
const appLoaded = React.useRef(false);
|
|
37
|
+
const { loading = false } = useModel("@@initialState") || {};
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
if (!loading) {
|
|
40
|
+
appLoaded.current = true;
|
|
41
|
+
}
|
|
42
|
+
}, [loading]);
|
|
43
|
+
if (loading && !appLoaded.current) {
|
|
44
|
+
return <Loading />;
|
|
45
|
+
}
|
|
46
|
+
return props.children;
|
|
47
|
+
}
|
|
48
|
+
`,
|
|
49
|
+
});
|
|
50
|
+
// @@initialState.ts
|
|
51
|
+
api.writeTmpFile({
|
|
52
|
+
path: '@@initialState.ts',
|
|
53
|
+
content: ((_a = api.appData.appJS) === null || _a === void 0 ? void 0 : _a.exports.includes('getInitialState'))
|
|
54
|
+
? `
|
|
55
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
56
|
+
import { getInitialState } from '@/app';
|
|
57
|
+
|
|
58
|
+
const initState = {
|
|
59
|
+
initialState: undefined,
|
|
60
|
+
loading: true,
|
|
61
|
+
error: undefined,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default () => {
|
|
65
|
+
const [state, setState] = useState(initState);
|
|
66
|
+
const refresh = useCallback(async () => {
|
|
67
|
+
setState((s) => ({ ...s, loading: true, error: undefined }));
|
|
68
|
+
try {
|
|
69
|
+
const ret = await getInitialState();
|
|
70
|
+
setState((s) => ({ ...s, initialState: ret, loading: false }));
|
|
71
|
+
} catch (e) {
|
|
72
|
+
setState((s) => ({ ...s, error: e, loading: false }));
|
|
73
|
+
}
|
|
74
|
+
// [?]
|
|
75
|
+
// await sleep(10);
|
|
76
|
+
}, []);
|
|
77
|
+
|
|
78
|
+
const setInitialState = useCallback(async (initialState) => {
|
|
79
|
+
setState((s) => {
|
|
80
|
+
if (typeof initialState === 'function') {
|
|
81
|
+
return { ...s, initialState: initialState(s.initialState), loading: false };
|
|
82
|
+
}
|
|
83
|
+
return { ...s, initialState, loading: false };
|
|
84
|
+
});
|
|
85
|
+
// [?]
|
|
86
|
+
// await sleep(10)
|
|
87
|
+
}, []);
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
refresh();
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
...state,
|
|
95
|
+
refresh,
|
|
96
|
+
setInitialState,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
`
|
|
100
|
+
: `
|
|
101
|
+
export default () => ({ loading: false, refresh: () => {} })
|
|
102
|
+
`,
|
|
103
|
+
});
|
|
104
|
+
// runtime.tsx
|
|
105
|
+
api.writeTmpFile({
|
|
106
|
+
path: 'runtime.tsx',
|
|
107
|
+
content: `
|
|
108
|
+
import React from 'react';
|
|
109
|
+
import Provider from './Provider';
|
|
110
|
+
export function innerProvider(container) {
|
|
111
|
+
return <Provider>{ container }</Provider>;
|
|
112
|
+
}
|
|
113
|
+
`,
|
|
114
|
+
});
|
|
115
|
+
});
|
|
5
116
|
};
|