@umijs/plugins 4.0.0-beta.9 → 4.0.0-canary.202200505.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 (58) hide show
  1. package/README.md +4 -1
  2. package/dist/access.js +135 -1
  3. package/dist/{sass.d.ts → analytics.d.ts} +0 -0
  4. package/dist/analytics.js +67 -0
  5. package/dist/antd.js +113 -145
  6. package/dist/dva.d.ts +3 -0
  7. package/dist/dva.js +194 -4
  8. package/dist/initial-state.js +112 -1
  9. package/dist/layout.js +534 -1
  10. package/dist/locale.d.ts +1 -0
  11. package/dist/locale.js +195 -1
  12. package/dist/model.js +114 -1
  13. package/dist/moment2dayjs.d.ts +3 -0
  14. package/dist/moment2dayjs.js +96 -0
  15. package/dist/qiankun/constants.d.ts +5 -0
  16. package/dist/qiankun/constants.js +8 -0
  17. package/dist/qiankun/master.d.ts +6 -0
  18. package/dist/qiankun/master.js +134 -0
  19. package/dist/qiankun/slave.d.ts +3 -0
  20. package/dist/qiankun/slave.js +158 -0
  21. package/dist/qiankun.js +15 -1
  22. package/dist/request.js +307 -1
  23. package/dist/tailwindcss.d.ts +3 -0
  24. package/dist/tailwindcss.js +40 -0
  25. package/dist/unocss.d.ts +3 -0
  26. package/dist/unocss.js +39 -0
  27. package/dist/utils/astUtils.d.ts +3 -0
  28. package/dist/utils/astUtils.js +38 -0
  29. package/dist/utils/localeUtils.d.ts +33 -0
  30. package/dist/utils/localeUtils.js +126 -0
  31. package/dist/utils/modelUtils.d.ts +36 -0
  32. package/dist/utils/modelUtils.js +150 -0
  33. package/dist/utils/resolveProjectDep.d.ts +5 -0
  34. package/dist/utils/resolveProjectDep.js +15 -0
  35. package/dist/utils/withTmpPath.d.ts +6 -0
  36. package/dist/utils/withTmpPath.js +11 -0
  37. package/libs/dva.ts +10 -0
  38. package/libs/locale/SelectLang.tpl +478 -0
  39. package/libs/locale/locale.tpl +82 -0
  40. package/libs/locale/localeExports.tpl +271 -0
  41. package/libs/locale/runtime.tpl +33 -0
  42. package/libs/model.tsx +140 -0
  43. package/libs/qiankun/master/AntdErrorBoundary.tsx +34 -0
  44. package/libs/qiankun/master/AntdLoader.tsx +15 -0
  45. package/libs/qiankun/master/ErrorBoundary.tsx +7 -0
  46. package/libs/qiankun/master/MicroApp.tsx +269 -0
  47. package/libs/qiankun/master/MicroAppWithMemoHistory.tsx +43 -0
  48. package/libs/qiankun/master/common.ts +133 -0
  49. package/libs/qiankun/master/constants.ts +7 -0
  50. package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +36 -0
  51. package/libs/qiankun/master/masterRuntimePlugin.tsx +130 -0
  52. package/libs/qiankun/master/types.ts +44 -0
  53. package/libs/qiankun/slave/connectMaster.tsx +14 -0
  54. package/libs/qiankun/slave/lifecycles.ts +147 -0
  55. package/libs/qiankun/slave/qiankunModel.ts +18 -0
  56. package/libs/qiankun/slave/slaveRuntimePlugin.ts +15 -0
  57. package/package.json +25 -7
  58. package/dist/sass.js +0 -5
