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