@umijs/core 3.5.20 → 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,348 +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 _joi() {
19
- const data = _interopRequireDefault(require("@umijs/deps/compiled/@hapi/joi"));
20
-
21
- _joi = function _joi() {
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
- var _enums = require("../Service/enums");
69
-
70
- var _configUtils = require("./utils/configUtils");
71
-
72
- var _isEqual = _interopRequireDefault(require("./utils/isEqual"));
73
-
74
- var _mergeDefault = _interopRequireDefault(require("./utils/mergeDefault"));
75
-
76
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
77
-
78
- 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); } }
79
-
80
- 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); }); }; }
81
-
82
- const debug = (0, _utils().createDebug)('umi:core:Config');
83
- const DEFAULT_CONFIG_FILES = ['.umirc.ts', '.umirc.js', 'config/config.ts', 'config/config.js']; // TODO:
84
- // 1. custom config file
85
-
86
- class Config {
87
- constructor(opts) {
88
- this.cwd = void 0;
89
- this.service = void 0;
90
- this.config = void 0;
91
- this.localConfig = void 0;
92
- this.configFile = void 0;
93
- this.configFiles = DEFAULT_CONFIG_FILES;
94
- this.cwd = opts.cwd || process.cwd();
95
- this.service = opts.service;
96
- this.localConfig = opts.localConfig;
97
-
98
- if (Array.isArray(opts.configFiles)) {
99
- // 配置的优先读取
100
- this.configFiles = _utils().lodash.uniq(opts.configFiles.concat(this.configFiles));
101
- }
102
- }
103
-
104
- getDefaultConfig() {
105
- var _this = this;
106
-
107
- return _asyncToGenerator(function* () {
108
- const pluginIds = Object.keys(_this.service.plugins); // collect default config
109
-
110
- let defaultConfig = pluginIds.reduce((memo, pluginId) => {
111
- const _this$service$plugins = _this.service.plugins[pluginId],
112
- key = _this$service$plugins.key,
113
- _this$service$plugins2 = _this$service$plugins.config,
114
- config = _this$service$plugins2 === void 0 ? {} : _this$service$plugins2;
115
- if ('default' in config) memo[key] = config.default;
116
- return memo;
117
- }, {});
118
- return defaultConfig;
119
- })();
120
- }
121
-
122
- getConfig({
123
- defaultConfig
124
- }) {
125
- (0, _assert().default)(this.service.stage >= _enums.ServiceStage.pluginReady, `Config.getConfig() failed, it should not be executed before plugin is ready.`);
126
- const userConfig = this.getUserConfig(); // 用于提示用户哪些 key 是未定义的
127
- // TODO: 考虑不排除 false 的 key
128
-
129
- const userConfigKeys = Object.keys(userConfig).filter(key => {
130
- return userConfig[key] !== false;
131
- }); // get config
132
-
133
- const pluginIds = Object.keys(this.service.plugins);
134
- pluginIds.forEach(pluginId => {
135
- const _this$service$plugins3 = this.service.plugins[pluginId],
136
- key = _this$service$plugins3.key,
137
- _this$service$plugins4 = _this$service$plugins3.config,
138
- config = _this$service$plugins4 === void 0 ? {} : _this$service$plugins4; // recognize as key if have schema config
139
-
140
- if (!config.schema) return;
141
- const value = (0, _configUtils.getUserConfigWithKey)({
142
- key,
143
- userConfig
144
- }); // 不校验 false 的值,此时已禁用插件
145
-
146
- if (value === false) return; // do validate
147
-
148
- const schema = config.schema(_joi().default);
149
- (0, _assert().default)(_joi().default.isSchema(schema), `schema return from plugin ${pluginId} is not valid schema.`);
150
-
151
- const _schema$validate = schema.validate(value),
152
- error = _schema$validate.error;
153
-
154
- if (error) {
155
- const e = new Error(`Validate config "${key}" failed, ${error.message}`);
156
- e.stack = error.stack;
157
- throw e;
158
- } // remove key
159
-
160
-
161
- const index = userConfigKeys.indexOf(key.split('.')[0]);
162
-
163
- if (index !== -1) {
164
- userConfigKeys.splice(index, 1);
165
- } // update userConfig with defaultConfig
166
-
167
-
168
- if (key in defaultConfig) {
169
- const newValue = (0, _mergeDefault.default)({
170
- defaultConfig: defaultConfig[key],
171
- config: value
172
- });
173
- (0, _configUtils.updateUserConfigWithKey)({
174
- key,
175
- value: newValue,
176
- userConfig
177
- });
178
- }
179
- });
180
-
181
- if (userConfigKeys.length) {
182
- const keys = userConfigKeys.length > 1 ? 'keys' : 'key';
183
- throw new Error(`Invalid config ${keys}: ${userConfigKeys.join(', ')}`);
184
- }
185
-
186
- return userConfig;
187
- }
188
-
189
- getUserConfig() {
190
- const configFile = this.getConfigFile();
191
- this.configFile = configFile; // 潜在问题:
192
- // .local 和 .env 的配置必须有 configFile 才有效
193
-
194
- if (configFile) {
195
- let envConfigFile;
196
-
197
- if (process.env.UMI_ENV) {
198
- var _getFile;
199
-
200
- const envConfigFileName = this.addAffix(configFile, process.env.UMI_ENV);
201
- const fileNameWithoutExt = envConfigFileName.replace((0, _path().extname)(envConfigFileName), '');
202
- envConfigFile = (_getFile = (0, _utils().getFile)({
203
- base: this.cwd,
204
- fileNameWithoutExt,
205
- type: 'javascript'
206
- })) === null || _getFile === void 0 ? void 0 : _getFile.filename;
207
-
208
- if (!envConfigFile) {
209
- throw new Error(`get user config failed, ${envConfigFile} does not exist, but process.env.UMI_ENV is set to ${process.env.UMI_ENV}.`);
210
- }
211
- }
212
-
213
- const files = [configFile, envConfigFile, this.localConfig && this.addAffix(configFile, 'local')].filter(f => !!f).map(f => (0, _path().join)(this.cwd, f)).filter(f => (0, _fs().existsSync)(f)); // clear require cache and set babel register
214
-
215
- const requireDeps = files.reduce((memo, file) => {
216
- memo = memo.concat((0, _utils().parseRequireDeps)(file));
217
- return memo;
218
- }, []);
219
- requireDeps.forEach(_utils().cleanRequireCache);
220
- this.service.babelRegister.setOnlyMap({
221
- key: 'config',
222
- value: requireDeps
223
- }); // require config and merge
224
-
225
- return this.mergeConfig(...this.requireConfigs(files));
226
- } else {
227
- return {};
228
- }
229
- }
230
-
231
- addAffix(file, affix) {
232
- const ext = (0, _path().extname)(file);
233
- return file.replace(new RegExp(`${ext}$`), `.${affix}${ext}`);
234
- }
235
-
236
- requireConfigs(configFiles) {
237
- return configFiles.map(f => (0, _utils().compatESModuleRequire)(require(f)));
238
- }
239
-
240
- mergeConfig(...configs) {
241
- let ret = {};
242
-
243
- for (var _i = 0, _configs = configs; _i < _configs.length; _i++) {
244
- const config = _configs[_i];
245
- // TODO: 精细化处理,比如处理 dotted config key
246
- ret = (0, _utils().deepmerge)(ret, config);
247
- }
248
-
249
- return ret;
250
- }
251
-
252
- getConfigFile() {
253
- // TODO: support custom config file
254
- const configFile = this.configFiles.find(f => (0, _fs().existsSync)((0, _path().join)(this.cwd, f)));
255
- return configFile ? (0, _utils().winPath)(configFile) : null;
256
- }
257
-
258
- getWatchFilesAndDirectories() {
259
- const umiEnv = process.env.UMI_ENV;
260
-
261
- const configFiles = _utils().lodash.clone(this.configFiles);
262
-
263
- this.configFiles.forEach(f => {
264
- if (this.localConfig) configFiles.push(this.addAffix(f, 'local'));
265
- if (umiEnv) configFiles.push(this.addAffix(f, umiEnv));
266
- });
267
- const configDir = (0, _utils().winPath)((0, _path().join)(this.cwd, 'config'));
268
- const files = configFiles.reduce((memo, f) => {
269
- const file = (0, _utils().winPath)((0, _path().join)(this.cwd, f));
270
-
271
- if ((0, _fs().existsSync)(file)) {
272
- memo = memo.concat((0, _utils().parseRequireDeps)(file));
273
- } else {
274
- memo.push(file);
275
- }
276
-
277
- return memo;
278
- }, []).filter(f => !f.startsWith(configDir));
279
- return [configDir].concat(files);
280
- }
281
-
282
- watch(opts) {
283
- let paths = this.getWatchFilesAndDirectories();
284
- let userConfig = opts.userConfig;
285
-
286
- const watcher = _utils().chokidar.watch(paths, {
287
- ignoreInitial: true,
288
- cwd: this.cwd
289
- });
290
-
291
- watcher.on('all', (event, path) => {
292
- console.log(_utils().chalk.green(`[${event}] ${path}`));
293
- const newPaths = this.getWatchFilesAndDirectories();
294
-
295
- const diffs = _utils().lodash.difference(newPaths, paths);
296
-
297
- if (diffs.length) {
298
- watcher.add(diffs);
299
- paths = paths.concat(diffs);
300
- }
301
-
302
- const newUserConfig = this.getUserConfig();
303
- const pluginChanged = [];
304
- const valueChanged = [];
305
- Object.keys(this.service.plugins).forEach(pluginId => {
306
- const _this$service$plugins5 = this.service.plugins[pluginId],
307
- key = _this$service$plugins5.key,
308
- _this$service$plugins6 = _this$service$plugins5.config,
309
- config = _this$service$plugins6 === void 0 ? {} : _this$service$plugins6; // recognize as key if have schema config
310
-
311
- if (!config.schema) return;
312
-
313
- if (!(0, _isEqual.default)(newUserConfig[key], userConfig[key])) {
314
- const changed = {
315
- key,
316
- pluginId: pluginId
317
- };
318
-
319
- if (newUserConfig[key] === false || userConfig[key] === false) {
320
- pluginChanged.push(changed);
321
- } else {
322
- valueChanged.push(changed);
323
- }
324
- }
325
- });
326
- debug(`newUserConfig: ${JSON.stringify(newUserConfig)}`);
327
- debug(`oldUserConfig: ${JSON.stringify(userConfig)}`);
328
- debug(`pluginChanged: ${JSON.stringify(pluginChanged)}`);
329
- debug(`valueChanged: ${JSON.stringify(valueChanged)}`);
330
-
331
- if (pluginChanged.length || valueChanged.length) {
332
- opts.onChange({
333
- userConfig: newUserConfig,
334
- pluginChanged,
335
- valueChanged
336
- });
337
- }
338
-
339
- userConfig = newUserConfig;
340
- });
341
- return () => {
342
- watcher.close();
343
- };
344
- }
345
-
346
- }
347
-
348
- exports.default = Config;
@@ -1,21 +0,0 @@
1
- import { BaseIConfig } from '@umijs/types';
2
- import { IRoute } from '..';
3
-
4
- type WithFalse<T> = {
5
- [P in keyof T]?: T[P] | false;
6
- };
7
-
8
- export interface BaseIConfig {
9
- singular?: boolean;
10
- outputPath?: string;
11
- publicPath?: string;
12
- title?: string;
13
- mountElementId?: string;
14
- routes?: IRoute[];
15
- exportStatic?: {
16
- htmlSuffix?: boolean;
17
- dynamicRoot?: boolean;
18
- };
19
- }
20
-
21
- export type IConfig = WithFalse<BaseIConfig>;
@@ -1,9 +0,0 @@
1
- export declare function updateUserConfigWithKey({ key, value, userConfig, }: {
2
- key: string;
3
- value: any;
4
- userConfig: object;
5
- }): void;
6
- export declare function getUserConfigWithKey({ key, userConfig, }: {
7
- key: string;
8
- userConfig: object;
9
- }): any;
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.updateUserConfigWithKey = updateUserConfigWithKey;
7
- exports.getUserConfigWithKey = getUserConfigWithKey;
8
-
9
- function _react() {
10
- const data = _interopRequireDefault(require("react"));
11
-
12
- _react = function _react() {
13
- return data;
14
- };
15
-
16
- return data;
17
- }
18
-
19
- function _setValue() {
20
- const data = _interopRequireDefault(require("@umijs/deps/compiled/set-value"));
21
-
22
- _setValue = function _setValue() {
23
- return data;
24
- };
25
-
26
- return data;
27
- }
28
-
29
- function _utils() {
30
- const data = require("@umijs/utils");
31
-
32
- _utils = function _utils() {
33
- return data;
34
- };
35
-
36
- return data;
37
- }
38
-
39
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
-
41
- function updateUserConfigWithKey({
42
- key,
43
- value,
44
- userConfig
45
- }) {
46
- (0, _setValue().default)(userConfig, key, value);
47
- }
48
-
49
- function getUserConfigWithKey({
50
- key,
51
- userConfig
52
- }) {
53
- return _utils().lodash.get(userConfig, key);
54
- }
@@ -1 +0,0 @@
1
- export default function (a: any, b: any): boolean;
@@ -1,45 +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 funcToStr(obj) {
31
- if (typeof obj === 'function') return obj.toString();
32
-
33
- if (_utils().lodash.isPlainObject(obj)) {
34
- return Object.keys(obj).reduce((memo, key) => {
35
- memo[key] = funcToStr(obj[key]);
36
- return memo;
37
- }, {});
38
- } else {
39
- return obj;
40
- }
41
- }
42
-
43
- function _default(a, b) {
44
- return _utils().lodash.isEqual(funcToStr(a), funcToStr(b));
45
- }
@@ -1,6 +0,0 @@
1
- interface IOpts {
2
- defaultConfig: any;
3
- config: any;
4
- }
5
- declare const _default: ({ defaultConfig, config }: IOpts) => any;
6
- export default _default;
@@ -1,41 +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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
- var _default = ({
31
- defaultConfig,
32
- config
33
- }) => {
34
- if (_utils().lodash.isPlainObject(defaultConfig) && _utils().lodash.isPlainObject(config)) {
35
- return (0, _utils().deepmerge)(defaultConfig, config);
36
- }
37
-
38
- return typeof config !== 'undefined' ? config : defaultConfig;
39
- };
40
-
41
- exports.default = _default;
@@ -1,16 +0,0 @@
1
- import { IConfig } from '..';
2
- import { IGetContentArgs, IOpts, IScript } from './types';
3
- declare class Html {
4
- config: IConfig;
5
- tplPath?: string;
6
- constructor(opts: IOpts);
7
- getHtmlPath(path: string): string;
8
- getRelPathToPublicPath(path: string): string;
9
- getAsset(opts: {
10
- file: string;
11
- path?: string;
12
- }): string;
13
- getScriptsContent(scripts: IScript[]): string;
14
- getContent(args: IGetContentArgs): Promise<string>;
15
- }
16
- export default Html;