package/dist/locale.js CHANGED
@@ -1,5 +1,199 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.packageNormalize = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const plugin_utils_1 = require("umi/plugin-utils");
7
+ const localeUtils_1 = require("./utils/localeUtils");
8
+ const withTmpPath_1 = require("./utils/withTmpPath");
9
+ const packageNormalize = (packageName) => packageName.replace(/[@\/\-.]/g, '_');
10
+ exports.packageNormalize = packageNormalize;
11
+ // TODO: runtime plugin
3
12
  exports.default = (api) => {
4
- api;
13
+ // TODO: antd 的校验考虑 antd 插件
14
+ let hasAntd = false;
15
+ try {
16
+ hasAntd = !!require.resolve('antd');
17
+ }
18
+ catch (e) {
19
+ api.logger.warn('antd is not installed. <SelecLang /> unavailable');
20
+ }
21
+ const defaultConfig = {
22
+ baseNavigator: true,
23
+ useLocalStorage: true,
24
+ baseSeparator: '-',
25
+ antd: hasAntd,
26
+ };
27
+ api.describe({
28
+ key: 'locale',
29
+ config: {
30
+ schema(joi) {
31
+ return joi.object({
32
+ default: joi.string(),
33
+ useLocalStorage: joi.boolean(),
34
+ baseNavigator: joi.boolean(),
35
+ title: joi.boolean(),
36
+ antd: joi.boolean(),
37
+ baseSeparator: joi.string(),
38
+ });
39
+ },
40
+ },
41
+ enableBy: api.EnableBy.config,
42
+ });
43
+ const reactIntlPkgPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('react-intl/package')));
44
+ // polyfill
45
+ api.addEntryImportsAhead(() => (0, localeUtils_1.isNeedPolyfill)(api.config.targets || {})
46
+ ? [
47
+ {
48
+ source: require.resolve('intl'),
49
+ },
50
+ ]
51
+ : []);
52
+ const addAntdLocales = async (args) => {
53
+ var _a;
54
+ return await api.applyPlugins({
55
+ key: 'addAntdLocales',
56
+ type: api.ApplyPluginsType.add,
57
+ initialValue: [
58
+ `antd/${((_a = api.config) === null || _a === void 0 ? void 0 : _a.ssr) ? 'lib' : 'es'}/locale/${(0, localeUtils_1.getAntdLocale)(args.lang, args.country)}`,
59
+ ],
60
+ args,
61
+ });
62
+ };
63
+ const getList = async (resolveKey) => {
64
+ var _a;
65
+ const { paths } = api;
66
+ return (0, localeUtils_1.getLocaleList)({
67
+ localeFolder: 'locales',
68
+ separator: (_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.baseSeparator,
69
+ absSrcPath: paths.absSrcPath,
70
+ absPagesPath: paths.absPagesPath,
71
+ addAntdLocales,
72
+ resolveKey,
73
+ });
74
+ };
75
+ api.onGenerateFiles(async () => {
76
+ var _a, _b, _c, _d;
77
+ const localeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/locale.tpl'), 'utf-8');
78
+ // moment2dayjs
79
+ const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
80
+ const momentPkgPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve(`${resolveKey}/package.json`)));
81
+ const EventEmitterPkg = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('event-emitter/package')));
82
+ const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = {
83
+ ...defaultConfig,
84
+ ...api.config.locale,
85
+ };
86
+ const defaultLocale = ((_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.default) || `zh${baseSeparator}CN`;
87
+ const localeList = await getList(resolveKey);
88
+ const momentLocales = localeList
89
+ .map(({ momentLocale }) => momentLocale)
90
+ .filter((locale) => locale);
91
+ const antdLocales = localeList
92
+ .map(({ antdLocale }) => antdLocale)
93
+ .filter((locale) => locale);
94
+ let MomentLocales = momentLocales;
95
+ let DefaultMomentLocale = '';
96
+ // set moment default accounding to locale.default
97
+ if (!MomentLocales.length && ((_b = api.config.locale) === null || _b === void 0 ? void 0 : _b.default)) {
98
+ const [lang, country = ''] = defaultLocale.split(baseSeparator);
99
+ const { momentLocale } = (0, localeUtils_1.getMomentLocale)(lang, country, resolveKey);
100
+ if (momentLocale) {
101
+ MomentLocales = [momentLocale];
102
+ DefaultMomentLocale = momentLocale;
103
+ }
104
+ }
105
+ let DefaultAntdLocales = [];
106
+ // set antd default locale
107
+ if (!antdLocales.length && ((_c = api.config.locale) === null || _c === void 0 ? void 0 : _c.antd)) {
108
+ const [lang, country = ''] = defaultLocale.split(baseSeparator);
109
+ DefaultAntdLocales = plugin_utils_1.lodash.uniq(await addAntdLocales({
110
+ lang,
111
+ country,
112
+ }));
113
+ }
114
+ const NormalizeAntdLocalesName = function () {
115
+ // @ts-ignore
116
+ return (0, exports.packageNormalize)(this);
117
+ };
118
+ api.writeTmpFile({
119
+ content: plugin_utils_1.Mustache.render(localeTpl, {
120
+ MomentLocales,
121
+ DefaultMomentLocale,
122
+ NormalizeAntdLocalesName,
123
+ DefaultAntdLocales,
124
+ Antd: !!antd,
125
+ Title: title && api.config.title,
126
+ BaseSeparator: baseSeparator,
127
+ DefaultLocale: defaultLocale,
128
+ DefaultLang: defaultLocale,
129
+ momentPkgPath,
130
+ }),
131
+ path: 'locale.tsx',
132
+ });
133
+ const localeExportsTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/localeExports.tpl'), 'utf-8');
134
+ const localeDirName = 'locales';
135
+ const localeDirPath = (0, path_1.join)(api.paths.absSrcPath, localeDirName);
136
+ api.writeTmpFile({
137
+ path: 'localeExports.ts',
138
+ content: plugin_utils_1.Mustache.render(localeExportsTpl, {
139
+ EventEmitterPkg,
140
+ BaseSeparator: baseSeparator,
141
+ BaseNavigator: baseNavigator,
142
+ UseLocalStorage: !!useLocalStorage,
143
+ LocaleDir: localeDirName,
144
+ ExistLocaleDir: (0, fs_1.existsSync)(localeDirPath),
145
+ LocaleList: localeList.map((locale) => ({
146
+ ...locale,
147
+ antdLocale: locale.antdLocale.map((antdLocale, index) => ({
148
+ locale: antdLocale,
149
+ index: index,
150
+ })),
151
+ paths: locale.paths.map((path, index) => ({
152
+ path,
153
+ index,
154
+ })),
155
+ })),
156
+ Antd: !!antd,
157
+ DefaultLocale: JSON.stringify(defaultLocale),
158
+ warningPkgPath: (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('warning/package'))),
159
+ reactIntlPkgPath,
160
+ }),
161
+ });
162
+ // runtime.tsx
163
+ const runtimeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/runtime.tpl'), 'utf-8');
164
+ api.writeTmpFile({
165
+ path: 'runtime.tsx',
166
+ content: plugin_utils_1.Mustache.render(runtimeTpl, {
167
+ Title: !!title,
168
+ }),
169
+ });
170
+ // SelectLang.tsx
171
+ const selectLang = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/SelectLang.tpl'), 'utf-8');
172
+ api.writeTmpFile({
173
+ path: 'SelectLang.tsx',
174
+ content: plugin_utils_1.Mustache.render(selectLang, {
175
+ Antd: !!antd,
176
+ LocaleList: localeList,
177
+ ShowSelectLang: localeList.length > 1 && !!antd,
178
+ antdFiles: ((_d = api.config) === null || _d === void 0 ? void 0 : _d.ssr) ? 'lib' : 'es',
179
+ }),
180
+ });
181
+ // index.ts
182
+ api.writeTmpFile({
183
+ path: 'index.ts',
184
+ content: `
185
+ export { setLocale, getLocale, useIntl, injectIntl, formatMessage, FormattedMessage } from './localeExports.ts';
186
+ export { SelectLang } from './SelectLang.tsx';
187
+ `,
188
+ });
189
+ });
190
+ // Runtime Plugin
191
+ api.addRuntimePlugin(() => [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })]);
192
+ api.addRuntimePluginKey(() => ['locale']);
193
+ // watch locale files
194
+ api.addTmpGenerateWatcherPaths(async () => {
195
+ const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
196
+ const localeList = await getList(resolveKey);
197
+ return (0, localeUtils_1.exactLocalePaths)(localeList);
198
+ });
5
199
  };
