@umijs/core 4.0.7 → 4.0.10

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,434 +1,399 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
4
10
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Service = void 0;
7
- const tapable_1 = require("@umijs/bundler-utils/compiled/tapable");
8
- const utils_1 = require("@umijs/utils");
9
- const assert_1 = __importDefault(require("assert"));
10
- const fs_1 = require("fs");
11
- const path_1 = require("path");
12
- const config_1 = require("../config/config");
13
- const constants_1 = require("../constants");
14
- const types_1 = require("../types");
15
- const env_1 = require("./env");
16
- const path_2 = require("./path");
17
- const plugin_1 = require("./plugin");
18
- const pluginAPI_1 = require("./pluginAPI");
19
- class Service {
20
- constructor(opts) {
21
- this.appData = {};
22
- this.args = { _: [], $0: '' };
23
- this.commands = {};
24
- this.generators = {};
25
- this.config = {};
26
- this.configSchemas = {};
27
- this.configDefaults = {};
28
- this.configOnChanges = {};
29
- this.hooks = {};
30
- this.name = '';
31
- this.paths = {};
32
- // preset is plugin with different type
33
- this.plugins = {};
34
- this.keyToPluginMap = {};
35
- this.pluginMethods = {};
36
- this.skipPluginIds = new Set();
37
- this.stage = types_1.ServiceStage.uninitialized;
38
- this.userConfig = {};
39
- this.configManager = null;
40
- this.pkg = {};
41
- this.pkgPath = '';
42
- this.cwd = opts.cwd;
43
- this.env = opts.env;
44
- this.opts = opts;
45
- (0, assert_1.default)((0, fs_1.existsSync)(this.cwd), `Invalid cwd ${this.cwd}, it's not found.`);
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // src/service/service.ts
23
+ var service_exports = {};
24
+ __export(service_exports, {
25
+ Service: () => Service
26
+ });
27
+ module.exports = __toCommonJS(service_exports);
28
+ var import_tapable = require("@umijs/bundler-utils/compiled/tapable");
29
+ var import_utils = require("@umijs/utils");
30
+ var import_assert = __toESM(require("assert"));
31
+ var import_fs = require("fs");
32
+ var import_path = require("path");
33
+ var import_config = require("../config/config");
34
+ var import_constants = require("../constants");
35
+ var import_types = require("../types");
36
+ var import_env = require("./env");
37
+ var import_path2 = require("./path");
38
+ var import_plugin = require("./plugin");
39
+ var import_pluginAPI = require("./pluginAPI");
40
+ var Service = class {
41
+ constructor(opts) {
42
+ this.appData = {};
43
+ this.args = { _: [], $0: "" };
44
+ this.commands = {};
45
+ this.generators = {};
46
+ this.config = {};
47
+ this.configSchemas = {};
48
+ this.configDefaults = {};
49
+ this.configOnChanges = {};
50
+ this.hooks = {};
51
+ this.name = "";
52
+ this.paths = {};
53
+ this.plugins = {};
54
+ this.keyToPluginMap = {};
55
+ this.pluginMethods = {};
56
+ this.skipPluginIds = /* @__PURE__ */ new Set();
57
+ this.stage = import_types.ServiceStage.uninitialized;
58
+ this.userConfig = {};
59
+ this.configManager = null;
60
+ this.pkg = {};
61
+ this.pkgPath = "";
62
+ this.cwd = opts.cwd;
63
+ this.env = opts.env;
64
+ this.opts = opts;
65
+ (0, import_assert.default)((0, import_fs.existsSync)(this.cwd), `Invalid cwd ${this.cwd}, it's not found.`);
66
+ }
67
+ applyPlugins(opts) {
68
+ const hooks = this.hooks[opts.key] || [];
69
+ let type = opts.type;
70
+ if (!type) {
71
+ if (opts.key.startsWith("on")) {
72
+ type = import_types.ApplyPluginsType.event;
73
+ } else if (opts.key.startsWith("modify")) {
74
+ type = import_types.ApplyPluginsType.modify;
75
+ } else if (opts.key.startsWith("add")) {
76
+ type = import_types.ApplyPluginsType.add;
77
+ } else {
78
+ throw new Error(`Invalid applyPlugins arguments, type must be supplied for key ${opts.key}.`);
79
+ }
46
80
  }
47
- applyPlugins(opts) {
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
- });
84
- }
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
- });
103
- }
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);
124
- }
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
- });
140
- }
141
- return tEvent.promise(1);
142
- default:
143
- throw new Error(`applyPlugins failed, type is not defined or is not matched, got ${opts.type}.`);
81
+ switch (type) {
82
+ case import_types.ApplyPluginsType.add:
83
+ (0, import_assert.default)(!("initialValue" in opts) || Array.isArray(opts.initialValue), `applyPlugins failed, opts.initialValue must be Array if opts.type is add.`);
84
+ const tAdd = new import_tapable.AsyncSeriesWaterfallHook(["memo"]);
85
+ for (const hook of hooks) {
86
+ if (!this.isPluginEnable(hook))
87
+ continue;
88
+ tAdd.tapPromise({
89
+ name: hook.plugin.key,
90
+ stage: hook.stage || 0,
91
+ before: hook.before
92
+ }, async (memo) => {
93
+ var _a, _b;
94
+ const dateStart = new Date();
95
+ const items = await hook.fn(opts.args);
96
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
97
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
98
+ return memo.concat(items);
99
+ });
144
100
  }
145
- }
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');
101
+ return tAdd.promise(opts.initialValue || []);
102
+ case import_types.ApplyPluginsType.modify:
103
+ const tModify = new import_tapable.AsyncSeriesWaterfallHook(["memo"]);
104
+ for (const hook of hooks) {
105
+ if (!this.isPluginEnable(hook))
106
+ continue;
107
+ tModify.tapPromise({
108
+ name: hook.plugin.key,
109
+ stage: hook.stage || 0,
110
+ before: hook.before
111
+ }, async (memo) => {
112
+ var _a, _b;
113
+ const dateStart = new Date();
114
+ const ret = await hook.fn(memo, opts.args);
115
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
116
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
117
+ return ret;
118
+ });
163
119
  }
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');
170
- }
171
- catch (_e) { }
120
+ return tModify.promise(opts.initialValue);
121
+ case import_types.ApplyPluginsType.event:
122
+ if (opts.sync) {
123
+ const tEvent2 = new import_tapable.SyncWaterfallHook(["_"]);
124
+ hooks.forEach((hook) => {
125
+ if (this.isPluginEnable(hook)) {
126
+ tEvent2.tap({
127
+ name: hook.plugin.key,
128
+ stage: hook.stage || 0,
129
+ before: hook.before
130
+ }, () => {
131
+ var _a, _b;
132
+ const dateStart = new Date();
133
+ hook.fn(opts.args);
134
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
135
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
136
+ });
172
137
  }
138
+ });
139
+ return tEvent2.call(1);
173
140
  }
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,
212
- });
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;
141
+ const tEvent = new import_tapable.AsyncSeriesWaterfallHook(["_"]);
142
+ for (const hook of hooks) {
143
+ if (!this.isPluginEnable(hook))
144
+ continue;
145
+ tEvent.tapPromise({
146
+ name: hook.plugin.key,
147
+ stage: hook.stage || 0,
148
+ before: hook.before
149
+ }, async () => {
150
+ var _a, _b;
151
+ const dateStart = new Date();
152
+ await hook.fn(opts.args);
153
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
154
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
155
+ });
230
156
  }
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);
157
+ return tEvent.promise(1);
158
+ default:
159
+ throw new Error(`applyPlugins failed, type is not defined or is not matched, got ${opts.type}.`);
160
+ }
161
+ }
162
+ async run(opts) {
163
+ const { name, args = {} } = opts;
164
+ args._ = args._ || [];
165
+ if (args._[0] === name)
166
+ args._.shift();
167
+ this.args = args;
168
+ this.name = name;
169
+ this.stage = import_types.ServiceStage.init;
170
+ (0, import_env.loadEnv)({ cwd: this.cwd, envFile: ".env" });
171
+ let pkg = {};
172
+ let pkgPath = "";
173
+ try {
174
+ pkg = require((0, import_path.join)(this.cwd, "package.json"));
175
+ pkgPath = (0, import_path.join)(this.cwd, "package.json");
176
+ } catch (_e) {
177
+ if (this.cwd !== process.cwd()) {
178
+ try {
179
+ pkg = require((0, import_path.join)(process.cwd(), "package.json"));
180
+ pkgPath = (0, import_path.join)(process.cwd(), "package.json");
181
+ } catch (_e2) {
238
182
  }
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
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
- 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;
183
+ }
290
184
  }
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({
302
- schemas: this.configSchemas,
303
- }).config
304
- : this.configManager.getUserConfig().config),
305
- args: { paths: this.paths },
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 };
185
+ this.pkg = pkg;
186
+ this.pkgPath = pkgPath || (0, import_path.join)(this.cwd, "package.json");
187
+ const prefix = this.opts.frameworkName || import_constants.DEFAULT_FRAMEWORK_NAME;
188
+ const configManager = new import_config.Config({
189
+ cwd: this.cwd,
190
+ env: this.env,
191
+ defaultConfigFiles: this.opts.defaultConfigFiles,
192
+ specifiedEnv: process.env[`${prefix}_ENV`.toUpperCase()]
193
+ });
194
+ this.configManager = configManager;
195
+ this.userConfig = configManager.getUserConfig().config;
196
+ const paths = (0, import_path2.getPaths)({
197
+ cwd: this.cwd,
198
+ env: this.env,
199
+ prefix: this.opts.frameworkName || import_constants.DEFAULT_FRAMEWORK_NAME
200
+ });
201
+ this.paths = paths;
202
+ const { plugins, presets } = import_plugin.Plugin.getPluginsAndPresets({
203
+ cwd: this.cwd,
204
+ pkg,
205
+ plugins: [require.resolve("./generatePlugin")].concat(this.opts.plugins || []),
206
+ presets: [require.resolve("./servicePlugin")].concat(this.opts.presets || []),
207
+ userConfig: this.userConfig,
208
+ prefix
209
+ });
210
+ this.stage = import_types.ServiceStage.initPresets;
211
+ const presetPlugins = [];
212
+ while (presets.length) {
213
+ await this.initPreset({
214
+ preset: presets.shift(),
215
+ presets,
216
+ plugins: presetPlugins
217
+ });
313
218
  }
