@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/README.md CHANGED
@@ -1,3 +1,6 @@
1
1
  # @umijs/plugins
2
2
 
3
- See our website [umijs](https://umijs.org) for more information.
3
+ See our website [umijs](https://umijs.org) for more information.
4
+
5
+
6
+
package/dist/access.js CHANGED
@@ -1,5 +1,139 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
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");
3
9
  exports.default = (api) => {
4
- 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
+ ]);
5
139
  };
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
@@ -1,169 +1,137 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("@umijs/utils");
4
- const fs_1 = require("fs");
5
3
  const path_1 = require("path");
6
- const presets = {
7
- antd: {
8
- plugins: [
9
- 'isSameOrBefore',
10
- 'isSameOrAfter',
11
- 'advancedFormat',
12
- 'customParseFormat',
13
- 'weekday',
14
- 'weekYear',
15
- 'weekOfYear',
16
- 'isMoment',
17
- 'localeData',
18
- 'localizedFormat',
19
- ],
20
- replaceMoment: true,
21
- },
22
- antdv3: {
23
- plugins: [
24
- 'isSameOrBefore',
25
- 'isSameOrAfter',
26
- 'advancedFormat',
27
- 'customParseFormat',
28
- 'weekday',
29
- 'weekYear',
30
- 'weekOfYear',
31
- 'isMoment',
32
- 'localeData',
33
- 'localizedFormat',
34
- 'badMutable',
35
- ],
36
- replaceMoment: true,
37
- },
38
- };
39
- const getConfig = (api) => {
40
- let { preset = 'antd', plugins, replaceMoment, } = api.userConfig.antdDayjs || {};
41
- if (preset && presets[preset]) {
42
- plugins = presets[preset].plugins;
43
- replaceMoment = presets[preset].replaceMoment;
44
- }
45
- if (plugins)
46
- plugins = plugins;
47
- if (replaceMoment !== undefined)
48
- replaceMoment = replaceMoment;
49
- return {
50
- plugins,
51
- replaceMoment,
52
- };
53
- };
54
- const DIR_NAME = 'plugin-antd';
4
+ const plugin_utils_1 = require("umi/plugin-utils");
5
+ const resolveProjectDep_1 = require("./utils/resolveProjectDep");
6
+ const withTmpPath_1 = require("./utils/withTmpPath");
55
7
  exports.default = (api) => {
56
- const opts = api.userConfig.antd;
57
- // dayjs (by default)
58
- const { dayjs = true } = opts;
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) { }
59
18
  api.describe({
60
19
  config: {
61
20
  schema(Joi) {
62
21
  return Joi.object({
22
+ configProvider: Joi.object(),
23
+ // themes
63
24
  dark: Joi.boolean(),
64
25
  compact: Joi.boolean(),
65
- config: Joi.object(),
66
- dayjs: Joi.alternatives(Joi.boolean(), Joi.object({
67
- preset: Joi.string(),
68
- plugins: Joi.array(),
69
- replaceMoment: Joi.boolean(),
70
- })),
26
+ // babel-plugin-import
27
+ import: Joi.boolean(),
28
+ // less or css, default less
29
+ style: Joi.string().allow('less', 'css'),
71
30
  });
72
31
  },
73
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;
74
48
  });
75
- // antd import
76
- // TODO: use api.modifyConfig for support with vite
77
- api.chainWebpack((memo) => {
78
- function getUserLibDir({ library }) {
79
- if (
80
- // @ts-ignore
81
- (api.pkg.dependencies && api.pkg.dependencies[library]) ||
82
- // @ts-ignore
83
- (api.pkg.devDependencies && api.pkg.devDependencies[library]) ||
84
- // egg project using `clientDependencies` in ali tnpm
85
- // @ts-ignore
86
- (api.pkg.clientDependencies && api.pkg.clientDependencies[library])) {
87
- return (0, utils_1.winPath)((0, path_1.dirname)(
88
- // 通过 resolve 往上找,可支持 lerna 仓库
89
- // lerna 仓库如果用 yarn workspace 的依赖不一定在 node_modules,可能被提到根目录,并且没有 link
90
- utils_1.resolve.sync(`${library}/package.json`, {
91
- basedir: api.paths.cwd,
92
- })));
93
- }
94
- return null;
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'));
95
56
  }
96
- memo.resolve.alias.set('antd', getUserLibDir({ library: 'antd' }) ||
97
- (0, path_1.dirname)(require.resolve('antd/package.json')));
98
- if (dayjs !== false) {
99
- const { replaceMoment } = getConfig(api);
100
- if (replaceMoment) {
101
- memo.resolve.alias.set('moment', getUserLibDir({ library: 'dayjs' }) ||
102
- (0, path_1.dirname)(require.resolve('dayjs/package.json')));
103
- }
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
+ };
104
64
  }
105
65
  return memo;
106
66
  });
