Service.js
22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _tapable() {
const data = require("@umijs/deps/compiled/tapable");
_tapable = function _tapable() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _assert() {
const data = _interopRequireDefault(require("assert"));
_assert = function _assert() {
return data;
};
return data;
}
function _events() {
const data = require("events");
_events = function _events() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
var _Config = _interopRequireDefault(require("../Config/Config"));
var _configUtils = require("../Config/utils/configUtils");
var _Logger = _interopRequireDefault(require("../Logger/Logger"));
var _enums = require("./enums");
var _getPaths = _interopRequireDefault(require("./getPaths"));
var _PluginAPI = _interopRequireDefault(require("./PluginAPI"));
var _isPromise = _interopRequireDefault(require("./utils/isPromise"));
var _loadDotEnv = _interopRequireDefault(require("./utils/loadDotEnv"));
var _pluginUtils = require("./utils/pluginUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; } } }; }
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); }
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; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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); } }
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); }); }; }
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; }
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; }
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; }
const logger = new _Logger.default('umi:core:Service'); // TODO
// 1. duplicated key
class Service extends _events().EventEmitter {
constructor(opts) {
super();
this.skipPluginIds = new Set(); // lifecycle stage
this.stage = _enums.ServiceStage.uninitialized; // registered commands
this.commands = {}; // including presets and plugins
this.plugins = {}; // plugin methods
this.pluginMethods = {}; // presets and plugins for registering
this._extraPresets = [];
this._extraPlugins = [];
this.config = null; // hooks
this.hooksByPluginId = {};
this.hooks = {}; // paths
this.paths = {};
this.ApplyPluginsType = _enums.ApplyPluginsType;
this.EnableBy = _enums.EnableBy;
this.ConfigChangeType = _enums.ConfigChangeType;
this.ServiceStage = _enums.ServiceStage;
logger.debug('opts:');
logger.debug(opts);
this.cwd = opts.cwd || process.cwd(); // repoDir should be the root dir of repo
this.pkg = opts.pkg || this.resolvePackage();
this.env = opts.env || process.env.NODE_ENV;
(0, _assert().default)((0, _fs().existsSync)(this.cwd), `cwd ${this.cwd} does not exist.`); // register babel before config parsing
this.babelRegister = new (_utils().BabelRegister)(); // load .env or .local.env
logger.debug('load env');
this.loadEnv(); // get user config without validation
logger.debug('get user config');
const configFiles = opts.configFiles;
this.configInstance = new _Config.default({
cwd: this.cwd,
service: this,
localConfig: this.env === 'development',
configFiles: Array.isArray(configFiles) && !!configFiles[0] ? configFiles : undefined
});
this.userConfig = this.configInstance.getUserConfig();
logger.debug('userConfig:');
logger.debug(this.userConfig); // get paths
this.paths = (0, _getPaths.default)({
cwd: this.cwd,
config: this.userConfig,
env: this.env
});
logger.debug('paths:');
logger.debug(this.paths); // setup initial presets and plugins
const baseOpts = {
pkg: this.pkg,
cwd: this.cwd
};
this.initialPresets = (0, _pluginUtils.resolvePresets)(_objectSpread(_objectSpread({}, baseOpts), {}, {
presets: opts.presets || [],
userConfigPresets: this.userConfig.presets || []
}));
this.initialPlugins = (0, _pluginUtils.resolvePlugins)(_objectSpread(_objectSpread({}, baseOpts), {}, {
plugins: opts.plugins || [],
userConfigPlugins: this.userConfig.plugins || []
}));
this.babelRegister.setOnlyMap({
key: 'initialPlugins',
value: _utils().lodash.uniq([...this.initialPresets.map(({
path
}) => path), ...this.initialPlugins.map(({
path
}) => path)])
});
logger.debug('initial presets:');
logger.debug(this.initialPresets);
logger.debug('initial plugins:');
logger.debug(this.initialPlugins);
}
setStage(stage) {
this.stage = stage;
}
resolvePackage() {
try {
return require((0, _path().join)(this.cwd, 'package.json'));
} catch (e) {
return {};
}
}
loadEnv() {
const basePath = (0, _path().join)(this.cwd, '.env');
const localPath = `${basePath}.local`;
(0, _loadDotEnv.default)(localPath);
(0, _loadDotEnv.default)(basePath);
}
init() {
var _this = this;
return _asyncToGenerator(function* () {
_this.setStage(_enums.ServiceStage.init); // we should have the final hooksByPluginId which is added with api.register()
yield _this.initPresetsAndPlugins(); // collect false configs, then add to this.skipPluginIds
// skipPluginIds include two parts:
// 1. api.skipPlugins()
// 2. user config with the `false` value
// Object.keys(this.hooksByPluginId).forEach(pluginId => {
// const { key } = this.plugins[pluginId];
// if (this.getPluginOptsWithKey(key) === false) {
// this.skipPluginIds.add(pluginId);
// }
// });
// delete hooks from this.hooksByPluginId with this.skipPluginIds
// for (const pluginId of this.skipPluginIds) {
// if (this.hooksByPluginId[pluginId]) delete this.hooksByPluginId[pluginId];
// delete this.plugins[pluginId];
// }
// hooksByPluginId -> hooks
// hooks is mapped with hook key, prepared for applyPlugins()
_this.setStage(_enums.ServiceStage.initHooks);
Object.keys(_this.hooksByPluginId).forEach(id => {
const hooks = _this.hooksByPluginId[id];
hooks.forEach(hook => {
const key = hook.key;
hook.pluginId = id;
_this.hooks[key] = (_this.hooks[key] || []).concat(hook);
});
}); // plugin is totally ready
_this.setStage(_enums.ServiceStage.pluginReady);
yield _this.applyPlugins({
key: 'onPluginReady',
type: _enums.ApplyPluginsType.event
}); // get config, including:
// 1. merge default config
// 2. validate
_this.setStage(_enums.ServiceStage.getConfig);
const defaultConfig = yield _this.applyPlugins({
key: 'modifyDefaultConfig',
type: _this.ApplyPluginsType.modify,
initialValue: yield _this.configInstance.getDefaultConfig()
});
_this.config = yield _this.applyPlugins({
key: 'modifyConfig',
type: _this.ApplyPluginsType.modify,
initialValue: _this.configInstance.getConfig({
defaultConfig
})
}); // merge paths to keep the this.paths ref
_this.setStage(_enums.ServiceStage.getPaths); // config.outputPath may be modified by plugins
if (_this.config.outputPath) {
_this.paths.absOutputPath = (0, _path().join)(_this.cwd, _this.config.outputPath);
}
const paths = yield _this.applyPlugins({
key: 'modifyPaths',
type: _enums.ApplyPluginsType.modify,
initialValue: _this.paths
});
Object.keys(paths).forEach(key => {
_this.paths[key] = paths[key];
});
})();
}
initPresetsAndPlugins() {
var _this2 = this;
return _asyncToGenerator(function* () {
_this2.setStage(_enums.ServiceStage.initPresets);
_this2._extraPlugins = [];
while (_this2.initialPresets.length) {
yield _this2.initPreset(_this2.initialPresets.shift());
}
_this2.setStage(_enums.ServiceStage.initPlugins);
_this2._extraPlugins.push(..._this2.initialPlugins);
while (_this2._extraPlugins.length) {
yield _this2.initPlugin(_this2._extraPlugins.shift());
}
})();
}
getPluginAPI(opts) {
const pluginAPI = new _PluginAPI.default(opts); // register built-in methods
['onPluginReady', 'modifyPaths', 'onStart', 'modifyDefaultConfig', 'modifyConfig'].forEach(name => {
pluginAPI.registerMethod({
name,
exitsError: false
});
});
return new Proxy(pluginAPI, {
get: (target, prop) => {
// 由于 pluginMethods 需要在 register 阶段可用
// 必须通过 proxy 的方式动态获取最新,以实现边注册边使用的效果
if (this.pluginMethods[prop]) return this.pluginMethods[prop];
if (['applyPlugins', 'ApplyPluginsType', 'EnableBy', 'ConfigChangeType', 'babelRegister', 'stage', 'ServiceStage', 'paths', 'cwd', 'pkg', 'userConfig', 'config', 'env', 'args', 'hasPlugins', 'hasPresets'].includes(prop)) {
return typeof this[prop] === 'function' ? this[prop].bind(this) : this[prop];
}
return target[prop];
}
});
}
applyAPI(opts) {
return _asyncToGenerator(function* () {
let ret = opts.apply()(opts.api);
if ((0, _isPromise.default)(ret)) {
ret = yield ret;
}
return ret || {};
})();
}
initPreset(preset) {
var _this3 = this;
return _asyncToGenerator(function* () {
const id = preset.id,
key = preset.key,
apply = preset.apply;
preset.isPreset = true;
const api = _this3.getPluginAPI({
id,
key,
service: _this3
}); // register before apply
_this3.registerPlugin(preset); // TODO: ...defaultConfigs 考虑要不要支持,可能这个需求可以通过其他渠道实现
const _yield$_this3$applyAP = yield _this3.applyAPI({
api,
apply
}),
presets = _yield$_this3$applyAP.presets,
plugins = _yield$_this3$applyAP.plugins,
defaultConfigs = _objectWithoutProperties(_yield$_this3$applyAP, ["presets", "plugins"]); // register extra presets and plugins
if (presets) {
(0, _assert().default)(Array.isArray(presets), `presets returned from preset ${id} must be Array.`); // 插到最前面,下个 while 循环优先执行
_this3._extraPresets.splice(0, 0, ...presets.map(path => {
return (0, _pluginUtils.pathToObj)({
type: _enums.PluginType.preset,
path,
cwd: _this3.cwd
});
}));
} // 深度优先
const extraPresets = _utils().lodash.clone(_this3._extraPresets);
_this3._extraPresets = [];
while (extraPresets.length) {
yield _this3.initPreset(extraPresets.shift());
}
if (plugins) {
(0, _assert().default)(Array.isArray(plugins), `plugins returned from preset ${id} must be Array.`);
_this3._extraPlugins.push(...plugins.map(path => {
return (0, _pluginUtils.pathToObj)({
type: _enums.PluginType.plugin,
path,
cwd: _this3.cwd
});
}));
}
})();
}
initPlugin(plugin) {
var _this4 = this;
return _asyncToGenerator(function* () {
const id = plugin.id,
key = plugin.key,
apply = plugin.apply;
const api = _this4.getPluginAPI({
id,
key,
service: _this4
}); // register before apply
_this4.registerPlugin(plugin);
yield _this4.applyAPI({
api,
apply
});
})();
}
getPluginOptsWithKey(key) {
return (0, _configUtils.getUserConfigWithKey)({
key,
userConfig: this.userConfig
});
}
registerPlugin(plugin) {
// 考虑要不要去掉这里的校验逻辑
// 理论上不会走到这里,因为在 describe 的时候已经做了冲突校验
if (this.plugins[plugin.id]) {
const name = plugin.isPreset ? 'preset' : 'plugin';
throw new Error(`\
${name} ${plugin.id} is already registered by ${this.plugins[plugin.id].path}, \
${name} from ${plugin.path} register failed.`);
}
this.plugins[plugin.id] = plugin;
}
isPluginEnable(pluginId) {
// api.skipPlugins() 的插件
if (this.skipPluginIds.has(pluginId)) return false;
const _this$plugins$pluginI = this.plugins[pluginId],
key = _this$plugins$pluginI.key,
enableBy = _this$plugins$pluginI.enableBy; // 手动设置为 false
if (this.userConfig[key] === false) return false; // 配置开启
if (enableBy === this.EnableBy.config && !(key in this.userConfig)) {
return false;
} // 函数自定义开启
if (typeof enableBy === 'function') {
return enableBy();
} // 注册开启
return true;
}
hasPlugins(pluginIds) {
return pluginIds.every(pluginId => {
const plugin = this.plugins[pluginId];
return plugin && !plugin.isPreset && this.isPluginEnable(pluginId);
});
}
hasPresets(presetIds) {
return presetIds.every(presetId => {
const preset = this.plugins[presetId];
return preset && preset.isPreset && this.isPluginEnable(presetId);
});
}
applyPlugins(opts) {
var _this5 = this;
return _asyncToGenerator(function* () {
const hooks = _this5.hooks[opts.key] || [];
switch (opts.type) {
case _enums.ApplyPluginsType.add:
if ('initialValue' in opts) {
(0, _assert().default)(Array.isArray(opts.initialValue), `applyPlugins failed, opts.initialValue must be Array if opts.type is add.`);
}
const tAdd = new (_tapable().AsyncSeriesWaterfallHook)(['memo']);
var _iterator = _createForOfIteratorHelper(hooks),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
const hook = _step.value;
if (!_this5.isPluginEnable(hook.pluginId)) {
continue;
}
tAdd.tapPromise({
name: hook.pluginId,
stage: hook.stage || 0,
// @ts-ignore
before: hook.before
}, /*#__PURE__*/function () {
var _ref = _asyncToGenerator(function* (memo) {
const items = yield hook.fn(opts.args);
return memo.concat(items);
});
return function (_x) {
return _ref.apply(this, arguments);
};
}());
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return yield tAdd.promise(opts.initialValue || []);
case _enums.ApplyPluginsType.modify:
const tModify = new (_tapable().AsyncSeriesWaterfallHook)(['memo']);
var _iterator2 = _createForOfIteratorHelper(hooks),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
const hook = _step2.value;
if (!_this5.isPluginEnable(hook.pluginId)) {
continue;
}
tModify.tapPromise({
name: hook.pluginId,
stage: hook.stage || 0,
// @ts-ignore
before: hook.before
}, /*#__PURE__*/function () {
var _ref2 = _asyncToGenerator(function* (memo) {
return yield hook.fn(memo, opts.args);
});
return function (_x2) {
return _ref2.apply(this, arguments);
};
}());
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return yield tModify.promise(opts.initialValue);
case _enums.ApplyPluginsType.event:
const tEvent = new (_tapable().AsyncSeriesWaterfallHook)(['_']);
var _iterator3 = _createForOfIteratorHelper(hooks),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
const hook = _step3.value;
if (!_this5.isPluginEnable(hook.pluginId)) {
continue;
}
tEvent.tapPromise({
name: hook.pluginId,
stage: hook.stage || 0,
// @ts-ignore
before: hook.before
}, /*#__PURE__*/_asyncToGenerator(function* () {
yield hook.fn(opts.args);
}));
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return yield tEvent.promise();
default:
throw new Error(`applyPlugin failed, type is not defined or is not matched, got ${opts.type}.`);
}
})();
}
run({
name,
args = {}
}) {
var _this6 = this;
return _asyncToGenerator(function* () {
args._ = args._ || []; // shift the command itself
if (args._[0] === name) args._.shift();
_this6.args = args;
yield _this6.init();
logger.debug('plugins:');
logger.debug(_this6.plugins);
_this6.setStage(_enums.ServiceStage.run);
yield _this6.applyPlugins({
key: 'onStart',
type: _enums.ApplyPluginsType.event,
args: {
name,
args
}
});
return _this6.runCommand({
name,
args
});
})();
}
runCommand({
name,
args = {}
}) {
var _this7 = this;
return _asyncToGenerator(function* () {
(0, _assert().default)(_this7.stage >= _enums.ServiceStage.init, `service is not initialized.`);
args._ = args._ || []; // shift the command itself
if (args._[0] === name) args._.shift();
const command = typeof _this7.commands[name] === 'string' ? _this7.commands[_this7.commands[name]] : _this7.commands[name];
(0, _assert().default)(command, `run command failed, command ${name} does not exists.`);
const fn = command.fn;
return fn({
args
});
})();
}
}
exports.default = Service;