UNPKG

@mini-umi/core

Version:
159 lines (157 loc) 5.05 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; 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 )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/core.ts var core_exports = {}; __export(core_exports, { ApplyPluginsType: () => ApplyPluginsType, Core: () => Core }); module.exports = __toCommonJS(core_exports); var import_plugin = require("./plugin"); var import_pluginAPI = require("./pluginAPI"); var import_tapable = require("@umijs/bundler-utils/compiled/tapable"); var import_config = require("./config/config"); var ApplyPluginsType = /* @__PURE__ */ ((ApplyPluginsType2) => { ApplyPluginsType2["add"] = "add"; ApplyPluginsType2["modify"] = "modify"; ApplyPluginsType2["event"] = "event"; return ApplyPluginsType2; })(ApplyPluginsType || {}); var Core = class { constructor(opts) { this.commands = {}; this.plugins = []; this.hooksByPluginId = {}; this.hooks = {}; this.pluginMethods = {}; this.configManager = void 0; this.userConfig = void 0; this.config = void 0; this.userPlugins = []; this.opts = opts; this.cwd = opts.cwd; this.env = opts.env; this.hooksByPluginId = {}; this.hooks = {}; this.applyPlugins = function(opts2) { const hooks = this.hooks[opts2.key]; if (!hooks) { return opts2.initialValue; } const waterFullHook = new import_tapable.AsyncSeriesWaterfallHook(["memo"]); for (const hook of hooks) { waterFullHook.tapPromise( { name: "tmp" }, async (memo) => { const items = await hook.fn(memo, opts2.args); return typeof memo !== "object" ? items : Array.isArray(memo) ? [...memo, ...items] : { ...memo, ...items }; } ); } return waterFullHook.promise(opts2.initialValue); }; } async run(opts) { const { name, args = {} } = opts; const configManager = new import_config.Config({ cwd: this.cwd, env: this.env, defaultConfigFiles: this.opts.defaultConfigFiles }); this.configManager = configManager; this.userConfig = configManager.getUserConfig().config; let { presets, plugins } = this.opts; presets = [require.resolve("./servicePlugin")].concat( presets || [] ); this.userPlugins = plugins; this.plugins = []; if (presets) { while (presets.length) { await this.initPreset({ preset: new import_plugin.Plugin({ path: presets.shift() }), presets, plugins: this.plugins }); } } this.plugins.push(...this.userPlugins); this.plugins.forEach(async (plugin) => { await this.initPlugin({ plugin: new import_plugin.Plugin({ path: plugin }) }); }); await this.applyPlugins({ key: "onCheck" }); await this.applyPlugins({ key: "onStart" }); this.config = await this.applyPlugins({ key: "modifyConfig", initialValue: { ...this.userConfig }, args: {} }); await this.applyPlugins({ key: "onBuildStart" }); const command = this.commands[name]; await command.fn({ ...args }); } async initPreset(opts) { const { presets = [], plugins = [] } = await this.initPlugin({ plugin: opts.preset, presets: opts.presets, plugins: opts.plugins }); opts.presets.unshift(...presets || []); opts.plugins.push(...plugins || []); } async initPlugin(opts) { const pluginApi = new import_pluginAPI.PluginAPi({ service: this, plugin: opts.plugin }); const proxyPluginAPI = import_pluginAPI.PluginAPi.proxyPluginAPI({ service: this, pluginAPI: pluginApi, serviceProps: [ "appData", "applyPlugins", "args", "cwd", "userConfig", "config" ], staticProps: { ApplyPluginsType, service: this } }); return opts.plugin.apply()(proxyPluginAPI) || {}; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ApplyPluginsType, Core }); //# sourceMappingURL=core.js.map