@umijs/core 3.5.17 → 4.0.0-beta.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 (84) hide show
  1. package/README.md +2 -0
  2. package/dist/config/config.d.ts +63 -0
  3. package/dist/config/config.js +178 -0
  4. package/dist/config/utils.d.ts +8 -0
  5. package/dist/config/utils.js +15 -0
  6. package/dist/constants.d.ts +9 -0
  7. package/dist/constants.js +12 -0
  8. package/dist/index.d.ts +5 -0
  9. package/dist/index.js +19 -0
  10. package/dist/route/defineRoutes.d.ts +1 -0
  11. package/dist/route/defineRoutes.js +28 -0
  12. package/dist/route/route.d.ts +2 -0
  13. package/dist/route/route.js +18 -0
  14. package/dist/route/routeUtils.d.ts +7 -0
  15. package/dist/route/routeUtils.js +21 -0
  16. package/dist/route/routesConfig.d.ts +0 -0
  17. package/dist/route/routesConfig.js +1 -0
  18. package/dist/route/routesConvention.d.ts +3 -0
  19. package/dist/route/routesConvention.js +73 -0
  20. package/dist/route/utils.d.ts +7 -0
  21. package/dist/route/utils.js +26 -0
  22. package/dist/service/command.d.ts +27 -0
  23. package/dist/service/command.js +14 -0
  24. package/dist/service/env.d.ts +4 -0
  25. package/dist/service/env.js +21 -0
  26. package/dist/service/hook.d.ts +16 -0
  27. package/dist/service/hook.js +18 -0
  28. package/dist/service/path.d.ts +13 -0
  29. package/dist/service/path.js +31 -0
  30. package/dist/service/plugin.d.ts +53 -0
  31. package/dist/service/plugin.js +137 -0
  32. package/dist/service/pluginAPI.d.ts +35 -0
  33. package/dist/service/pluginAPI.js +120 -0
  34. package/dist/service/service.d.ts +91 -0
  35. package/dist/service/service.js +361 -0
  36. package/dist/service/servicePlugin.d.ts +3 -0
  37. package/dist/service/servicePlugin.js +14 -0
  38. package/dist/service/utils.d.ts +2 -0
  39. package/dist/service/utils.js +12 -0
  40. package/dist/types.d.ts +108 -0
  41. package/dist/types.js +42 -0
  42. package/package.json +35 -15
  43. package/lib/Config/Config.d.ts +0 -39
  44. package/lib/Config/Config.js +0 -348
  45. package/lib/Config/types.d.ts +0 -21
  46. package/lib/Config/utils/configUtils.d.ts +0 -9
  47. package/lib/Config/utils/configUtils.js +0 -54
  48. package/lib/Config/utils/isEqual.d.ts +0 -1
  49. package/lib/Config/utils/isEqual.js +0 -45
  50. package/lib/Config/utils/mergeDefault.d.ts +0 -6
  51. package/lib/Config/utils/mergeDefault.js +0 -41
  52. package/lib/Html/Html.d.ts +0 -16
  53. package/lib/Html/Html.js +0 -292
  54. package/lib/Html/document.ejs +0 -12
  55. package/lib/Html/types.d.ts +0 -55
  56. package/lib/Logger/Common.d.ts +0 -13
  57. package/lib/Logger/Common.js +0 -53
  58. package/lib/Logger/Logger.d.ts +0 -17
  59. package/lib/Logger/Logger.js +0 -87
  60. package/lib/Route/Route.d.ts +0 -32
  61. package/lib/Route/Route.js +0 -228
  62. package/lib/Route/getConventionalRoutes.d.ts +0 -10
  63. package/lib/Route/getConventionalRoutes.js +0 -259
  64. package/lib/Route/routesToJSON.d.ts +0 -9
  65. package/lib/Route/routesToJSON.js +0 -140
  66. package/lib/Route/types.d.ts +0 -11
  67. package/lib/Service/PluginAPI.d.ts +0 -38
  68. package/lib/Service/PluginAPI.js +0 -193
  69. package/lib/Service/Service.d.ts +0 -96
  70. package/lib/Service/Service.js +0 -694
  71. package/lib/Service/enums.d.ts +0 -29
  72. package/lib/Service/enums.js +0 -67
  73. package/lib/Service/getPaths.d.ts +0 -6
  74. package/lib/Service/getPaths.js +0 -79
  75. package/lib/Service/types.d.ts +0 -52
  76. package/lib/Service/types.js +0 -17
  77. package/lib/Service/utils/isPromise.d.ts +0 -1
  78. package/lib/Service/utils/isPromise.js +0 -22
  79. package/lib/Service/utils/loadDotEnv.d.ts +0 -5
  80. package/lib/Service/utils/loadDotEnv.js +0 -54
  81. package/lib/Service/utils/pluginUtils.d.ts +0 -42
  82. package/lib/Service/utils/pluginUtils.js +0 -201
  83. package/lib/index.d.ts +0 -14
  84. package/lib/index.js +0 -81
