cli-webpack.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*!
  2. * @nuxt/cli v2.15.8 (c) 2016-2021
  3. * Released under the MIT License
  4. * Repository: https://github.com/nuxt/nuxt.js
  5. * Website: https://nuxtjs.org
  6. */
  7. 'use strict';
  8. const util = require('util');
  9. const consola = require('consola');
  10. const lodash = require('lodash');
  11. const index = require('./cli-index.js');
  12. require('@nuxt/utils');
  13. require('@nuxt/config');
  14. require('path');
  15. require('exit');
  16. require('chalk');
  17. require('std-env');
  18. require('wrap-ansi');
  19. require('boxen');
  20. require('minimist');
  21. require('hable');
  22. require('defu');
  23. require('semver');
  24. require('fs');
  25. require('execa');
  26. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  27. const util__default = /*#__PURE__*/_interopDefaultLegacy(util);
  28. const consola__default = /*#__PURE__*/_interopDefaultLegacy(consola);
  29. const webpack = {
  30. name: 'webpack',
  31. description: 'Inspect Nuxt webpack config',
  32. usage: 'webpack [query...]',
  33. options: {
  34. ...index.common,
  35. name: {
  36. alias: 'n',
  37. type: 'string',
  38. default: 'client',
  39. description: 'Webpack bundle name: server, client, modern'
  40. },
  41. depth: {
  42. alias: 'd',
  43. type: 'string',
  44. default: 2,
  45. description: 'Inspection depth'
  46. },
  47. colors: {
  48. type: 'boolean',
  49. default: process.stdout.isTTY,
  50. description: 'Output with ANSI colors'
  51. },
  52. dev: {
  53. type: 'boolean',
  54. default: false,
  55. description: 'Inspect development mode webpack config'
  56. }
  57. },
  58. async run (cmd) {
  59. const { name } = cmd.argv;
  60. const queries = [...cmd.argv._];
  61. const config = await cmd.getNuxtConfig({ dev: cmd.argv.dev, server: false });
  62. const nuxt = await cmd.getNuxt(config);
  63. const builder = await cmd.getBuilder(nuxt);
  64. const { bundleBuilder } = builder;
  65. const webpackConfig = bundleBuilder.getWebpackConfig(name);
  66. let queryError;
  67. const match = queries.reduce((result, query) => {
  68. const m = advancedGet(result, query);
  69. if (m === undefined) {
  70. queryError = query;
  71. return result
  72. }
  73. return m
  74. }, webpackConfig);
  75. const serialized = formatObj(match, {
  76. depth: parseInt(cmd.argv.depth),
  77. colors: cmd.argv.colors
  78. });
  79. consola__default['default'].log(serialized + '\n');
  80. if (serialized.includes('[Object]' )) {
  81. consola__default['default'].info('You can use `--depth` or add more queries to inspect `[Object]` and `[Array]` fields.');
  82. }
  83. if (queryError) {
  84. consola__default['default'].warn(`No match in webpack config for \`${queryError}\``);
  85. }
  86. }
  87. };
  88. function advancedGet (obj = {}, query = '') {
  89. let result = obj;
  90. if (!query || !result) {
  91. return result
  92. }
  93. const [l, r] = query.split('=');
  94. if (!Array.isArray(result)) {
  95. return typeof result === 'object' ? lodash.get(result, l) : result
  96. }
  97. result = result.filter((i) => {
  98. const v = lodash.get(i, l);
  99. if (!v) {
  100. return false
  101. }
  102. if (
  103. (v === r) ||
  104. (typeof v.test === 'function' && v.test(r)) ||
  105. (typeof v.match === 'function' && v.match(r)) ||
  106. (r && r.match(v))
  107. ) {
  108. return true
  109. }
  110. return false
  111. });
  112. if (result.length === 1) {
  113. return result[0]
  114. }
  115. return result.length ? result : undefined
  116. }
  117. function formatObj (obj, formatOptions) {
  118. if (!util__default['default'].formatWithOptions) {
  119. return util__default['default'].format(obj)
  120. }
  121. return util__default['default'].formatWithOptions(formatOptions, obj)
  122. }
  123. exports.default = webpack;