package/dist/model.js CHANGED
@@ -1,5 +1,118 @@
1
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
+ };
2
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
27
+ const fs_1 = require("fs");
28
+ const path_1 = require("path");
29
+ const plugin_utils_1 = require("umi/plugin-utils");
30
+ const modelUtils_1 = require("./utils/modelUtils");
31
+ const withTmpPath_1 = require("./utils/withTmpPath");
3
32
  exports.default = (api) => {
4
- api;
33
+ api.describe({
34
+ config: {
35
+ schema(Joi) {
36
+ return Joi.object({
37
+ extraModels: Joi.array().items(Joi.string()),
38
+ });
39
+ },
40
+ },
41
+ enableBy: api.EnableBy.config,
42
+ });
43
+ api.modifyAppData(async (memo) => {
44
+ const models = await getAllModels(api);
45
+ memo.pluginModel = {
46
+ models,
47
+ };
48
+ return memo;
49
+ });
50
+ api.onGenerateFiles(async (args) => {
51
+ const models = args.isFirstTime
52
+ ? api.appData.pluginModel.models
53
+ : await getAllModels(api);
54
+ // model.ts
55
+ api.writeTmpFile({
56
+ path: 'model.ts',
57
+ content: modelUtils_1.ModelUtils.getModelsContent(models),
58
+ });
59
+ // index.tsx
60
+ const indexContent = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/model.tsx'), 'utf-8').replace('fast-deep-equal', (0, plugin_utils_1.winPath)(require.resolve('fast-deep-equal')));
61
+ api.writeTmpFile({
62
+ path: 'index.tsx',
63
+ content: indexContent,
64
+ });
65
+ // runtime.tsx
66
+ api.writeTmpFile({
67
+ path: 'runtime.tsx',
68
+ content: `
69
+ import React from 'react';
70
+ import { Provider } from './';
71
+ import { models as rawModels } from './model';
72
+
73
+ function ProviderWrapper(props: any) {
74
+ const models = React.useMemo(() => {
75
+ return Object.keys(rawModels).reduce((memo, key) => {
76
+ memo[rawModels[key].namespace] = rawModels[key].model;
77
+ return memo;
78
+ }, {});
79
+ }, []);
80
+ return <Provider models={models} {...props}>{ props.children }</Provider>
81
+ }
82
+
83
+ export function dataflowProvider(container, opts) {
84
+ return <ProviderWrapper {...opts}>{ container }</ProviderWrapper>;
85
+ }
86
+ `,
87
+ });
88
+ });
89
+ api.addTmpGenerateWatcherPaths(() => {
90
+ return [(0, path_1.join)(api.paths.absSrcPath, 'models')];
91
+ });
92
+ api.addRuntimePlugin(() => {
93
+ return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
94
+ });
5
95
  };
