"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _default(api) {
  const service = api.service;
  api.registerCommand('inspect', {
    webpack: true,
    description: 'inspect internal webpack config',
    usage: 'umi inspect [options]',
    options: {
      '--mode': 'specify env mode (development or production, default is development)',
      '--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'
    }
  }, (args = {}) => {
    const verbose = args.verbose;

    const webpackChain = require('af-webpack/webpack-chain');

    const config = service.webpackConfig;
    let res;

    if (args.rule) {
      res = config.module.rules.find(r => r.__ruleNames[0] === args.rule);
    } else if (args.plugin) {
      res = config.plugins.find(p => p.__pluginName === args.plugin);
    } else if (args.rules) {
      res = config.module.rules.map(r => r.__ruleNames[0]);
    } else if (args.plugins) {
      res = config.plugins.map(p => p.__pluginName || p.constructor.name);
    } else {
      res = config;
    }

    const output = webpackChain.toString(res, {
      verbose
    });
    console.log(output);
  });
}