@umijs/plugins 4.0.0-canary.20220429.2 → 4.0.0-canary.20220506.1

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.
Files changed (53) hide show
  1. package/dist/access.d.ts +3 -0
  2. package/dist/access.js +139 -0
  3. package/dist/analytics.d.ts +3 -0
  4. package/dist/analytics.js +67 -0
  5. package/dist/antd.d.ts +3 -0
  6. package/dist/antd.js +137 -0
  7. package/dist/dva.d.ts +6 -0
  8. package/dist/dva.js +198 -0
  9. package/dist/icons.d.ts +3 -0
  10. package/dist/icons.js +5 -0
  11. package/dist/initial-state.d.ts +3 -0
  12. package/dist/initial-state.js +116 -0
  13. package/dist/layout.d.ts +3 -0
  14. package/dist/layout.js +538 -0
  15. package/dist/locale.d.ts +4 -0
  16. package/dist/locale.js +199 -0
  17. package/dist/model.d.ts +3 -0
  18. package/dist/model.js +101 -0
  19. package/dist/moment2dayjs.d.ts +3 -0
  20. package/dist/moment2dayjs.js +96 -0
  21. package/dist/qiankun/constants.d.ts +5 -0
  22. package/dist/qiankun/constants.js +8 -0
  23. package/dist/qiankun/master.d.ts +6 -0
  24. package/dist/qiankun/master.js +134 -0
  25. package/dist/qiankun/slave.d.ts +3 -0
  26. package/dist/qiankun/slave.js +158 -0
  27. package/dist/qiankun.d.ts +3 -0
  28. package/dist/qiankun.js +19 -0
  29. package/dist/request.d.ts +3 -0
  30. package/dist/request.js +311 -0
  31. package/dist/tailwindcss.d.ts +3 -0
  32. package/dist/tailwindcss.js +40 -0
  33. package/dist/unocss.d.ts +3 -0
  34. package/dist/unocss.js +39 -0
  35. package/dist/utils/astUtils.d.ts +3 -0
  36. package/dist/utils/astUtils.js +38 -0
  37. package/dist/utils/localeUtils.d.ts +33 -0
  38. package/dist/utils/localeUtils.js +126 -0
  39. package/dist/utils/modelUtils.d.ts +39 -0
  40. package/dist/utils/modelUtils.js +231 -0
  41. package/dist/utils/resolveProjectDep.d.ts +5 -0
  42. package/dist/utils/resolveProjectDep.js +15 -0
  43. package/dist/utils/withTmpPath.d.ts +6 -0
  44. package/dist/utils/withTmpPath.js +11 -0
  45. package/libs/qiankun/master/MicroApp.tsx +7 -0
  46. package/libs/qiankun/master/common.ts +13 -10
  47. package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +5 -5
  48. package/libs/qiankun/master/masterRuntimePlugin.tsx +5 -5
  49. package/libs/qiankun/slave/connectMaster.tsx +0 -1
  50. package/libs/qiankun/slave/lifecycles.ts +6 -8
  51. package/libs/qiankun/slave/qiankunModel.ts +0 -1
  52. package/libs/qiankun/slave/slaveRuntimePlugin.ts +9 -15
  53. package/package.json +3 -3
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
package/dist/access.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const path_1 = require("path");
8
+ const withTmpPath_1 = require("./utils/withTmpPath");
9
+ exports.default = (api) => {
10
+ api.describe({
11
+ config: {
12
+ schema(joi) {
13
+ return joi.object();
14
+ },
15
+ },
16
+ enableBy: api.EnableBy.config,
17
+ });
18
+ api.onGenerateFiles(async () => {
19
+ // allow enable access without access file
20
+ const hasAccessFile = ['js', 'jsx', 'ts', 'tsx'].some((ext) => fs_1.default.existsSync((0, path_1.join)(api.paths.absSrcPath, `access.${ext}`)));
21
+ // runtime.tsx
22
+ api.writeTmpFile({
23
+ path: 'runtime.tsx',
24
+ content: `
25
+ import React from 'react';${hasAccessFile
26
+ ? `
27
+ import accessFactory from '@/access'
28
+ import { useModel } from '@@/plugin-model';
29
+ `
30
+ : ''}
31
+ import { AccessContext } from './context';
32
+
33
+ function Provider(props) {${hasAccessFile
34
+ ? `
35
+ const { initialState } = useModel('@@initialState');
36
+ const access = React.useMemo(() => accessFactory(initialState), [initialState]);
37
+ `
38
+ : `
39
+ const access = {};
40
+ `}
41
+ return (
42
+ <AccessContext.Provider value={access}>
43
+ { props.children }
44
+ </AccessContext.Provider>
45
+ );
46
+ }
47
+
48
+ export function accessProvider(container) {
49
+ return <Provider>{ container }</Provider>;
50
+ }
51
+ `,
52
+ });
53
+ // index.tsx
54
+ api.writeTmpFile({
55
+ path: 'index.tsx',
56
+ content: `
57
+ import React from 'react';
58
+ import { AccessContext } from './context';
59
+ import type { IRoute } from 'umi';
60
+
61
+ export const useAccess = () => {
62
+ return React.useContext(AccessContext);
63
+ };
64
+
65
+ export interface AccessProps {
66
+ accessible: boolean;
67
+ fallback?: React.ReactNode;
68
+ }
69
+ export const Access: React.FC<AccessProps> = (props) => {
70
+ if (process.env.NODE_ENV === 'development' && typeof props.accessible !== 'boolean') {
71
+ throw new Error('[access] the \`accessible\` property on <Access /> should be a boolean');
72
+ }
73
+
74
+ return <>{ props.accessible ? props.children : props.fallback }</>;
75
+ };
76
+
77
+ export const useAccessMarkedRoutes = (routes: IRoute[]) => {
78
+ const access = useAccess();
79
+ const markdedRoutes: IRoute[] = React.useMemo(() => {
80
+ const process = (route, parentAccessCode) => {
81
+ const accessCode = route.access || parentAccessCode;
82
+
83
+ // set default status
84
+ route.unaccessible = ${api.config.access.strictMode ? 'true' : 'false'};
85
+
86
+ // check access code
87
+ if (typeof accessCode === 'string') {
88
+ const detector = access[route.access];
89
+
90
+ if (typeof detector === 'function') {
91
+ route.unaccessible = !detector(route);
92
+ } else if (typeof detector === 'boolean') {
93
+ route.unaccessible = !detector;
94
+ } else if (typeof detector === 'undefined') {
95
+ route.unaccessible = true;
96
+ }
97
+ }
98
+
99
+ // check children access code
100
+ if (route.routes) {
101
+ const isNoAccessibleChild = !route.routes.reduce((hasAccessibleChild, child) => {
102
+ process(child, accessCode);
103
+
104
+ return hasAccessibleChild || !child.unaccessible;
105
+ }, false);
106
+
107
+ // make sure parent route is unaccessible if all children are unaccessible
108
+ if (isNoAccessibleChild) {
109
+ route.unaccessible = true;
110
+ }
111
+ }
112
+
113
+ return route;
114
+ }
115
+
116
+ return routes.map(route => process(route));
117
+ }, [routes.length]);
118
+
119
+ return markdedRoutes;
120
+ }
121
+ `,
122
+ });
123
+ // context.ts
124
+ api.writeTmpFile({
125
+ path: 'context.ts',
126
+ content: `
127
+ import React from 'react';
128
+ export const AccessContext = React.createContext<any>(null);
129
+ `,
130
+ });
131
+ });
132
+ api.addRuntimePlugin(() => {
133
+ return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
134
+ });
135
+ api.addTmpGenerateWatcherPaths(() => [
136
+ (0, path_1.join)(api.paths.absSrcPath, 'access.ts'),
137
+ (0, path_1.join)(api.paths.absSrcPath, 'access.js'),
138
+ ]);
139
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -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.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
package/dist/antd.js ADDED
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const path_1 = require("path");
4
+ const plugin_utils_1 = require("umi/plugin-utils");
5
+ const resolveProjectDep_1 = require("./utils/resolveProjectDep");
6
+ const withTmpPath_1 = require("./utils/withTmpPath");
7
+ exports.default = (api) => {
8
+ let pkgPath;
9
+ try {
10
+ pkgPath =
11
+ (0, resolveProjectDep_1.resolveProjectDep)({
12
+ pkg: api.pkg,
13
+ cwd: api.cwd,
14
+ dep: 'antd',
15
+ }) || (0, path_1.dirname)(require.resolve('antd/package.json'));
16
+ }
17
+ catch (e) { }
18
+ api.describe({
19
+ config: {
20
+ schema(Joi) {
21
+ return Joi.object({
22
+ configProvider: Joi.object(),
23
+ // themes
24
+ dark: Joi.boolean(),
25
+ compact: Joi.boolean(),
26
+ // babel-plugin-import
27
+ import: Joi.boolean(),
28
+ // less or css, default less
29
+ style: Joi.string().allow('less', 'css'),
30
+ });
31
+ },
32
+ },
33
+ enableBy: api.EnableBy.config,
34
+ });
35
+ function checkPkgPath() {
36
+ if (!pkgPath) {
37
+ throw new Error(`Can't find antd package. Please install antd first.`);
38
+ }
39
+ }
40
+ api.modifyAppData((memo) => {
41
+ checkPkgPath();
42
+ const version = require(`${pkgPath}/package.json`).version;
43
+ memo.antd = {
44
+ pkgPath,
45
+ version,
46
+ };
47
+ return memo;
48
+ });
49
+ api.modifyConfig((memo) => {
50
+ checkPkgPath();
51
+ // antd import
52
+ memo.alias.antd = pkgPath;
53
+ // moment > dayjs
54
+ if (memo.antd.dayjs) {
55
+ memo.alias.moment = (0, path_1.dirname)(require.resolve('dayjs/package.json'));
56
+ }
57
+ // dark mode & compact mode
58
+ if (memo.antd.dark || memo.antd.compact) {
59
+ const { getThemeVariables } = require('antd/dist/theme');
60
+ memo.theme = {
61
+ ...getThemeVariables(memo.antd),
62
+ ...memo.theme,
63
+ };
64
+ }
65
+ return memo;
66
+ });
67
+ api.modifyConfig((memo) => {
68
+ memo.theme = {
69
+ 'root-entry-name': 'default',
70
+ ...memo.theme,
71
+ };
72
+ return memo;
73
+ });
74
+ // babel-plugin-import
75
+ api.addExtraBabelPlugins(() => {
76
+ const style = api.config.antd.style || 'less';
77
+ return api.config.antd.import && !api.appData.vite
78
+ ? [
79
+ [
80
+ require.resolve('babel-plugin-import'),
81
+ {
82
+ libraryName: 'antd',
83
+ libraryDirectory: 'es',
84
+ style: style === 'less' ? true : 'css',
85
+ },
86
+ 'antd',
87
+ ],
88
+ ]
89
+ : [];
90
+ });
91
+ // antd config provider
92
+ api.onGenerateFiles(() => {
93
+ if (!api.config.antd.configProvider)
94
+ return;
95
+ api.writeTmpFile({
96
+ path: `runtime.tsx`,
97
+ content: plugin_utils_1.Mustache.render(`
98
+ import React from 'react';
99
+ import { ConfigProvider, Modal, message, notification } from 'antd';
100
+
101
+ export function rootContainer(container) {
102
+ const finalConfig = {...{{{ config }}}}
103
+ if (finalConfig.prefixCls) {
104
+ Modal.config({
105
+ rootPrefixCls: finalConfig.prefixCls
106
+ });
107
+ message.config({
108
+ prefixCls: \`\${finalConfig.prefixCls}-message\`
109
+ });
110
+ notification.config({
111
+ prefixCls: \`\${finalConfig.prefixCls}-notification\`
112
+ });
113
+ }
114
+ return <ConfigProvider {...finalConfig}>{container}</ConfigProvider>;
115
+ }
116
+ `.trim(), {
117
+ config: JSON.stringify(api.config.antd.configProvider),
118
+ }),
119
+ });
120
+ });
121
+ api.addRuntimePlugin(() => {
122
+ return api.config.antd.configProvider
123
+ ? [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })]
124
+ : [];
125
+ });
126
+ // import antd style if antd.import is not configured
127
+ api.addEntryImportsAhead(() => {
128
+ const style = api.config.antd.style || 'less';
129
+ return api.config.antd.import && !api.appData.vite
130
+ ? []
131
+ : [
132
+ {
133
+ source: style === 'less' ? 'antd/dist/antd.less' : 'antd/dist/antd.css',
134
+ },
135
+ ];
136
+ });
137
+ };
package/dist/dva.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { IApi } from 'umi';
2
+ import { Model, ModelUtils } from './utils/modelUtils';
3
+ declare const _default: (api: IApi) => void;
4
+ export default _default;
5
+ export declare function getModelUtil(api: IApi | null): ModelUtils;
6
+ export declare function getAllModels(api: IApi): Model[];
package/dist/dva.js ADDED
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.getAllModels = exports.getModelUtil = void 0;
27
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
28
+ const utils_1 = require("@umijs/utils");
29
+ const path_1 = require("path");
30
+ const plugin_utils_1 = require("umi/plugin-utils");
31
+ const modelUtils_1 = require("./utils/modelUtils");
32
+ const withTmpPath_1 = require("./utils/withTmpPath");
33
+ exports.default = (api) => {
34
+ const pkgPath = (0, path_1.join)(__dirname, '../libs/dva.ts');
35
+ api.describe({
36
+ config: {
37
+ schema(Joi) {
38
+ return Joi.object({
39
+ extraModels: Joi.array().items(Joi.string()),
40
+ immer: Joi.object(),
41
+ });
42
+ },
43
+ },
44
+ enableBy: api.EnableBy.config,
45
+ });
46
+ api.modifyAppData((memo) => {
47
+ const models = getAllModels(api);
48
+ memo.pluginDva = {
49
+ pkgPath,
50
+ models,
51
+ };
52
+ return memo;
53
+ });
54
+ api.modifyConfig((memo) => {
55
+ // import from dva
56
+ memo.alias['dva$'] = pkgPath;
57
+ return memo;
58
+ });
59
+ api.onGenerateFiles((args) => {
60
+ var _a, _b, _c, _d, _e, _f;
61
+ const models = args.isFirstTime
62
+ ? api.appData.pluginDva.models
63
+ : getAllModels(api);
64
+ // models.ts
65
+ api.writeTmpFile({
66
+ path: 'models.ts',
67
+ content: modelUtils_1.ModelUtils.getModelsContent(models),
68
+ });
69
+ // dva.tsx
70
+ api.writeTmpFile({
71
+ path: 'dva.tsx',
72
+ tpl: `
73
+ // It's faked dva
74
+ // aliased to @umijs/plugins/templates/dva
75
+ import { create, Provider } from 'dva';
76
+ import createLoading from '${(0, utils_1.winPath)(require.resolve('dva-loading'))}';
77
+ ${((_a = api.config.dva) === null || _a === void 0 ? void 0 : _a.immer)
78
+ ? `
79
+ import dvaImmer, { enableES5, enableAllPlugins } from '${(0, utils_1.winPath)(require.resolve('dva-immer'))}';
80
+ `
81
+ : ''}
82
+ import React, { useRef } from 'react';
83
+ import { history, ApplyPluginsType, useAppData } from 'umi';
84
+ import { models } from './models';
85
+
86
+ export function RootContainer(props: any) {
87
+ const { pluginManager } = useAppData();
88
+ const app = useRef<any>();
89
+ const runtimeDva = pluginManager.applyPlugins({
90
+ key: 'dva',
91
+ type: ApplyPluginsType.modify,
92
+ initialValue: {},
93
+ });
94
+ if (!app.current) {
95
+ app.current = create(
96
+ {
97
+ history,
98
+ ...(runtimeDva.config || {}),
99
+ },
100
+ {
101
+ initialReducer: {},
102
+ setupMiddlewares(middlewares: Function[]) {
103
+ return [...middlewares];
104
+ },
105
+ setupApp(app: IDvaApp) {
106
+ app._history = history;
107
+ },
108
+ },
109
+ );
110
+ app.current.use(createLoading());
111
+ ${((_b = api.config.dva) === null || _b === void 0 ? void 0 : _b.immer) ? `app.current.use(dvaImmer());` : ''}
112
+ ${((_d = (_c = api.config.dva) === null || _c === void 0 ? void 0 : _c.immer) === null || _d === void 0 ? void 0 : _d.enableES5) ? `enableES5();` : ''}
113
+ ${((_f = (_e = api.config.dva) === null || _e === void 0 ? void 0 : _e.immer) === null || _f === void 0 ? void 0 : _f.enableAllPlugins) ? `enableAllPlugins();` : ''}
114
+ for (const id of Object.keys(models)) {
115
+ app.current.model(models[id].model);
116
+ }
117
+ app.current.start();
118
+ }
119
+ return <Provider store={app.current!._store}>{props.children}</Provider>;
120
+ }
121
+ `,
122
+ context: {},
123
+ });
124
+ // runtime.tsx
125
+ api.writeTmpFile({
126
+ path: 'runtime.tsx',
127
+ content: `
128
+ import React from 'react';
129
+ import { RootContainer } from './dva';
130
+
131
+ export function dataflowProvider(container, opts) {
132
+ return React.createElement(RootContainer, opts, container);
133
+ }
134
+ `,
135
+ });
136
+ // index.ts for export
137
+ api.writeTmpFile({
138
+ path: 'index.ts',
139
+ content: `
140
+ export { connect, useDispatch, useStore, useSelector } from 'dva';`,
141
+ });
142
+ });
143
+ api.addTmpGenerateWatcherPaths(() => {
144
+ return [(0, path_1.join)(api.paths.absSrcPath, 'models')];
145
+ });
146
+ api.addRuntimePlugin(() => {
147
+ return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
148
+ });
149
+ api.addRuntimePluginKey(() => ['dva']);
150
+ // dva list model
151
+ api.registerCommand({
152
+ name: 'dva',
153
+ fn() {
154
+ api.logger.info(plugin_utils_1.chalk.green.bold('dva models'));
155
+ api.appData.pluginDva.models.forEach((model) => {
156
+ api.logger.info(` - ${(0, path_1.relative)(api.cwd, model.file)}`);
157
+ });
158
+ },
159
+ });
160
+ };
161
+ function getModelUtil(api) {
162
+ return new modelUtils_1.ModelUtils(api, {
163
+ contentTest(content) {
164
+ return content.startsWith('// @dva-model');
165
+ },
166
+ astTest({ node, content }) {
167
+ if (isModelObject(node)) {
168
+ return true;
169
+ }
170
+ else if (content.includes('dva-model-extend') &&
171
+ t.isCallExpression(node) &&
172
+ node.arguments.length === 2 &&
173
+ isModelObject(node.arguments[1])) {
174
+ return true;
175
+ }
176
+ return false;
177
+ },
178
+ });
179
+ }
180
+ exports.getModelUtil = getModelUtil;
181
+ function getAllModels(api) {
182
+ return getModelUtil(api).getAllModels({
183
+ extraModels: [...(api.config.dva.extraModels || [])],
184
+ });
185
+ }
186
+ exports.getAllModels = getAllModels;
187
+ function isModelObject(node) {
188
+ return (t.isObjectExpression(node) &&
189
+ node.properties.some((property) => {
190
+ return [
191
+ 'state',
192
+ 'reducers',
193
+ 'subscriptions',
194
+ 'effects',
195
+ 'namespace',
196
+ ].includes(property.key.name);
197
+ }));
198
+ }
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
package/dist/icons.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = (api) => {
4
+ api;
5
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const withTmpPath_1 = require("./utils/withTmpPath");
4
+ exports.default = (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 />; }`}
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 dataflowProvider(container) {
111
+ return <Provider>{ container }</Provider>;
112
+ }
113
+ `,
114
+ });
115
+ });
116
+ };