|
| 1 | +'use strict'; |
| 2 | +module.exports = function inspectCommand(api, opts) { |
| 3 | + |
| 4 | + const chalk = require('chalk'); |
| 5 | + const _ = require('lodash'); |
| 6 | + |
| 7 | + const logger = api.logger; |
| 8 | + |
| 9 | + api.registerCommand('inspect', { |
| 10 | + description: 'inspect internal webpack config', |
| 11 | + usage: 'micro-app inspect [options] [...paths]', |
| 12 | + options: { |
| 13 | + '--mode': 'specify env mode (default: development)', |
| 14 | + '--rule <ruleName>': 'inspect a specific module rule', |
| 15 | + '--plugin <pluginName>': 'inspect a specific plugin', |
| 16 | + '--rules': 'list all module rule names', |
| 17 | + '--plugins': 'list all plugin names', |
| 18 | + '--verbose': 'show full function definitions in output', |
| 19 | + '--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].', |
| 20 | + '--open-soft-link': '启用开发软链接', |
| 21 | + '--open-disabled-entry': '支持可配置禁用部分模块入口.', |
| 22 | + }, |
| 23 | + details: ` |
| 24 | +Examples: |
| 25 | + ${chalk.gray('# vusion')} |
| 26 | + micro-app inspect --type vusion |
| 27 | + ${chalk.gray('# open soft link')} |
| 28 | + micro-app inspect --type vusion --open-soft-link |
| 29 | + `.trim(), |
| 30 | + }, |
| 31 | + args => { |
| 32 | + const { toString } = require('webpack-chain'); |
| 33 | + const { highlight } = require('cli-highlight'); |
| 34 | + |
| 35 | + const { _: paths, verbose } = args; |
| 36 | + |
| 37 | + let config = api.resolveWebpackConfig(); |
| 38 | + |
| 39 | + const { webpackConfig } = api.applyPluginHooks('modifyWebpackConfig', { |
| 40 | + args, |
| 41 | + webpackConfig: config, |
| 42 | + }); |
| 43 | + |
| 44 | + if ( |
| 45 | + !_.isPlainObject(webpackConfig) || !webpackConfig |
| 46 | + ) { |
| 47 | + logger.error('[Plugin] modifyWebpackConfig must return { args, webpackConfig }'); |
| 48 | + return process.exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + // 更新一次 |
| 52 | + api.setState('webpackConfig', webpackConfig); |
| 53 | + config = _.cloneDeep(webpackConfig); |
| 54 | + |
| 55 | + let res; |
| 56 | + let hasUnnamedRule; |
| 57 | + if (args.rule) { |
| 58 | + res = config.module.rules.find(r => r.__ruleNames[0] === args.rule); |
| 59 | + } else if (args.plugin) { |
| 60 | + res = Array.isArray(config.plugins) |
| 61 | + ? config.plugins.find(p => p.__pluginName === args.plugin) |
| 62 | + : {}; |
| 63 | + } else if (args.rules) { |
| 64 | + res = config.module && Array.isArray(config.module.rules) |
| 65 | + ? config.module.rules.map(r => { |
| 66 | + const name = r.__ruleNames ? r.__ruleNames[0] : 'Nameless Rule (*)'; |
| 67 | + |
| 68 | + hasUnnamedRule = hasUnnamedRule || !r.__ruleNames; |
| 69 | + |
| 70 | + return name; |
| 71 | + }) |
| 72 | + : []; |
| 73 | + } else if (args.plugins) { |
| 74 | + res = Array.isArray(config.plugins) |
| 75 | + ? config.plugins.map(p => p.__pluginName || p.constructor.name) |
| 76 | + : []; |
| 77 | + } else if (paths.length > 1) { |
| 78 | + res = {}; |
| 79 | + paths.forEach(path => { |
| 80 | + res[path] = _.get(config, path); |
| 81 | + }); |
| 82 | + } else if (paths.length === 1) { |
| 83 | + res = _.get(config, paths[0]); |
| 84 | + } else { |
| 85 | + res = config; |
| 86 | + } |
| 87 | + |
| 88 | + const output = toString(res, { verbose }); |
| 89 | + logger.logo(highlight(output, { language: 'js' })); |
| 90 | + |
| 91 | + // Log explanation for Nameless Rules |
| 92 | + if (hasUnnamedRule) { |
| 93 | + logger.logo(`--- ${chalk.green('Footnotes')} ---`); |
| 94 | + logger.logo(`*: ${chalk.green( |
| 95 | + 'Nameless Rules' |
| 96 | + )} were added through the ${chalk.green( |
| 97 | + 'configureWebpack()' |
| 98 | + )} API (possibly by a plugin) instead of ${chalk.green( |
| 99 | + 'chainWebpack()' |
| 100 | + )} (recommended). |
| 101 | + You can run ${chalk.green( |
| 102 | + 'micro-app inspect' |
| 103 | + )} without any arguments to inspect the full config and read these rules' config.`); |
| 104 | + } |
| 105 | + } |
| 106 | + ); |
| 107 | +}; |
0 commit comments