96
+ async function getAllModels(api) {
97
+ const extraModelsPre = await api.applyPlugins({
98
+ key: 'addExtraModelsPre',
99
+ type: api.ApplyPluginsType.add,
100
+ initialValue: [],
101
+ });
102
+ const extraModels = await api.applyPlugins({
103
+ key: 'addExtraModels',
104
+ type: api.ApplyPluginsType.add,
105
+ initialValue: [],
106
+ });
107
+ return new modelUtils_1.ModelUtils(api, {
108
+ astTest({ node }) {
109
+ return t.isArrowFunctionExpression(node) || t.isFunctionDeclaration(node);
110
+ },
111
+ }).getAllModels({
112
+ extraModelsPre: [
113
+ ...extraModelsPre,
114
+ ...(api.config.model.extraModelsPre || []),
115
+ ],
116
+ extraModels: [...extraModels, ...(api.config.model.extraModels || [])],
117
+ });
118
+ }
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;
@@ -0,0 +1,96 @@
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
+ /*
6
+ As long as moment2dayjs is registered, moment will be replaced by dayjs.
7
+ The presets that can adapt to antd is registered by default.
8
+ When the user configures preset and plugins at the same time, we will merge them.
9
+ */
10
+ exports.default = (api) => {
11
+ api.describe({
12
+ key: 'moment2dayjs',
13
+ config: {
14
+ schema(joi) {
15
+ return joi.object({
16
+ preset: joi.string(),
17
+ plugins: joi.array(),
18
+ });
19
+ },
20
+ },
21
+ enableBy: api.EnableBy.config,
22
+ });
23
+ const presets = {
24
+ antd: [
25
+ 'isSameOrBefore',
26
+ 'isSameOrAfter',
27
+ 'advancedFormat',
28
+ 'customParseFormat',
29
+ 'weekday',
30
+ 'weekYear',
31
+ 'weekOfYear',
32
+ 'isMoment',
33
+ 'localeData',
34
+ 'localizedFormat',
35
+ ],
36
+ antdv3: [
37
+ 'isSameOrBefore',
38
+ 'isSameOrAfter',
39
+ 'advancedFormat',
40
+ 'customParseFormat',
41
+ 'weekday',
42
+ 'weekYear',
43
+ 'weekOfYear',
44
+ 'isMoment',
45
+ 'localeData',
46
+ 'localizedFormat',
47
+ 'badMutable',
48
+ ],
49
+ };
50
+ const getDayjsPlugins = (api) => {
51
+ let { preset = 'antd', plugins = [] } = api.config.moment2dayjs || {};
52
+ switch (preset) {
53
+ case 'antd':
54
+ return Array.from(new Set(presets['antd'].concat(plugins)));
55
+ case 'antdv3':
56
+ return Array.from(new Set(presets['antdv3'].concat(plugins)));
57
+ case 'none':
58
+ return [].concat(plugins);
59
+ default:
60
+ return [];
61
+ }
62
+ };
63
+ // replace moment
64
+ api.modifyConfig((memo) => {
65
+ memo.alias.moment = (0, path_1.dirname)(require.resolve('dayjs/package.json'));
66
+ return memo;
67
+ });
68
+ api.onGenerateFiles(() => {
69
+ const plugins = getDayjsPlugins(api);
70
+ const runtimeTpl = `
71
+ import dayjs from '{{{dayjsPath}}}';
72
+ import antdPlugin from '{{{dayjsAntdPluginPath}}}';
73
+
74
+ {{#plugins}}
75
+ import {{.}} from '{{{dayjsPath}}}/plugin/{{.}}';
76
+ {{/plugins}}
77
+
78
+ {{#plugins}}
79
+ dayjs.extend({{.}});
80
+ {{/plugins}}
81
+
82
+ dayjs.extend(antdPlugin);
83
+ `;
84
+ const dayjsAntdPluginPath = (0, plugin_utils_1.winPath)(require.resolve('antd-dayjs-webpack-plugin/src/antd-plugin'));
85
+ const dayjsPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('dayjs/package.json')));
86
+ api.writeTmpFile({
87
+ path: 'runtime.tsx',
88
+ content: plugin_utils_1.Mustache.render(runtimeTpl, {
89
+ plugins,
90
+ dayjsPath,
91
+ dayjsAntdPluginPath,
92
+ }),
93
+ });
94
+ });
95
+ api.addEntryCodeAhead(() => [`import './plugin-moment2dayjs/runtime.tsx'`]);
96
+ };
@@ -0,0 +1,5 @@
1
+ export declare const defaultMasterRootId = "root-master";
2
+ export declare const defaultHistoryType = "browser";
3
+ export declare const qiankunStateForSlaveModelNamespace = "@@qiankunStateForSlave";
4
+ export declare const qiankunStateFromMasterModelNamespace = "@@qiankunStateFromMaster";
5
+ export declare const MODEL_EXPORT_NAME = "useQiankunStateForSlave";
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MODEL_EXPORT_NAME = exports.qiankunStateFromMasterModelNamespace = exports.qiankunStateForSlaveModelNamespace = exports.defaultHistoryType = exports.defaultMasterRootId = void 0;
4
+ exports.defaultMasterRootId = 'root-master';
5
+ exports.defaultHistoryType = 'browser';
6
+ exports.qiankunStateForSlaveModelNamespace = '@@qiankunStateForSlave';
7
+ exports.qiankunStateFromMasterModelNamespace = '@@qiankunStateFromMaster';
8
+ exports.MODEL_EXPORT_NAME = 'useQiankunStateForSlave';
@@ -0,0 +1,6 @@
1
+ import { IApi } from 'umi';
2
+ export declare function isMasterEnable(opts: {
3
+ userConfig: any;
4
+ }): boolean;
5
+ declare const _default: (api: IApi) => void;
6
+ export default _default;
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMasterEnable = void 0;
4
+ const fs_1 = require("fs");
5
+ const path_1 = require("path");
6
+ const plugin_utils_1 = require("umi/plugin-utils");
7
+ const withTmpPath_1 = require("../utils/withTmpPath");
8
+ const constants_1 = require("./constants");
9
+ function isMasterEnable(opts) {
10
+ var _a;
11
+ const masterCfg = (_a = opts.userConfig.qiankun) === null || _a === void 0 ? void 0 : _a.master;
12
+ if (masterCfg) {
13
+ return masterCfg.enable !== false;
14
+ }
15
+ return !!process.env.INITIAL_QIANKUN_MASTER_OPTIONS;
16
+ }
17
+ exports.isMasterEnable = isMasterEnable;
18
+ exports.default = (api) => {
19
+ api.describe({
20
+ key: 'qiankun-master',
21
+ enableBy: isMasterEnable,
22
+ });
23
+ api.addRuntimePlugin(() => {
24
+ return [(0, withTmpPath_1.withTmpPath)({ api, path: 'masterRuntimePlugin.tsx' })];
25
+ });
26
+ api.modifyDefaultConfig((config) => ({
27
+ ...config,
28
+ mountElementId: constants_1.defaultMasterRootId,
29
+ qiankun: {
30
+ ...config.qiankun,
31
+ master: {
32
+ ...JSON.parse(process.env.INITIAL_QIANKUN_MASTER_OPTIONS || '{}'),
33
+ ...(config.qiankun || {}).master,
34
+ },
35
+ },
36
+ }));
37
+ // TODO: modify routes
38
+ api.modifyRoutes((memo) => {
39
+ Object.keys(memo).forEach((id) => {
40
+ const route = memo[id];
41
+ if (route.microApp) {
42
+ const appName = route.microApp;
43
+ // TODO: config base
44
+ const base = '/';
45
+ // TODO: config masterHistoryType
46
+ const masterHistoryType = 'browser';
47
+ const routeProps = route.microAppProps || {};
48
+ const normalizedRouteProps = JSON.stringify(routeProps).replace(/"/g, "'");
49
+ route.file = `(async () => {
50
+ const { getMicroAppRouteComponent } = await import('@@/plugin-qiankun-master/getMicroAppRouteComponent');
51
+ return getMicroAppRouteComponent({ appName: '${appName}', base: '${base}', masterHistoryType: '${masterHistoryType}', routeProps: ${normalizedRouteProps} })
52
+ })()`;
53
+ }
54
+ });
55
+ return memo;
56
+ });
57
+ // state model for slave app
58
+ api.addRuntimePluginKey(() => [constants_1.MODEL_EXPORT_NAME]);
59
+ api.register({
60
+ key: 'addExtraModels',
61
+ fn() {
62
+ const { path, exports } = api.appData.appJS || {};
63
+ return path && exports.includes(constants_1.MODEL_EXPORT_NAME)
64
+ ? [
65
+ `${path}#{"namespace":"${constants_1.qiankunStateForSlaveModelNamespace}","exportName":"${constants_1.MODEL_EXPORT_NAME}"}`,
66
+ ]
67
+ : [];
68
+ },
69
+ });
70
+ function getFileContent(file) {
71
+ return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../libs/qiankun/master', file), 'utf-8');
72
+ }
73
+ api.onGenerateFiles(() => {
74
+ var _a;
75
+ api.writeTmpFile({
76
+ path: 'masterOptions.ts',
77
+ content: `
78
+ let options = ${JSON.stringify({
79
+ masterHistoryType: ((_a = api.config.history) === null || _a === void 0 ? void 0 : _a.type) || constants_1.defaultHistoryType,
80
+ base: api.config.base || '/',
81
+ ...api.config.qiankun.master,
82
+ })};
83
+ export const getMasterOptions = () => options;
84
+ export const setMasterOptions = (newOpts) => options = ({ ...options, ...newOpts });
85
+ `,
86
+ });
87
+ api.writeTmpFile({
88
+ path: 'MicroAppLoader.tsx',
89
+ // 开启了 antd 插件的时候,使用 antd 的 loader 组件,否则提示用户必须设置一个自定义的 loader 组件
90
+ content: api.isPluginEnable('antd')
91
+ ? getFileContent('AntdLoader.tsx')
92
+ : `export default function Loader() { console.warn(\`[plugins/qiankun]: Seems like you'r not using @umijs/plugin-antd, you need to provide a custom loader or set autoSetLoading false to shut down this warning!\`); return null; }`,
93
+ });
94
+ [
95
+ 'common.ts',
96
+ 'constants.ts',
97
+ 'types.ts',
98
+ 'masterRuntimePlugin.tsx',
99
+ 'getMicroAppRouteComponent.tsx.tpl',
100
+ 'ErrorBoundary.tsx',
101
+ 'MicroApp.tsx',
102
+ ].forEach((file) => {
103
+ if (file.endsWith('.tpl')) {
104
+ api.writeTmpFile({
105
+ path: file.replace(/\.tpl$/, ''),
106
+ tpl: getFileContent(file),
107
+ context: {
108
+ dynamicRoot: false,
109
+ hasModelPlugin: api.isPluginEnable('model'),
110
+ // dynamicRoot:
111
+ // api.config.exportStatic && api.config.exportStatic.dynamicRoot,
112
+ },
113
+ });
114
+ }
115
+ else {
116
+ api.writeTmpFile({
117
+ path: file.replace(/\.tpl$/, ''),
118
+ content: getFileContent(file)
119
+ .replace('__USE_MODEL__', api.isPluginEnable('model')
120
+ ? `import { useModel } from '@@/plugin-model'`
121
+ : `const useModel = null;`)
122
+ .replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
123
+ .replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
124
+ });
125
+ }
126
+ });
127
+ api.writeTmpFile({
128
+ path: 'index.ts',
129
+ content: `
130
+ export { MicroApp } from './MicroApp';
131
+ `,
132
+ });
133
+ });
134
+ };
@@ -0,0 +1,3 @@
1
+ import { IApi } from 'umi';
2
+ declare const _default: (api: IApi) => void;
3
+ export default _default;