inspect.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, get } = require('@vue/cli-shared-utils')
  19. const { toString } = require('webpack-chain')
  20. const { highlight } = require('cli-highlight')
  21. const config = api.resolveWebpackConfig()
  22. const { _: paths, verbose } = args
  23. let res
  24. let hasUnnamedRule
  25. if (args.rule) {
  26. res = config.module.rules.find(r => r.__ruleNames[0] === args.rule)
  27. } else if (args.plugin) {
  28. res = config.plugins.find(p => p.__pluginName === args.plugin)
  29. } else if (args.rules) {
  30. res = config.module.rules.map(r => {
  31. const name = r.__ruleNames ? r.__ruleNames[0] : 'Nameless Rule (*)'
  32. hasUnnamedRule = hasUnnamedRule || !r.__ruleNames
  33. return name
  34. })
  35. } else if (args.plugins) {
  36. res = config.plugins.map(p => p.__pluginName || p.constructor.name)
  37. } else if (paths.length > 1) {
  38. res = {}
  39. paths.forEach(path => {
  40. res[path] = get(config, path)
  41. })
  42. } else if (paths.length === 1) {
  43. res = get(config, paths[0])
  44. } else {
  45. res = config
  46. }
  47. const output = toString(res, { verbose })
  48. console.log(highlight(output, { language: 'js' }))
  49. // Log explanation for Nameless Rules
  50. if (hasUnnamedRule) {
  51. console.log(`--- ${chalk.green('Footnotes')} ---`)
  52. console.log(`*: ${chalk.green(
  53. 'Nameless Rules'
  54. )} were added through the ${chalk.green(
  55. 'configureWebpack()'
  56. )} API (possibly by a plugin) instead of ${chalk.green(
  57. 'chainWebpack()'
  58. )} (recommended).
  59. You can run ${chalk.green(
  60. 'vue-cli-service inspect'
  61. )} without any arguments to inspect the full config and read these rules' config.`)
  62. }
  63. }
  64. )
  65. }
  66. module.exports.defaultModes = {
  67. inspect: 'development'
  68. }