314
- _baconPlugins() {
315
- // TODO: prettier
316
- if (this.args.baconPlugins) {
317
- console.log();
318
- for (const id of Object.keys(this.plugins)) {
319
- const plugin = this.plugins[id];
320
- console.log(utils_1.chalk.green('plugin'), plugin.id, plugin.time);
321
- }
322
- }
219
+ plugins.unshift(...presetPlugins);
220
+ this.stage = import_types.ServiceStage.initPlugins;
221
+ while (plugins.length) {
222
+ await this.initPlugin({ plugin: plugins.shift(), plugins });
323
223
  }
324
- async initPreset(opts) {
325
- const { presets, plugins } = await this.initPlugin({
326
- plugin: opts.preset,
327
- presets: opts.presets,
328
- plugins: opts.plugins,
329
- });
330
- opts.presets.unshift(...(presets || []));
331
- opts.plugins.push(...(plugins || []));
224
+ const command = this.commands[name];
225
+ (0, import_assert.default)(command, `Invalid command ${name}, it's not registered.`);
226
+ for (const id of Object.keys(this.plugins)) {
227
+ const { config: config2, key } = this.plugins[id];
228
+ if (config2.schema)
229
+ this.configSchemas[key] = config2.schema;
230
+ if (config2.default !== void 0) {
231
+ this.configDefaults[key] = config2.default;
232
+ }
233
+ this.configOnChanges[key] = config2.onChange || import_types.ConfigChangeType.reload;
332
234
  }
333
- async initPlugin(opts) {
334
- var _a, _b;
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,
367
- service: this,
368
- },
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 || {};
235
+ this.stage = import_types.ServiceStage.resolveConfig;
236
+ const { config, defaultConfig } = await this.resolveConfig();
237
+ if (this.config.outputPath) {
238
+ paths.absOutputPath = (0, import_path.isAbsolute)(this.config.outputPath) ? this.config.outputPath : (0, import_path.join)(this.cwd, this.config.outputPath);
394
239
  }
395
- isPluginEnable(hook) {
396
- let plugin;
397
- if (hook.plugin) {
398
- plugin = hook.plugin;
399
- }
400
- else {
401
- plugin = this.keyToPluginMap[hook];
402
- if (!plugin)
403
- return false;
404
- }
405
- const { id, key, enableBy } = plugin;
406
- if (this.skipPluginIds.has(id))
407
- return false;
408
- if (this.userConfig[key] === false)
409
- return false;
410
- if (this.config[key] === false)
411
- return false;
412
- if (enableBy === types_1.EnableBy.config) {
413
- // TODO: 提供单独的命令用于启用插件
414
- // this.userConfig 中如果存在,启用
415
- // this.config 好了之后如果存在,启用
416
- // this.config modifyConfig 和 modifyDefaultConfig 之后才会 ready
417
- // 这意味着 modifyConfig 和 modifyDefaultConfig 只能判断 api.userConfig
418
- // 举个具体场景:
419
- // - p1 enableBy config, p2 modifyDefaultConfig p1 = {}
420
- // - p1 里 modifyConfig 和 modifyDefaultConfig 仅 userConfig 里有 p1 有效,其他 p2 开启时即有效
421
- // - p2 里因为用了 modifyDefaultConfig,如果 p2 是 enableBy config,需要 userConfig 里配 p2,p2 和 p1 才有效
422
- return key in this.userConfig || (this.config && key in this.config);
423
- }
424
- if (typeof enableBy === 'function')
425
- return enableBy({
426
- userConfig: this.userConfig,
427
- config: this.config,
428
- env: this.env,
429
- });
430
- // EnableBy.register
431
- return true;
240
+ this.paths = await this.applyPlugins({
241
+ key: "modifyPaths",
242
+ initialValue: paths
243
+ });
244
+ this.stage = import_types.ServiceStage.collectAppData;
245
+ this.appData = await this.applyPlugins({
246
+ key: "modifyAppData",
247
+ initialValue: {
248
+ cwd: this.cwd,
249
+ pkg,
250
+ pkgPath,
251
+ plugins,
252
+ presets,
253
+ name,
254
+ args,
255
+ userConfig: this.userConfig,
256
+ mainConfigFile: configManager.mainConfigFile,
257
+ config,
258
+ defaultConfig
259
+ }
260
+ });
261
+ this.stage = import_types.ServiceStage.onCheck;
262
+ await this.applyPlugins({
263
+ key: "onCheck"
264
+ });
265
+ this.stage = import_types.ServiceStage.onStart;
266
+ await this.applyPlugins({
267
+ key: "onStart"
268
+ });
269
+ this.stage = import_types.ServiceStage.runCommand;
270
+ let ret = await command.fn({ args });
271
+ this._baconPlugins();
272
+ return ret;
273
+ }
274
+ async resolveConfig() {
275
+ (0, import_assert.default)(this.stage > import_types.ServiceStage.init, `Can't generate final config before init stage`);
276
+ const resolveMode = this.commands[this.name].configResolveMode;
277
+ const config = await this.applyPlugins({
278
+ key: "modifyConfig",
279
+ initialValue: import_utils.lodash.cloneDeep(resolveMode === "strict" ? this.configManager.getConfig({
280
+ schemas: this.configSchemas
281
+ }).config : this.configManager.getUserConfig().config),
282
+ args: { paths: this.paths }
283
+ });
284
+ const defaultConfig = await this.applyPlugins({
285
+ key: "modifyDefaultConfig",
286
+ initialValue: this.configDefaults
287
+ });
288
+ this.config = import_utils.lodash.merge(defaultConfig, config);
289
+ return { config, defaultConfig };
290
+ }
291
+ _baconPlugins() {
292
+ if (this.args.baconPlugins) {
293
+ console.log();
294
+ for (const id of Object.keys(this.plugins)) {
295
+ const plugin = this.plugins[id];
296
+ console.log(import_utils.chalk.green("plugin"), plugin.id, plugin.time);
297
+ }
432
298
  }
433
- }
434
- exports.Service = Service;
299
+ }
300
+ async initPreset(opts) {
301
+ const { presets, plugins } = await this.initPlugin({
302
+ plugin: opts.preset,
303
+ presets: opts.presets,
304
+ plugins: opts.plugins
305
+ });
306
+ opts.presets.unshift(...presets || []);
307
+ opts.plugins.push(...plugins || []);
308
+ }
309
+ async initPlugin(opts) {
310
+ var _a, _b;
311
+ (0, import_assert.default)(!this.plugins[opts.plugin.id], `${opts.plugin.type} ${opts.plugin.id} is already registered by ${(_a = this.plugins[opts.plugin.id]) == null ? void 0 : _a.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
312
+ this.plugins[opts.plugin.id] = opts.plugin;
313
+ const pluginAPI = new import_pluginAPI.PluginAPI({
314
+ plugin: opts.plugin,
315
+ service: this
316
+ });
317
+ pluginAPI.registerPresets = pluginAPI.registerPresets.bind(pluginAPI, opts.presets || []);
318
+ pluginAPI.registerPlugins = pluginAPI.registerPlugins.bind(pluginAPI, opts.plugins);
319
+ const proxyPluginAPI = import_pluginAPI.PluginAPI.proxyPluginAPI({
320
+ service: this,
321
+ pluginAPI,
322
+ serviceProps: [
323
+ "appData",
324
+ "applyPlugins",
325
+ "args",
326
+ "config",
327
+ "cwd",
328
+ "pkg",
329
+ "pkgPath",
330
+ "name",
331
+ "paths",
332
+ "userConfig",
333
+ "env",
334
+ "isPluginEnable"
335
+ ],
336
+ staticProps: {
337
+ ApplyPluginsType: import_types.ApplyPluginsType,
338
+ ConfigChangeType: import_types.ConfigChangeType,
339
+ EnableBy: import_types.EnableBy,
340
+ ServiceStage: import_types.ServiceStage,
341
+ service: this
342
+ }
343
+ });
344
+ let dateStart = new Date();
345
+ let ret = await opts.plugin.apply()(proxyPluginAPI);
346
+ opts.plugin.time.register = new Date().getTime() - dateStart.getTime();
347
+ if (opts.plugin.type === "plugin") {
348
+ (0, import_assert.default)(!ret, `plugin should return nothing`);
349
+ }
350
+ (0, import_assert.default)(!this.keyToPluginMap[opts.plugin.key], `key ${opts.plugin.key} is already registered by ${(_b = this.keyToPluginMap[opts.plugin.key]) == null ? void 0 : _b.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
351
+ this.keyToPluginMap[opts.plugin.key] = opts.plugin;
352
+ if (ret == null ? void 0 : ret.presets) {
353
+ ret.presets = ret.presets.map((preset) => new import_plugin.Plugin({
354
+ path: preset,
355
+ type: import_types.PluginType.preset,
356
+ cwd: this.cwd
357
+ }));
358
+ }
359
+ if (ret == null ? void 0 : ret.plugins) {
360
+ ret.plugins = ret.plugins.map((plugin) => new import_plugin.Plugin({
361
+ path: plugin,
362
+ type: import_types.PluginType.plugin,
363
+ cwd: this.cwd
364
+ }));
365
+ }
366
+ return ret || {};
367
+ }
368
+ isPluginEnable(hook) {
369
+ let plugin;
370
+ if (hook.plugin) {
371
+ plugin = hook.plugin;
372
+ } else {
373
+ plugin = this.keyToPluginMap[hook];
374
+ if (!plugin)
375
+ return false;
376
+ }
377
+ const { id, key, enableBy } = plugin;
378
+ if (this.skipPluginIds.has(id))
379
+ return false;
380
+ if (this.userConfig[key] === false)
381
+ return false;
382
+ if (this.config[key] === false)
383
+ return false;
384
+ if (enableBy === import_types.EnableBy.config) {
385
+ return key in this.userConfig || this.config && key in this.config;
386
+ }
387
+ if (typeof enableBy === "function")
388
+ return enableBy({
389
+ userConfig: this.userConfig,
390
+ config: this.config,
391
+ env: this.env
392
+ });
393
+ return true;
394
+ }
395
+ };
396
+ // Annotate the CommonJS export names for ESM import in node:
397
+ 0 && (module.exports = {
398
+ Service
399
+ });