inspect.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module.exports = (api, options) => {
  2. api.registerCommand(
  3. 'inspect',
  4. {
  5. description: 'inspect internal webpack config',
  6. usage: 'vue-cli-service inspect [options] [...paths]',
  7. options: {
  8. '--mode': 'specify env mode (default: development)',
  9. '--rule <ruleName>': 'inspect a specific module rule',
  10. '--plugin <pluginName>': 'inspect a specific plugin',
  11. '--rules': 'list all module rule names',
  12. '--plugins': 'list all plugin names',
  13. '--verbose': 'show full function definitions in output',
  14. '--skip-plugins': 'comma-separated list of plugin names to skip for this run'
  15. }
  16. },
  17. args => {
  18. const chalk = require('chalk')
  19. const { get } = require('@vue/cli-shared-utils')
  20. const { toString } = require('webpack-chain')
  21. const { highlight } = require('cli-highlight')
  22. const config = api.resolveWebpackConfig()
  23. const { _: paths, verbose } = args
  24. let res
  25. let hasUnnamedRule
  26. if (args.rule) {
  27. res = config.module.rules.find(r => r.__ruleNames[0] === args.rule)
  28. } else if (args.plugin) {
  29. res = config.plugins.find(p => p.__pluginName === args.plugin)
  30. } else if (args.rules) {
  31. res = config.module.rules.map(r => {
  32. const name = r.__ruleNames ? r.__ruleNames[0] : 'Nameless Rule (*)'
  33. hasUnnamedRule = hasUnnamedRule || !r.__ruleNames
  34. return name
  35. })
  36. } else if (args.plugins) {
  37. res = config.plugins.map(p => p.__pluginName || p.constructor.name)
  38. } else if (paths.length > 1) {
  39. res = {}
  40. paths.forEach(path => {
  41. res[path] = get(config, path)
  42. })
  43. } else if (paths.length === 1) {
  44. res = get(config, paths[0])
  45. } else {
  46. res = config
  47. }
  48. const output = toString(res, { verbose })
  49. console.log(highlight(output, { language: 'js' }))
  50. // Log explanation for Nameless Rules
  51. if (hasUnnamedRule) {
  52. console.log(`--- ${chalk.green('Footnotes')} ---`)
  53. console.log(`*: ${chalk.green(
  54. 'Nameless Rules'
  55. )} were added through the ${chalk.green(
  56. 'configureWebpack()'
  57. )} API (possibly by a plugin) instead of ${chalk.green(
  58. 'chainWebpack()'
  59. )} (recommended).
  60. You can run ${chalk.green(
  61. 'vue-cli-service inspect'
  62. )} without any arguments to inspect the full config and read these rules' config.`)
  63. }
  64. }
  65. )
  66. }
  67. module.exports.defaultModes = {
  68. inspect: 'development'
  69. }