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