@@ -1,228 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- function _react() {
9
- const data = _interopRequireDefault(require("react"));
10
-
11
- _react = function _react() {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _utils() {
19
- const data = require("@umijs/utils");
20
-
21
- _utils = function _utils() {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _assert() {
29
- const data = _interopRequireDefault(require("assert"));
30
-
31
- _assert = function _assert() {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
37
-
38
- function _path() {
39
- const data = _interopRequireWildcard(require("path"));
40
-
41
- _path = function _path() {
42
- return data;
43
- };
44
-
45
- return data;
46
- }
47
-
48
- var _getConventionalRoutes = _interopRequireDefault(require("./getConventionalRoutes"));
49
-
50
- var _routesToJSON = _interopRequireDefault(require("./routesToJSON"));
51
-
52
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
53
-
54
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
55
-
56
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
57
-
58
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
59
-
60
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
61
-
62
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
63
-
64
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
65
-
66
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
67
-
68
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
69
-
70
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
71
-
72
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
73
-
74
- class Route {
75
- constructor(opts) {
76
- this.opts = void 0;
77
- this.opts = opts || {};
78
- }
79
-
80
- getRoutes(opts) {
81
- var _this = this;
82
-
83
- return _asyncToGenerator(function* () {
84
- const config = opts.config,
85
- root = opts.root,
86
- componentPrefix = opts.componentPrefix; // 避免修改配置里的 routes,导致重复 patch
87
-
88
- let routes = _utils().lodash.cloneDeep(config.routes);
89
-
90
- let isConventional = false;
91
-
92
- if (!routes) {
93
- (0, _assert().default)(root, `opts.root must be supplied for conventional routes.`);
94
- routes = _this.getConventionRoutes({
95
- root: root,
96
- config,
97
- componentPrefix
98
- });
99
- isConventional = true;
100
- }
101
-
102
- yield _this.patchRoutes(routes, _objectSpread(_objectSpread({}, opts), {}, {
103
- isConventional
104
- }));
105
- return routes;
106
- })();
107
- } // TODO:
108
- // 1. 移动 /404 到最后,并处理 component 和 redirect
109
-
110
-
111
- patchRoutes(routes, opts) {
112
- var _this2 = this;
113
-
114
- return _asyncToGenerator(function* () {
115
- if (_this2.opts.onPatchRoutesBefore) {
116
- yield _this2.opts.onPatchRoutesBefore({
117
- routes,
118
- parentRoute: opts.parentRoute
119
- });
120
- }
121
-
122
- var _iterator = _createForOfIteratorHelper(routes),
123
- _step;
124
-
125
- try {
126
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
127
- const route = _step.value;
128
- yield _this2.patchRoute(route, opts);
129
- }
130
- } catch (err) {
131
- _iterator.e(err);
132
- } finally {
133
- _iterator.f();
134
- }
135
-
136
- if (_this2.opts.onPatchRoutes) {
137
- yield _this2.opts.onPatchRoutes({
138
- routes,
139
- parentRoute: opts.parentRoute
140
- });
141
- }
142
- })();
143
- }
144
-
145
- patchRoute(route, opts) {
146
- var _this3 = this;
147
-
148
- return _asyncToGenerator(function* () {
149
- if (_this3.opts.onPatchRouteBefore) {
150
- yield _this3.opts.onPatchRouteBefore({
151
- route,
152
- parentRoute: opts.parentRoute
153
- });
154
- } // route.path 的修改需要在子路由 patch 之前做
155
-
156
-
157
- if (route.path && route.path.charAt(0) !== '/' && !/^https?:\/\//.test(route.path)) {
158
- var _opts$parentRoute;
159
-
160
- route.path = (0, _utils().winPath)((0, _path().join)(((_opts$parentRoute = opts.parentRoute) === null || _opts$parentRoute === void 0 ? void 0 : _opts$parentRoute.path) || '/', route.path));
161
- }
162
-
163
- if (route.redirect && route.redirect.charAt(0) !== '/') {
164
- var _opts$parentRoute2;
165
-
166
- route.redirect = (0, _utils().winPath)((0, _path().join)(((_opts$parentRoute2 = opts.parentRoute) === null || _opts$parentRoute2 === void 0 ? void 0 : _opts$parentRoute2.path) || '/', route.redirect));
167
- }
168
-
169
- if (route.routes) {
170
- yield _this3.patchRoutes(route.routes, _objectSpread(_objectSpread({}, opts), {}, {
171
- parentRoute: route
172
- }));
173
- } else {
174
- if (!('exact' in route)) {
175
- // exact by default
176
- route.exact = true;
177
- }
178
- } // resolve component path
179
-
180
-
181
- if (route.component && !opts.isConventional && typeof route.component === 'string' && !route.component.startsWith('@/') && !_path().default.isAbsolute(route.component)) {
182
- route.component = (0, _utils().winPath)((0, _path().join)(opts.root, route.component));
183
- } // resolve wrappers path
184
-
185
-
186
- if (route.wrappers) {
187
- route.wrappers = route.wrappers.map(wrapper => {
188
- if (wrapper.startsWith('@/') || _path().default.isAbsolute(wrapper)) {
189
- return wrapper;
190
- } else {
191
- return (0, _utils().winPath)((0, _path().join)(opts.root, wrapper));
192
- }
193
- });
194
- }
195
-
196
- if (_this3.opts.onPatchRoute) {
197
- yield _this3.opts.onPatchRoute({
198
- route,
199
- parentRoute: opts.parentRoute
200
- });
201
- }
202
- })();
203
- }
204
-
205
- getConventionRoutes(opts) {
206
- return (0, _getConventionalRoutes.default)(opts);
207
- }
208
-
209
- getJSON(opts) {
210
- return (0, _routesToJSON.default)(opts);
211
- }
212
-
213
- getPaths({
214
- routes
215
- }) {
216
- return _utils().lodash.uniq(routes.reduce((memo, route) => {
217
- if (route.path) memo.push(route.path);
218
- if (route.routes) memo = memo.concat(this.getPaths({
219
- routes: route.routes
220
- }));
221
- return memo;
222
- }, []));
223
- }
224
-
225
- }
226
-
227
- var _default = Route;
228
- exports.default = _default;
@@ -1,10 +0,0 @@
1
- import { IConfig } from '..';
2
- import { IRoute } from './types';
3
- interface IOpts {
4
- root: string;
5
- relDir?: string;
6
- componentPrefix?: string;
7
- config: IConfig;
8
- }
9
- export default function getRoutes(opts: IOpts): IRoute[];
10
- export {};
@@ -1,259 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = getRoutes;
7
-
8
- function _react() {
9
- const data = _interopRequireDefault(require("react"));
10
-
11
- _react = function _react() {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _ast() {
19
- const data = require("@umijs/ast");
20
-
21
- _ast = function _ast() {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _utils() {
29
- const data = require("@umijs/utils");
30
-
31
- _utils = function _utils() {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
37
-
38
- function _assert() {
39
- const data = _interopRequireDefault(require("assert"));
40
-
41
- _assert = function _assert() {
42
- return data;
43
- };
44
-
45
- return data;
46
- }
47
-
48
- function _fs() {
49
- const data = require("fs");
50
-
51
- _fs = function _fs() {
52
- return data;
53
- };
54
-
55
- return data;
56
- }
57
-
58
- function _path() {
59
- const data = require("path");
60
-
61
- _path = function _path() {
62
- return data;
63
- };
64
-
65
- return data;
66
- }
67
-
68
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
69
-
70
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
71
-
72
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
73
-
74
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
75
-
76
- // 考虑多种情况:
77
- // 可能是目录,没有后缀,比如 [post]/add.tsx
78
- // 可能是文件,有后缀,比如 [id].tsx
79
- // [id$] 是可选动态路由
80
- const RE_DYNAMIC_ROUTE = /^\[(.+?)\]/;
81
-
82
- function getFiles(root) {
83
- if (!(0, _fs().existsSync)(root)) return [];
84
- return (0, _fs().readdirSync)(root).filter(file => {
85
- const absFile = (0, _path().join)(root, file);
86
- const fileStat = (0, _fs().statSync)(absFile);
87
- const isDirectory = fileStat.isDirectory();
88
- const isFile = fileStat.isFile();
89
-
90
- if (isDirectory && ['components', 'component', 'utils', 'util'].includes(file)) {
91
- return false;
92
- }
93
-
94
- if (file.charAt(0) === '.') return false;
95
- if (file.charAt(0) === '_') return false; // exclude test file
96
-
97
- if (/\.(test|spec|e2e)\.(j|t)sx?$/.test(file)) return false; // d.ts
98
-
99
- if (/\.d\.ts$/.test(file)) return false;
100
-
101
- if (isFile) {
102
- if (!/\.(j|t)sx?$/.test(file)) return false;
103
- const content = (0, _fs().readFileSync)(absFile, 'utf-8');
104
-
105
- try {
106
- if (!(0, _ast().isReactComponent)(content)) return false;
107
- } catch (e) {
108
- throw new Error(`Parse conventional route component ${absFile} failed, ${e.message}`);
109
- }
110
- }
111
-
112
- return true;
113
- });
114
- }
115
-
116
- function fileToRouteReducer(opts, memo, file) {
117
- const root = opts.root,
118
- _opts$relDir = opts.relDir,
119
- relDir = _opts$relDir === void 0 ? '' : _opts$relDir;
120
- const absFile = (0, _path().join)(root, relDir, file);
121
- const stats = (0, _fs().statSync)(absFile);
122
-
123
- const __isDynamic = RE_DYNAMIC_ROUTE.test(file);
124
-
125
- if (stats.isDirectory()) {
126
- const relFile = (0, _path().join)(relDir, file);
127
- const layoutFile = (0, _utils().getFile)({
128
- base: (0, _path().join)(root, relFile),
129
- fileNameWithoutExt: '_layout',
130
- type: 'javascript'
131
- });
132
-
133
- const route = _objectSpread({
134
- path: normalizePath(relFile, opts),
135
- routes: getRoutes(_objectSpread(_objectSpread({}, opts), {}, {
136
- relDir: (0, _path().join)(relFile)
137
- })),
138
- __isDynamic
139
- }, layoutFile ? {
140
- component: layoutFile.path
141
- } : {
142
- exact: true,
143
- __toMerge: true
144
- });
145
-
146
- memo.push(normalizeRoute(route, opts));
147
- } else {
148
- const bName = (0, _path().basename)(file, (0, _path().extname)(file));
149
- memo.push(normalizeRoute({
150
- path: normalizePath((0, _path().join)(relDir, bName), opts),
151
- exact: true,
152
- component: absFile,
153
- __isDynamic
154
- }, opts));
155
- }
156
-
157
- return memo;
158
- }
159
-
160
- function normalizeRoute(route, opts) {
161
- let props = undefined;
162
-
163
- if (route.component) {
164
- try {
165
- props = (0, _ast().getExportProps)((0, _fs().readFileSync)(route.component, 'utf-8'));
166
- } catch (e) {
167
- throw new Error(`Parse conventional route component ${route.component} failed, ${e.message}`);
168
- }
169
-
170
- route.component = (0, _utils().winPath)((0, _path().relative)((0, _path().join)(opts.root, '..'), route.component));
171
- route.component = `${opts.componentPrefix || '@/'}${route.component}`;
172
- }
173
-
174
- return _objectSpread(_objectSpread({}, route), typeof props === 'object' ? props : {});
175
- }
176
-
177
- function normalizePath(path, opts) {
178
- path = (0, _utils().winPath)(path).split('/').map(p => {
179
- // dynamic route
180
- p = p.replace(RE_DYNAMIC_ROUTE, ':$1'); // :post$ => :post?
181
-
182
- if (p.endsWith('$')) {
183
- p = p.slice(0, -1) + '?';
184
- }
185
-
186
- return p;
187
- }).join('/');
188
- path = `/${path}`; // /index/index -> /
189
-
190
- if (path === '/index/index') {
191
- path = '/';
192
- } // /xxxx/index -> /xxxx/
193
-
194
-
195
- path = path.replace(/\/index$/, '/'); // remove the last slash
196
- // e.g. /abc/ -> /abc
197
-
198
- if (path !== '/' && path.slice(-1) === '/') {
199
- path = path.slice(0, -1);
200
- }
201
-
202
- return path;
203
- }
204
-
205
- function normalizeRoutes(routes) {
206
- const paramsRoutes = [];
207
- const exactRoutes = [];
208
- const layoutRoutes = [];
209
- routes.forEach(route => {
210
- const __isDynamic = route.__isDynamic,
211
- exact = route.exact;
212
- delete route.__isDynamic;
213
-
214
- if (__isDynamic) {
215
- paramsRoutes.push(route);
216
- } else if (exact) {
217
- exactRoutes.push(route);
218
- } else {
219
- layoutRoutes.push(route);
220
- }
221
- });
222
- (0, _assert().default)(paramsRoutes.length <= 1, `We should not have multiple dynamic routes under a directory.`);
223
- return [...exactRoutes, ...layoutRoutes, ...paramsRoutes].reduce((memo, route) => {
224
- if (route.__toMerge && route.routes) {
225
- memo = memo.concat(route.routes);
226
- } else {
227
- memo.push(route);
228
- }
229
-
230
- return memo;
231
- }, []);
232
- }
233
-
234
- function getRoutes(opts) {
235
- const root = opts.root,
236
- _opts$relDir2 = opts.relDir,
237
- relDir = _opts$relDir2 === void 0 ? '' : _opts$relDir2,
238
- config = opts.config;
239
- const files = getFiles((0, _path().join)(root, relDir));
240
- const routes = normalizeRoutes(files.reduce(fileToRouteReducer.bind(null, opts), []));
241
-
242
- if (!relDir) {
243
- const globalLayoutFile = (0, _utils().getFile)({
244
- base: root,
245
- fileNameWithoutExt: `../${config.singular ? 'layout' : 'layouts'}/index`,
246
- type: 'javascript'
247
- });
248
-
249
- if (globalLayoutFile) {
250
- return [normalizeRoute({
251
- path: '/',
252
- component: globalLayoutFile.path,
253
- routes
254
- }, opts)];
255
- }
256
- }
257
-
258
- return routes;
259
- }
@@ -1,9 +0,0 @@
1
- import { IRoute } from './types';
2
- interface IOpts {
3
- routes: IRoute[];
4
- config: any;
5
- cwd?: string;
6
- isServer?: boolean;
7
- }
8
- export default function ({ routes, config, cwd, isServer }: IOpts): string;
9
- export {};
@@ -1,140 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = _default;
7
-
8
- function _react() {
9
- const data = _interopRequireDefault(require("react"));
10
-
11
- _react = function _react() {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _utils() {
19
- const data = require("@umijs/utils");
20
-
21
- _utils = function _utils() {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
31
-
32
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
33
-
34
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
35
-
36
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
37
-
38
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
39
-
40
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41
-
42
- const SEPARATOR = '^^^';
43
- const EMPTY_PATH = '_'; // TODO:
44
- // 1. support dynamic import (and levels)
45
- // 2. require().default -> import in production? (for tree-shaking)
46
-
47
- function _default({
48
- routes,
49
- config,
50
- cwd,
51
- isServer
52
- }) {
53
- // 因为要往 routes 里加无用的信息,所以必须 deep clone 一下,避免污染
54
- const clonedRoutes = _utils().lodash.cloneDeep(routes);
55
-
56
- if (config.dynamicImport) {
57
- patchRoutes(clonedRoutes);
58
- }
59
-
60
- function patchRoutes(routes) {
61
- routes.forEach(patchRoute);
62
- }
63
-
64
- function patchRoute(route) {
65
- if (route.component && !isFunctionComponent(route.component)) {
66
- const webpackChunkName = (0, _utils().routeToChunkName)({
67
- route,
68
- cwd
69
- }); // 解决 SSR 开启动态加载后,页面闪烁问题
70
-
71
- if (isServer && (config === null || config === void 0 ? void 0 : config.dynamicImport)) {
72
- route._chunkName = webpackChunkName;
73
- }
74
-
75
- route.component = [route.component, webpackChunkName, route.path || EMPTY_PATH].join(SEPARATOR);
76
- }
77
-
78
- if (route.routes) {
79
- patchRoutes(route.routes);
80
- }
81
- }
82
-
83
- function isFunctionComponent(component) {
84
- return /^\((.+)?\)(\s+)?=>/.test(component) || /^function([^\(]+)?\(([^\)]+)?\)([^{]+)?{/.test(component);
85
- }
86
-
87
- function replacer(key, value) {
88
- switch (key) {
89
- case 'component':
90
- if (isFunctionComponent(value)) return value;
91
-
92
- if (config.dynamicImport) {
93
- const _value$split = value.split(SEPARATOR),
94
- _value$split2 = _slicedToArray(_value$split, 2),
95
- component = _value$split2[0],
96
- webpackChunkName = _value$split2[1];
97
-
98
- let loading = '';
99
-
100
- if (config.dynamicImport.loading) {
101
- loading = `, loading: LoadingComponent`;
102
- } // server routes can't using import() for avoiding OOM when `dynamicImport`
103
-
104
-
105
- return isServer ? `require('${component}').default` : `dynamic({ loader: () => import(/* webpackChunkName: '${webpackChunkName}' */'${component}')${loading}})`;
106
- } else {
107
- return `require('${value}').default`;
108
- }
109
-
110
- case 'wrappers':
111
- const wrappers = value.map(wrapper => {
112
- if (config.dynamicImport) {
113
- let loading = '';
114
-
115
- if (config.dynamicImport.loading) {
116
- loading = `, loading: LoadingComponent`;
117
- }
118
-
119
- return isServer ? `require('${wrapper}').default` : `dynamic({ loader: () => import(/* webpackChunkName: 'wrappers' */'${wrapper}')${loading}})`;
120
- } else {
121
- return `require('${wrapper}').default`;
122
- }
123
- });
124
- return `[${wrappers.join(', ')}]`;
125
-
126
- default:
127
- return value;
128
- }
129
- }
130
-
131
- return JSON.stringify(clonedRoutes, replacer, 2).replace(/\"component\": (\"(.+?)\")/g, (global, m1, m2) => {
132
- return `"component": ${m2.replace(/\^/g, '"')}`;
133
- }).replace(/\"wrappers\": (\"(.+?)\")/g, (global, m1, m2) => {
134
- return `"wrappers": ${m2.replace(/\^/g, '"')}`;
135
- }).replace(/\\r\\n/g, '\r\n').replace(/\\n/g, '\r\n');
136
- }
137
-
138
- function lastSlash(str) {
139
- return str[str.length - 1] === '/' ? str : `${str}/`;
140
- }
@@ -1,11 +0,0 @@
1
- export interface IRoute {
2
- component?: any;
3
- exact?: boolean;
4
- path?: string;
5
- routes?: IRoute[];
6
- wrappers?: string[];
7
- title?: string;
8
- __toMerge?: boolean;
9
- __isDynamic?: boolean;
10
- [key: string]: any;
11
- }
@@ -1,38 +0,0 @@
1
- import * as utils from '@umijs/utils';
2
- import Html from '../Html/Html';
3
- import Logger from '../Logger/Logger';
4
- import { EnableBy } from './enums';
5
- import Service from './Service';
6
- import { ICommand, IHook, IPlugin, IPluginConfig, IPreset } from './types';
7
- interface IOpts {
8
- id: string;
9
- key: string;
10
- service: Service;
11
- }
12
- export default class PluginAPI {
13
- id: string;
14
- key: string;
15
- service: Service;
16
- Html: typeof Html;
17
- utils: typeof utils;
18
- logger: Logger;
19
- constructor(opts: IOpts);
20
- describe({ id, key, config, enableBy, }?: {
21
- id?: string;
22
- key?: string;
23
- config?: IPluginConfig;
24
- onChange?: any;
25
- enableBy?: EnableBy | (() => boolean);
26
- }): void;
27
- register(hook: IHook): void;
28
- registerCommand(command: ICommand): void;
29
- registerPresets(presets: (IPreset | string)[]): void;
30
- registerPlugins(plugins: (IPlugin | string)[]): void;
31
- registerMethod({ name, fn, exitsError, }: {
32
- name: string;
33
- fn?: Function;
34
- exitsError?: boolean;
35
- }): void;
36
- skipPlugins(pluginIds: string[]): void;
37
- }
38
- export {};