@luban-cli/cli-plugin-service
Version:
A development runtime environment dependency
93 lines • 4.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const Config = require("webpack-chain");
const cli_highlight_1 = require("cli-highlight");
const chalk_1 = __importDefault(require("chalk"));
const accessWebpackConfigName = ["client", "server"];
class Inspect {
apply(params) {
const { api, args } = params;
api.registerCommand("inspect", {
description: "inspect internal webpack config",
usage: "luban-cli-service inspect [options] [...paths]",
options: {
"--mode": "specify env mode (default: development)",
"--name": "specify webpack config side name (default: client)",
"--config": "specify config file",
"--rule <ruleName>": "inspect a specific module rule",
"--plugin <pluginName>": "inspect a specific plugin",
"--rules": "list all module rule names",
"--plugins": "list all plugin names",
"--verbose": "show full function definitions in output",
},
}, () => {
let name = "client";
if (args.name && accessWebpackConfigName.includes(args.name)) {
name = args.name;
}
const webpackConfig = api.resolveWebpackConfig(name);
if (!webpackConfig) {
throw new Error(`${name} side webpack config unable resolved; command [inspect]`);
}
// paths is webpack config key, eg. entry, plugins and module
const { _: paths } = args;
let res = webpackConfig;
let hasUnnamedRule = false;
if (args.rule) {
res = webpackConfig.module
? webpackConfig.module.rules.find((r) => r.__ruleNames[0] === args.rule) || {}
: {};
}
if (args.plugin) {
res = webpackConfig.plugins
? webpackConfig.plugins.find((p) => (p.__pluginName || p.constructor.name) === args.plugin) || {}
: {};
}
if (args.rules) {
// string[]
res = webpackConfig.module
? webpackConfig.module.rules.map((r) => {
const name = r.__ruleNames
? r.__ruleNames[0]
: "Nameless Rule (*)";
hasUnnamedRule = hasUnnamedRule || !r.__ruleNames;
return name;
})
: [];
}
if (args.plugins) {
// string[]
res = webpackConfig.plugins
? webpackConfig.plugins.map((p) => p.__pluginName || p.constructor.name)
: [];
}
if (paths.length > 1) {
res = {};
paths.forEach((path) => {
res[path] = webpackConfig[path];
});
}
if (paths.length === 1) {
res = webpackConfig[paths[0]];
}
// class `Config` override `Function.toString`
// see https://github.com/neutrinojs/webpack-chain/blob/master/src/Config.js#L47
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
const output = Config.toString(res, { verbose: args.verbose });
console.log(cli_highlight_1.highlight(output, { language: "js" }));
// Log explanation for Nameless Rules
if (hasUnnamedRule) {
console.log(`--- ${chalk_1.default.green("Footnotes")} ---`);
console.log(`*: ${chalk_1.default.green("Nameless Rules")} were added through the ${chalk_1.default.green("configureWebpack()")} API (possibly by a plugin) instead of ${chalk_1.default.green("chainWebpack()")} (recommended).
You can run ${chalk_1.default.green("luban-cli-service inspect")} without any arguments to inspect the full config and read these rules' config.`);
}
});
}
}
exports.default = Inspect;
//# sourceMappingURL=inspect.js.map