@umijs/core 4.0.0-rc.7 → 4.0.0
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.
- package/dist/index.d.ts +1 -1
- package/dist/route/routesConfig.d.ts +1 -0
- package/dist/route/routesConfig.js +45 -16
- package/dist/route/routesConvention.js +2 -2
- package/dist/route/utils.js +1 -1
- package/dist/service/command.d.ts +3 -0
- package/dist/service/command.js +1 -0
- package/dist/service/generatePlugin.js +59 -64
- package/dist/service/generator.d.ts +40 -25
- package/dist/service/generator.js +6 -12
- package/dist/service/path.d.ts +1 -0
- package/dist/service/plugin.js +3 -3
- package/dist/service/pluginAPI.d.ts +5 -2
- package/dist/service/pluginAPI.js +18 -6
- package/dist/service/service.d.ts +13 -1
- package/dist/service/service.js +320 -307
- package/dist/types.d.ts +1 -0
- package/package.json +8 -10
- package/compiled/tapable/LICENSE +0 -21
- package/compiled/tapable/index.js +0 -1
- package/compiled/tapable/package.json +0 -1
- package/compiled/tapable/tapable.d.ts +0 -116
package/dist/service/service.js
CHANGED
|
@@ -1,23 +1,14 @@
|
|
|
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
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.Service = void 0;
|
|
7
|
+
const tapable_1 = require("@umijs/bundler-utils/compiled/tapable");
|
|
16
8
|
const utils_1 = require("@umijs/utils");
|
|
17
9
|
const assert_1 = __importDefault(require("assert"));
|
|
18
10
|
const fs_1 = require("fs");
|
|
19
11
|
const path_1 = require("path");
|
|
20
|
-
const tapable_1 = require("../../compiled/tapable");
|
|
21
12
|
const config_1 = require("../config/config");
|
|
22
13
|
const constants_1 = require("../constants");
|
|
23
14
|
const types_1 = require("../types");
|
|
@@ -25,7 +16,6 @@ const env_1 = require("./env");
|
|
|
25
16
|
const path_2 = require("./path");
|
|
26
17
|
const plugin_1 = require("./plugin");
|
|
27
18
|
const pluginAPI_1 = require("./pluginAPI");
|
|
28
|
-
const utils_2 = require("./utils");
|
|
29
19
|
class Service {
|
|
30
20
|
constructor(opts) {
|
|
31
21
|
this.appData = {};
|
|
@@ -55,243 +45,271 @@ class Service {
|
|
|
55
45
|
(0, assert_1.default)((0, fs_1.existsSync)(this.cwd), `Invalid cwd ${this.cwd}, it's not found.`);
|
|
56
46
|
}
|
|
57
47
|
applyPlugins(opts) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (
|
|
63
|
-
|
|
64
|
-
|
|
48
|
+
const hooks = this.hooks[opts.key] || [];
|
|
49
|
+
let type = opts.type;
|
|
50
|
+
// guess type from key
|
|
51
|
+
if (!type) {
|
|
52
|
+
if (opts.key.startsWith('on')) {
|
|
53
|
+
type = types_1.ApplyPluginsType.event;
|
|
54
|
+
}
|
|
55
|
+
else if (opts.key.startsWith('modify')) {
|
|
56
|
+
type = types_1.ApplyPluginsType.modify;
|
|
57
|
+
}
|
|
58
|
+
else if (opts.key.startsWith('add')) {
|
|
59
|
+
type = types_1.ApplyPluginsType.add;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
throw new Error(`Invalid applyPlugins arguments, type must be supplied for key ${opts.key}.`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
switch (type) {
|
|
66
|
+
case types_1.ApplyPluginsType.add:
|
|
67
|
+
(0, assert_1.default)(!('initialValue' in opts) || Array.isArray(opts.initialValue), `applyPlugins failed, opts.initialValue must be Array if opts.type is add.`);
|
|
68
|
+
const tAdd = new tapable_1.AsyncSeriesWaterfallHook(['memo']);
|
|
69
|
+
for (const hook of hooks) {
|
|
70
|
+
if (!this.isPluginEnable(hook))
|
|
71
|
+
continue;
|
|
72
|
+
tAdd.tapPromise({
|
|
73
|
+
name: hook.plugin.key,
|
|
74
|
+
stage: hook.stage || 0,
|
|
75
|
+
before: hook.before,
|
|
76
|
+
}, async (memo) => {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
const dateStart = new Date();
|
|
79
|
+
const items = await hook.fn(opts.args);
|
|
80
|
+
(_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
81
|
+
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
82
|
+
return memo.concat(items);
|
|
83
|
+
});
|
|
65
84
|
}
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
return tAdd.promise(opts.initialValue || []);
|
|
86
|
+
case types_1.ApplyPluginsType.modify:
|
|
87
|
+
const tModify = new tapable_1.AsyncSeriesWaterfallHook(['memo']);
|
|
88
|
+
for (const hook of hooks) {
|
|
89
|
+
if (!this.isPluginEnable(hook))
|
|
90
|
+
continue;
|
|
91
|
+
tModify.tapPromise({
|
|
92
|
+
name: hook.plugin.key,
|
|
93
|
+
stage: hook.stage || 0,
|
|
94
|
+
before: hook.before,
|
|
95
|
+
}, async (memo) => {
|
|
96
|
+
var _a, _b;
|
|
97
|
+
const dateStart = new Date();
|
|
98
|
+
const ret = await hook.fn(memo, opts.args);
|
|
99
|
+
(_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
100
|
+
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
101
|
+
return ret;
|
|
102
|
+
});
|
|
68
103
|
}
|
|
69
|
-
|
|
70
|
-
|
|
104
|
+
return tModify.promise(opts.initialValue);
|
|
105
|
+
case types_1.ApplyPluginsType.event:
|
|
106
|
+
if (opts.sync) {
|
|
107
|
+
const tEvent = new tapable_1.SyncWaterfallHook(['_']);
|
|
108
|
+
hooks.forEach((hook) => {
|
|
109
|
+
if (this.isPluginEnable(hook)) {
|
|
110
|
+
tEvent.tap({
|
|
111
|
+
name: hook.plugin.key,
|
|
112
|
+
stage: hook.stage || 0,
|
|
113
|
+
before: hook.before,
|
|
114
|
+
}, () => {
|
|
115
|
+
var _a, _b;
|
|
116
|
+
const dateStart = new Date();
|
|
117
|
+
hook.fn(opts.args);
|
|
118
|
+
(_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
119
|
+
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return tEvent.call(1);
|
|
71
124
|
}
|
|
72
|
-
|
|
73
|
-
|
|
125
|
+
const tEvent = new tapable_1.AsyncSeriesWaterfallHook(['_']);
|
|
126
|
+
for (const hook of hooks) {
|
|
127
|
+
if (!this.isPluginEnable(hook))
|
|
128
|
+
continue;
|
|
129
|
+
tEvent.tapPromise({
|
|
130
|
+
name: hook.plugin.key,
|
|
131
|
+
stage: hook.stage || 0,
|
|
132
|
+
before: hook.before,
|
|
133
|
+
}, async () => {
|
|
134
|
+
var _a, _b;
|
|
135
|
+
const dateStart = new Date();
|
|
136
|
+
await hook.fn(opts.args);
|
|
137
|
+
(_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
138
|
+
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
139
|
+
});
|
|
74
140
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const tAdd = new tapable_1.AsyncSeriesWaterfallHook(['memo']);
|
|
80
|
-
for (const hook of hooks) {
|
|
81
|
-
if (!this.isPluginEnable(hook))
|
|
82
|
-
continue;
|
|
83
|
-
tAdd.tapPromise({
|
|
84
|
-
name: hook.plugin.key,
|
|
85
|
-
stage: hook.stage,
|
|
86
|
-
before: hook.before,
|
|
87
|
-
}, (memo) => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
var _a, _b;
|
|
89
|
-
const dateStart = new Date();
|
|
90
|
-
const items = yield hook.fn(opts.args);
|
|
91
|
-
(_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
92
|
-
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
93
|
-
return memo.concat(items);
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
return (yield tAdd.promise(opts.initialValue || []));
|
|
97
|
-
case types_1.ApplyPluginsType.modify:
|
|
98
|
-
const tModify = new tapable_1.AsyncSeriesWaterfallHook(['memo']);
|
|
99
|
-
for (const hook of hooks) {
|
|
100
|
-
if (!this.isPluginEnable(hook))
|
|
101
|
-
continue;
|
|
102
|
-
tModify.tapPromise({
|
|
103
|
-
name: hook.plugin.key,
|
|
104
|
-
stage: hook.stage,
|
|
105
|
-
before: hook.before,
|
|
106
|
-
}, (memo) => __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
var _c, _d;
|
|
108
|
-
const dateStart = new Date();
|
|
109
|
-
const ret = yield hook.fn(memo, opts.args);
|
|
110
|
-
(_c = hook.plugin.time.hooks)[_d = opts.key] || (_c[_d] = []);
|
|
111
|
-
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
112
|
-
return ret;
|
|
113
|
-
}));
|
|
114
|
-
}
|
|
115
|
-
return (yield tModify.promise(opts.initialValue));
|
|
116
|
-
case types_1.ApplyPluginsType.event:
|
|
117
|
-
const tEvent = new tapable_1.AsyncSeriesWaterfallHook(['_']);
|
|
118
|
-
for (const hook of hooks) {
|
|
119
|
-
if (!this.isPluginEnable(hook))
|
|
120
|
-
continue;
|
|
121
|
-
tEvent.tapPromise({
|
|
122
|
-
name: hook.plugin.key,
|
|
123
|
-
stage: hook.stage || 0,
|
|
124
|
-
before: hook.before,
|
|
125
|
-
}, () => __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
var _f, _g;
|
|
127
|
-
const dateStart = new Date();
|
|
128
|
-
yield hook.fn(opts.args);
|
|
129
|
-
(_f = hook.plugin.time.hooks)[_g = opts.key] || (_f[_g] = []);
|
|
130
|
-
hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
return (yield tEvent.promise(1));
|
|
134
|
-
default:
|
|
135
|
-
throw new Error(`applyPlugins failed, type is not defined or is not matched, got ${opts.type}.`);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
141
|
+
return tEvent.promise(1);
|
|
142
|
+
default:
|
|
143
|
+
throw new Error(`applyPlugins failed, type is not defined or is not matched, got ${opts.type}.`);
|
|
144
|
+
}
|
|
138
145
|
}
|
|
139
|
-
run(opts) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
pkgPath = (0, path_1.join)(process.cwd(), 'package.json');
|
|
164
|
-
}
|
|
165
|
-
catch (_e) { }
|
|
146
|
+
async run(opts) {
|
|
147
|
+
const { name, args = {} } = opts;
|
|
148
|
+
args._ = args._ || [];
|
|
149
|
+
// shift the command itself
|
|
150
|
+
if (args._[0] === name)
|
|
151
|
+
args._.shift();
|
|
152
|
+
this.args = args;
|
|
153
|
+
this.name = name;
|
|
154
|
+
// loadEnv
|
|
155
|
+
this.stage = types_1.ServiceStage.init;
|
|
156
|
+
(0, env_1.loadEnv)({ cwd: this.cwd, envFile: '.env' });
|
|
157
|
+
// get pkg from package.json
|
|
158
|
+
let pkg = {};
|
|
159
|
+
let pkgPath = '';
|
|
160
|
+
try {
|
|
161
|
+
pkg = require((0, path_1.join)(this.cwd, 'package.json'));
|
|
162
|
+
pkgPath = (0, path_1.join)(this.cwd, 'package.json');
|
|
163
|
+
}
|
|
164
|
+
catch (_e) {
|
|
165
|
+
// APP_ROOT
|
|
166
|
+
if (this.cwd !== process.cwd()) {
|
|
167
|
+
try {
|
|
168
|
+
pkg = require((0, path_1.join)(process.cwd(), 'package.json'));
|
|
169
|
+
pkgPath = (0, path_1.join)(process.cwd(), 'package.json');
|
|
166
170
|
}
|
|
171
|
+
catch (_e) { }
|
|
167
172
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
173
|
+
}
|
|
174
|
+
this.pkg = pkg;
|
|
175
|
+
this.pkgPath = pkgPath || (0, path_1.join)(this.cwd, 'package.json');
|
|
176
|
+
const prefix = this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME;
|
|
177
|
+
// get user config
|
|
178
|
+
const configManager = new config_1.Config({
|
|
179
|
+
cwd: this.cwd,
|
|
180
|
+
env: this.env,
|
|
181
|
+
defaultConfigFiles: this.opts.defaultConfigFiles,
|
|
182
|
+
specifiedEnv: process.env[`${prefix}_ENV`.toUpperCase()],
|
|
183
|
+
});
|
|
184
|
+
this.configManager = configManager;
|
|
185
|
+
this.userConfig = configManager.getUserConfig().config;
|
|
186
|
+
// get paths
|
|
187
|
+
const paths = (0, path_2.getPaths)({
|
|
188
|
+
cwd: this.cwd,
|
|
189
|
+
env: this.env,
|
|
190
|
+
prefix: this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME,
|
|
191
|
+
});
|
|
192
|
+
// temporary paths for use by function generateFinalConfig.
|
|
193
|
+
// the value of paths may be updated by plugins later
|
|
194
|
+
this.paths = paths;
|
|
195
|
+
// resolve initial presets and plugins
|
|
196
|
+
const { plugins, presets } = plugin_1.Plugin.getPluginsAndPresets({
|
|
197
|
+
cwd: this.cwd,
|
|
198
|
+
pkg,
|
|
199
|
+
plugins: [require.resolve('./generatePlugin')].concat(this.opts.plugins || []),
|
|
200
|
+
presets: [require.resolve('./servicePlugin')].concat(this.opts.presets || []),
|
|
201
|
+
userConfig: this.userConfig,
|
|
202
|
+
prefix,
|
|
203
|
+
});
|
|
204
|
+
// register presets and plugins
|
|
205
|
+
this.stage = types_1.ServiceStage.initPresets;
|
|
206
|
+
const presetPlugins = [];
|
|
207
|
+
while (presets.length) {
|
|
208
|
+
await this.initPreset({
|
|
209
|
+
preset: presets.shift(),
|
|
210
|
+
presets,
|
|
211
|
+
plugins: presetPlugins,
|
|
175
212
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
213
|
+
}
|
|
214
|
+
plugins.unshift(...presetPlugins);
|
|
215
|
+
this.stage = types_1.ServiceStage.initPlugins;
|
|
216
|
+
while (plugins.length) {
|
|
217
|
+
await this.initPlugin({ plugin: plugins.shift(), plugins });
|
|
218
|
+
}
|
|
219
|
+
const command = this.commands[name];
|
|
220
|
+
(0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
|
|
221
|
+
// collect configSchemas and configDefaults
|
|
222
|
+
for (const id of Object.keys(this.plugins)) {
|
|
223
|
+
const { config, key } = this.plugins[id];
|
|
224
|
+
if (config.schema)
|
|
225
|
+
this.configSchemas[key] = config.schema;
|
|
226
|
+
if (config.default !== undefined) {
|
|
227
|
+
this.configDefaults[key] = config.default;
|
|
228
|
+
}
|
|
229
|
+
this.configOnChanges[key] = config.onChange || types_1.ConfigChangeType.reload;
|
|
230
|
+
}
|
|
231
|
+
// setup api.config from modifyConfig and modifyDefaultConfig
|
|
232
|
+
this.stage = types_1.ServiceStage.resolveConfig;
|
|
233
|
+
const { config, defaultConfig } = await this.resolveConfig();
|
|
234
|
+
if (this.config.outputPath) {
|
|
235
|
+
paths.absOutputPath = (0, path_1.isAbsolute)(this.config.outputPath)
|
|
236
|
+
? this.config.outputPath
|
|
237
|
+
: (0, path_1.join)(this.cwd, this.config.outputPath);
|
|
238
|
+
}
|
|
239
|
+
this.paths = await this.applyPlugins({
|
|
240
|
+
key: 'modifyPaths',
|
|
241
|
+
initialValue: paths,
|
|
242
|
+
});
|
|
243
|
+
// applyPlugin collect app data
|
|
244
|
+
// TODO: some data is mutable
|
|
245
|
+
this.stage = types_1.ServiceStage.collectAppData;
|
|
246
|
+
this.appData = await this.applyPlugins({
|
|
247
|
+
key: 'modifyAppData',
|
|
248
|
+
initialValue: {
|
|
249
|
+
// base
|
|
181
250
|
cwd: this.cwd,
|
|
182
251
|
pkg,
|
|
183
|
-
|
|
184
|
-
|
|
252
|
+
pkgPath,
|
|
253
|
+
plugins,
|
|
254
|
+
presets,
|
|
255
|
+
name,
|
|
256
|
+
args,
|
|
257
|
+
// config
|
|
185
258
|
userConfig: this.userConfig,
|
|
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
|
-
|
|
259
|
+
mainConfigFile: configManager.mainConfigFile,
|
|
260
|
+
config,
|
|
261
|
+
defaultConfig: defaultConfig,
|
|
262
|
+
// TODO
|
|
263
|
+
// moduleGraph,
|
|
264
|
+
// routes,
|
|
265
|
+
// npmClient,
|
|
266
|
+
// nodeVersion,
|
|
267
|
+
// gitInfo,
|
|
268
|
+
// gitBranch,
|
|
269
|
+
// debugger info,
|
|
270
|
+
// devPort,
|
|
271
|
+
// devHost,
|
|
272
|
+
// env
|
|
273
|
+
},
|
|
274
|
+
});
|
|
275
|
+
// applyPlugin onCheck
|
|
276
|
+
this.stage = types_1.ServiceStage.onCheck;
|
|
277
|
+
await this.applyPlugins({
|
|
278
|
+
key: 'onCheck',
|
|
279
|
+
});
|
|
280
|
+
// applyPlugin onStart
|
|
281
|
+
this.stage = types_1.ServiceStage.onStart;
|
|
282
|
+
await this.applyPlugins({
|
|
283
|
+
key: 'onStart',
|
|
284
|
+
});
|
|
285
|
+
// run command
|
|
286
|
+
this.stage = types_1.ServiceStage.runCommand;
|
|
287
|
+
let ret = await command.fn({ args });
|
|
288
|
+
this._baconPlugins();
|
|
289
|
+
return ret;
|
|
290
|
+
}
|
|
291
|
+
async resolveConfig() {
|
|
292
|
+
// configManager and paths are not available until the init stage
|
|
293
|
+
(0, assert_1.default)(this.stage > types_1.ServiceStage.init, `Can't generate final config before init stage`);
|
|
294
|
+
const resolveMode = this.commands[this.name].configResolveMode;
|
|
295
|
+
const config = await this.applyPlugins({
|
|
296
|
+
key: 'modifyConfig',
|
|
297
|
+
// why clone deep?
|
|
298
|
+
// user may change the config in modifyConfig
|
|
299
|
+
// e.g. memo.alias = xxx
|
|
300
|
+
initialValue: utils_1.lodash.cloneDeep(resolveMode === 'strict'
|
|
301
|
+
? this.configManager.getConfig({
|
|
226
302
|
schemas: this.configSchemas,
|
|
227
|
-
}).config
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
const defaultConfig = yield this.applyPlugins({
|
|
231
|
-
key: 'modifyDefaultConfig',
|
|
232
|
-
initialValue: this.configDefaults,
|
|
233
|
-
});
|
|
234
|
-
this.config = utils_1.lodash.merge(defaultConfig, config);
|
|
235
|
-
if (this.config.outputPath) {
|
|
236
|
-
paths.absOutputPath = (0, path_1.join)(this.cwd, this.config.outputPath);
|
|
237
|
-
}
|
|
238
|
-
this.paths = yield this.applyPlugins({
|
|
239
|
-
key: 'modifyPaths',
|
|
240
|
-
initialValue: paths,
|
|
241
|
-
});
|
|
242
|
-
// applyPlugin collect app data
|
|
243
|
-
// TODO: some data is mutable
|
|
244
|
-
this.stage = types_1.ServiceStage.collectAppData;
|
|
245
|
-
this.appData = yield this.applyPlugins({
|
|
246
|
-
key: 'modifyAppData',
|
|
247
|
-
initialValue: {
|
|
248
|
-
// base
|
|
249
|
-
cwd: this.cwd,
|
|
250
|
-
pkg,
|
|
251
|
-
pkgPath,
|
|
252
|
-
plugins,
|
|
253
|
-
presets,
|
|
254
|
-
name,
|
|
255
|
-
args,
|
|
256
|
-
// config
|
|
257
|
-
userConfig: this.userConfig,
|
|
258
|
-
mainConfigFile: configManager.mainConfigFile,
|
|
259
|
-
config,
|
|
260
|
-
defaultConfig: defaultConfig,
|
|
261
|
-
// TODO
|
|
262
|
-
// moduleGraph,
|
|
263
|
-
// routes,
|
|
264
|
-
// npmClient,
|
|
265
|
-
// nodeVersion,
|
|
266
|
-
// gitInfo,
|
|
267
|
-
// gitBranch,
|
|
268
|
-
// debugger info,
|
|
269
|
-
// devPort,
|
|
270
|
-
// devHost,
|
|
271
|
-
// env
|
|
272
|
-
},
|
|
273
|
-
});
|
|
274
|
-
// applyPlugin onCheck
|
|
275
|
-
this.stage = types_1.ServiceStage.onCheck;
|
|
276
|
-
yield this.applyPlugins({
|
|
277
|
-
key: 'onCheck',
|
|
278
|
-
});
|
|
279
|
-
// applyPlugin onStart
|
|
280
|
-
this.stage = types_1.ServiceStage.onStart;
|
|
281
|
-
yield this.applyPlugins({
|
|
282
|
-
key: 'onStart',
|
|
283
|
-
});
|
|
284
|
-
// run command
|
|
285
|
-
this.stage = types_1.ServiceStage.runCommand;
|
|
286
|
-
const command = this.commands[name];
|
|
287
|
-
(0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
|
|
288
|
-
let ret = command.fn({ args });
|
|
289
|
-
if ((0, utils_2.isPromise)(ret)) {
|
|
290
|
-
ret = yield ret;
|
|
291
|
-
}
|
|
292
|
-
this._baconPlugins();
|
|
293
|
-
return ret;
|
|
303
|
+
}).config
|
|
304
|
+
: this.configManager.getUserConfig().config),
|
|
305
|
+
args: { paths: this.paths },
|
|
294
306
|
});
|
|
307
|
+
const defaultConfig = await this.applyPlugins({
|
|
308
|
+
key: 'modifyDefaultConfig',
|
|
309
|
+
initialValue: this.configDefaults,
|
|
310
|
+
});
|
|
311
|
+
this.config = utils_1.lodash.merge(defaultConfig, config);
|
|
312
|
+
return { config, defaultConfig };
|
|
295
313
|
}
|
|
296
314
|
_baconPlugins() {
|
|
297
315
|
// TODO: prettier
|
|
@@ -303,83 +321,76 @@ class Service {
|
|
|
303
321
|
}
|
|
304
322
|
}
|
|
305
323
|
}
|
|
306
|
-
initPreset(opts) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
plugins: opts.plugins,
|
|
312
|
-
});
|
|
313
|
-
opts.presets.unshift(...(presets || []));
|
|
314
|
-
opts.plugins.push(...(plugins || []));
|
|
324
|
+
async initPreset(opts) {
|
|
325
|
+
const { presets, plugins } = await this.initPlugin({
|
|
326
|
+
plugin: opts.preset,
|
|
327
|
+
presets: opts.presets,
|
|
328
|
+
plugins: opts.plugins,
|
|
315
329
|
});
|
|
330
|
+
opts.presets.unshift(...(presets || []));
|
|
331
|
+
opts.plugins.push(...(plugins || []));
|
|
316
332
|
}
|
|
317
|
-
initPlugin(opts) {
|
|
333
|
+
async initPlugin(opts) {
|
|
318
334
|
var _a, _b;
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
335
|
+
// register to this.plugins
|
|
336
|
+
(0, assert_1.default)(!this.plugins[opts.plugin.id], `${opts.plugin.type} ${opts.plugin.id} is already registered by ${(_a = this.plugins[opts.plugin.id]) === null || _a === void 0 ? void 0 : _a.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
|
|
337
|
+
this.plugins[opts.plugin.id] = opts.plugin;
|
|
338
|
+
// apply with PluginAPI
|
|
339
|
+
const pluginAPI = new pluginAPI_1.PluginAPI({
|
|
340
|
+
plugin: opts.plugin,
|
|
341
|
+
service: this,
|
|
342
|
+
});
|
|
343
|
+
pluginAPI.registerPresets = pluginAPI.registerPresets.bind(pluginAPI, opts.presets || []);
|
|
344
|
+
pluginAPI.registerPlugins = pluginAPI.registerPlugins.bind(pluginAPI, opts.plugins);
|
|
345
|
+
const proxyPluginAPI = pluginAPI_1.PluginAPI.proxyPluginAPI({
|
|
346
|
+
service: this,
|
|
347
|
+
pluginAPI,
|
|
348
|
+
serviceProps: [
|
|
349
|
+
'appData',
|
|
350
|
+
'applyPlugins',
|
|
351
|
+
'args',
|
|
352
|
+
'config',
|
|
353
|
+
'cwd',
|
|
354
|
+
'pkg',
|
|
355
|
+
'pkgPath',
|
|
356
|
+
'name',
|
|
357
|
+
'paths',
|
|
358
|
+
'userConfig',
|
|
359
|
+
'env',
|
|
360
|
+
'isPluginEnable',
|
|
361
|
+
],
|
|
362
|
+
staticProps: {
|
|
363
|
+
ApplyPluginsType: types_1.ApplyPluginsType,
|
|
364
|
+
ConfigChangeType: types_1.ConfigChangeType,
|
|
365
|
+
EnableBy: types_1.EnableBy,
|
|
366
|
+
ServiceStage: types_1.ServiceStage,
|
|
331
367
|
service: this,
|
|
332
|
-
|
|
333
|
-
serviceProps: [
|
|
334
|
-
'appData',
|
|
335
|
-
'applyPlugins',
|
|
336
|
-
'args',
|
|
337
|
-
'config',
|
|
338
|
-
'cwd',
|
|
339
|
-
'pkg',
|
|
340
|
-
'pkgPath',
|
|
341
|
-
'name',
|
|
342
|
-
'paths',
|
|
343
|
-
'userConfig',
|
|
344
|
-
'env',
|
|
345
|
-
'isPluginEnable',
|
|
346
|
-
],
|
|
347
|
-
staticProps: {
|
|
348
|
-
ApplyPluginsType: types_1.ApplyPluginsType,
|
|
349
|
-
ConfigChangeType: types_1.ConfigChangeType,
|
|
350
|
-
EnableBy: types_1.EnableBy,
|
|
351
|
-
ServiceStage: types_1.ServiceStage,
|
|
352
|
-
service: this,
|
|
353
|
-
},
|
|
354
|
-
});
|
|
355
|
-
let dateStart = new Date();
|
|
356
|
-
let ret = opts.plugin.apply()(proxyPluginAPI);
|
|
357
|
-
if ((0, utils_2.isPromise)(ret)) {
|
|
358
|
-
ret = yield ret;
|
|
359
|
-
}
|
|
360
|
-
opts.plugin.time.register = new Date().getTime() - dateStart.getTime();
|
|
361
|
-
if (opts.plugin.type === 'plugin') {
|
|
362
|
-
(0, assert_1.default)(!ret, `plugin should return nothing`);
|
|
363
|
-
}
|
|
364
|
-
// key should be unique
|
|
365
|
-
(0, assert_1.default)(!this.keyToPluginMap[opts.plugin.key], `key ${opts.plugin.key} is already registered by ${(_b = this.keyToPluginMap[opts.plugin.key]) === null || _b === void 0 ? void 0 : _b.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
|
|
366
|
-
this.keyToPluginMap[opts.plugin.key] = opts.plugin;
|
|
367
|
-
if (ret === null || ret === void 0 ? void 0 : ret.presets) {
|
|
368
|
-
ret.presets = ret.presets.map((preset) => new plugin_1.Plugin({
|
|
369
|
-
path: preset,
|
|
370
|
-
type: types_1.PluginType.preset,
|
|
371
|
-
cwd: this.cwd,
|
|
372
|
-
}));
|
|
373
|
-
}
|
|
374
|
-
if (ret === null || ret === void 0 ? void 0 : ret.plugins) {
|
|
375
|
-
ret.plugins = ret.plugins.map((plugin) => new plugin_1.Plugin({
|
|
376
|
-
path: plugin,
|
|
377
|
-
type: types_1.PluginType.plugin,
|
|
378
|
-
cwd: this.cwd,
|
|
379
|
-
}));
|
|
380
|
-
}
|
|
381
|
-
return ret || {};
|
|
368
|
+
},
|
|
382
369
|
});
|
|
370
|
+
let dateStart = new Date();
|
|
371
|
+
let ret = await opts.plugin.apply()(proxyPluginAPI);
|
|
372
|
+
opts.plugin.time.register = new Date().getTime() - dateStart.getTime();
|
|
373
|
+
if (opts.plugin.type === 'plugin') {
|
|
374
|
+
(0, assert_1.default)(!ret, `plugin should return nothing`);
|
|
375
|
+
}
|
|
376
|
+
// key should be unique
|
|
377
|
+
(0, assert_1.default)(!this.keyToPluginMap[opts.plugin.key], `key ${opts.plugin.key} is already registered by ${(_b = this.keyToPluginMap[opts.plugin.key]) === null || _b === void 0 ? void 0 : _b.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
|
|
378
|
+
this.keyToPluginMap[opts.plugin.key] = opts.plugin;
|
|
379
|
+
if (ret === null || ret === void 0 ? void 0 : ret.presets) {
|
|
380
|
+
ret.presets = ret.presets.map((preset) => new plugin_1.Plugin({
|
|
381
|
+
path: preset,
|
|
382
|
+
type: types_1.PluginType.preset,
|
|
383
|
+
cwd: this.cwd,
|
|
384
|
+
}));
|
|
385
|
+
}
|
|
386
|
+
if (ret === null || ret === void 0 ? void 0 : ret.plugins) {
|
|
387
|
+
ret.plugins = ret.plugins.map((plugin) => new plugin_1.Plugin({
|
|
388
|
+
path: plugin,
|
|
389
|
+
type: types_1.PluginType.plugin,
|
|
390
|
+
cwd: this.cwd,
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
return ret || {};
|
|
383
394
|
}
|
|
384
395
|
isPluginEnable(hook) {
|
|
385
396
|
let plugin;
|
|
@@ -388,6 +399,8 @@ class Service {
|
|
|
388
399
|
}
|
|
389
400
|
else {
|
|
390
401
|
plugin = this.keyToPluginMap[hook];
|
|
402
|
+
if (!plugin)
|
|
403
|
+
return false;
|
|
391
404
|
}
|
|
392
405
|
const { id, key, enableBy } = plugin;
|
|
393
406
|
if (this.skipPluginIds.has(id))
|