UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

49 lines 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const config_1 = require("../config"); const constants_1 = require("../constants"); /** * 提供插件配置能力 * - 提供 userConfig.plugins 配置校验 * - 提供 `--plugins` 命令行支持 * - 提供 `${CLI_NAME}_PLUGINS` 环境变量支持,需开启 env 支持,如 TAKIN_PLUGINS=plugin1,plugin2 */ class PluginConfigPlugin { constructor(options = { loadCliOrEnvPlugins: false, registerPluginSchema: true }) { this.options = options; this.name = 'TakinPluginConfigPlugin'; } apply(runner) { var _a, _b; runner.hooks.cli.tap(this.name, function (cli) { cli.option('--plugins <plugins>', '指定需要运行的插件, 如: plugin1,plugin2'); }); // 载入命令行或环境变量中指定的插件 // 优先级:命令行 > 环境变量 if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.loadCliOrEnvPlugins) { runner.hooks.loadConfig.tap(this.name, (command) => { var _a, _b, _c, _d; const cliName = ((_c = (_b = (_a = runner.config) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.replace) === null || _c === void 0 ? void 0 : _c.call(_b, /[^a-zA-Z_]/g, '_')) || ''; const ENV_PLUGINS = cliName ? `${cliName.toUpperCase()}_PLUGINS` : ''; let pluginNames = (_d = command === null || command === void 0 ? void 0 : command.options) === null || _d === void 0 ? void 0 : _d.plugins; if (!pluginNames && ENV_PLUGINS) { pluginNames = runner.config.env.get(ENV_PLUGINS); } if (!pluginNames || typeof pluginNames !== 'string') return; const plugins = runner.config.env.arrayify(pluginNames); runner.config.usePlugins(plugins, config_1.PluginTypes.cli); }); } // 注册用户配置 plugins 校验 if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.registerPluginSchema) { runner.hooks.registerUserConfig.tap(this.name, function (schema) { return schema.extend({ plugins: constants_1.UserConfigSchema.plugins }); }); } } } exports.default = PluginConfigPlugin; //# sourceMappingURL=PluginConfigPlugin.js.map