107
- // dark mode
108
- // compat mode
109
- if ((opts === null || opts === void 0 ? void 0 : opts.dark) || (opts === null || opts === void 0 ? void 0 : opts.compact)) {
110
- // support dark mode, user use antd 4 by default
111
- const { getThemeVariables } = require('antd/dist/theme');
112
- api.modifyDefaultConfig((config) => {
113
- config.theme = Object.assign(Object.assign({}, getThemeVariables(opts)), config.theme);
114
- return config;
115
- });
116
- }
117
- if (dayjs !== false) {
118
- api.onGenerateFiles({
119
- fn: () => {
120
- const { plugins } = getConfig(api);
121
- const runtimeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../templates/antd/dayjs.tpl'), 'utf-8');
122
- api.writeTmpFile({
123
- path: 'plugin-antd/dayjs.tsx',
124
- content: utils_1.Mustache.render(runtimeTpl, {
125
- plugins,
126
- dayjsPath: (0, path_1.dirname)(require.resolve('dayjs/package.json')),
127
- dayjsPluginPath: (0, path_1.dirname)(require.resolve('antd-dayjs-webpack-plugin/package.json')),
128
- }),
129
- });
130
- },
131
- });
132
- api.addEntryCodeAhead(() => {
133
- return [`import './${DIR_NAME}/dayjs.tsx'`];
134
- });
135
- }
67
+ api.modifyConfig((memo) => {
68
+ memo.theme = {
69
+ 'root-entry-name': 'default',
70
+ ...memo.theme,
71
+ };
72
+ return memo;
73
+ });
136
74
  // babel-plugin-import
137
75
  api.addExtraBabelPlugins(() => {
138
- return [
139
- [
140
- require.resolve('babel-plugin-import'),
141
- {
142
- libraryName: 'antd',
143
- libraryDirectory: 'es',
144
- style: true,
145
- },
146
- ],
147
- ];
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
+ : [];
148
90
  });
149
91
  // antd config provider
150
- // TODO: use umi provider
151
- if (opts === null || opts === void 0 ? void 0 : opts.config) {
152
- api.onGenerateFiles({
153
- fn() {
154
- // runtime.tsx
155
- const runtimeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../templates/antd/runtime.tpl'), 'utf-8');
156
- api.writeTmpFile({
157
- path: `${DIR_NAME}/runtime.tsx`,
158
- content: utils_1.Mustache.render(runtimeTpl, {
159
- config: JSON.stringify(opts === null || opts === void 0 ? void 0 : opts.config),
160
- }),
161
- });
162
- },
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
+ }),
163
119
  });
164
- //TODO:Runtime Plugin
165
- api.addRuntimePlugin(() => [
166
- (0, path_1.join)(api.paths.absTmpPath, DIR_NAME, 'runtime.tsx'),
167
- ]);
168
- }
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
+ });
169
137
  };
package/dist/dva.d.ts CHANGED
@@ -1,3 +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;
5
+ export declare function getModelUtil(api: IApi | null): ModelUtils;
6
+ export declare function getAllModels(api: IApi): Model[];