UNPKG

12.6 kBJavaScriptView Raw
1/*!
2 * @nuxt/cli v2.12.0 (c) 2016-2020
3
4 * - All the amazing contributors
5 * Released under the MIT License.
6 * Website: https://nuxtjs.org
7*/
8'use strict';
9
10function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
11
12const index = require('./cli-index.js');
13require('path');
14require('@nuxt/config');
15require('exit');
16require('@nuxt/utils');
17require('chalk');
18require('std-env');
19require('wrap-ansi');
20require('boxen');
21const consola = _interopDefault(require('consola'));
22require('minimist');
23require('hable');
24require('fs');
25require('execa');
26const util = _interopDefault(require('util'));
27
28/** `Object#toString` result references. */
29var symbolTag = '[object Symbol]';
30
31/**
32 * Checks if `value` is classified as a `Symbol` primitive or object.
33 *
34 * @static
35 * @memberOf _
36 * @since 4.0.0
37 * @category Lang
38 * @param {*} value The value to check.
39 * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
40 * @example
41 *
42 * _.isSymbol(Symbol.iterator);
43 * // => true
44 *
45 * _.isSymbol('abc');
46 * // => false
47 */
48function isSymbol(value) {
49 return typeof value == 'symbol' ||
50 (index.isObjectLike(value) && index.baseGetTag(value) == symbolTag);
51}
52
53var isSymbol_1 = isSymbol;
54
55/** Used to match property names within property paths. */
56var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
57 reIsPlainProp = /^\w*$/;
58
59/**
60 * Checks if `value` is a property name and not a property path.
61 *
62 * @private
63 * @param {*} value The value to check.
64 * @param {Object} [object] The object to query keys on.
65 * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
66 */
67function isKey(value, object) {
68 if (index.isArray(value)) {
69 return false;
70 }
71 var type = typeof value;
72 if (type == 'number' || type == 'symbol' || type == 'boolean' ||
73 value == null || isSymbol_1(value)) {
74 return true;
75 }
76 return reIsPlainProp.test(value) || !reIsDeepProp.test(value) ||
77 (object != null && value in Object(object));
78}
79
80var _isKey = isKey;
81
82/** Error message constants. */
83var FUNC_ERROR_TEXT = 'Expected a function';
84
85/**
86 * Creates a function that memoizes the result of `func`. If `resolver` is
87 * provided, it determines the cache key for storing the result based on the
88 * arguments provided to the memoized function. By default, the first argument
89 * provided to the memoized function is used as the map cache key. The `func`
90 * is invoked with the `this` binding of the memoized function.
91 *
92 * **Note:** The cache is exposed as the `cache` property on the memoized
93 * function. Its creation may be customized by replacing the `_.memoize.Cache`
94 * constructor with one whose instances implement the
95 * [`Map`](http://ecma-international.org/ecma-262/7.0/#sec-properties-of-the-map-prototype-object)
96 * method interface of `clear`, `delete`, `get`, `has`, and `set`.
97 *
98 * @static
99 * @memberOf _
100 * @since 0.1.0
101 * @category Function
102 * @param {Function} func The function to have its output memoized.
103 * @param {Function} [resolver] The function to resolve the cache key.
104 * @returns {Function} Returns the new memoized function.
105 * @example
106 *
107 * var object = { 'a': 1, 'b': 2 };
108 * var other = { 'c': 3, 'd': 4 };
109 *
110 * var values = _.memoize(_.values);
111 * values(object);
112 * // => [1, 2]
113 *
114 * values(other);
115 * // => [3, 4]
116 *
117 * object.a = 2;
118 * values(object);
119 * // => [1, 2]
120 *
121 * // Modify the result cache.
122 * values.cache.set(object, ['a', 'b']);
123 * values(object);
124 * // => ['a', 'b']
125 *
126 * // Replace `_.memoize.Cache`.
127 * _.memoize.Cache = WeakMap;
128 */
129function memoize(func, resolver) {
130 if (typeof func != 'function' || (resolver != null && typeof resolver != 'function')) {
131 throw new TypeError(FUNC_ERROR_TEXT);
132 }
133 var memoized = function() {
134 var args = arguments,
135 key = resolver ? resolver.apply(this, args) : args[0],
136 cache = memoized.cache;
137
138 if (cache.has(key)) {
139 return cache.get(key);
140 }
141 var result = func.apply(this, args);
142 memoized.cache = cache.set(key, result) || cache;
143 return result;
144 };
145 memoized.cache = new (memoize.Cache || index.MapCache);
146 return memoized;
147}
148
149// Expose `MapCache`.
150memoize.Cache = index.MapCache;
151
152var memoize_1 = memoize;
153
154/** Used as the maximum memoize cache size. */
155var MAX_MEMOIZE_SIZE = 500;
156
157/**
158 * A specialized version of `_.memoize` which clears the memoized function's
159 * cache when it exceeds `MAX_MEMOIZE_SIZE`.
160 *
161 * @private
162 * @param {Function} func The function to have its output memoized.
163 * @returns {Function} Returns the new memoized function.
164 */
165function memoizeCapped(func) {
166 var result = memoize_1(func, function(key) {
167 if (cache.size === MAX_MEMOIZE_SIZE) {
168 cache.clear();
169 }
170 return key;
171 });
172
173 var cache = result.cache;
174 return result;
175}
176
177var _memoizeCapped = memoizeCapped;
178
179/** Used to match property names within property paths. */
180var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
181
182/** Used to match backslashes in property paths. */
183var reEscapeChar = /\\(\\)?/g;
184
185/**
186 * Converts `string` to a property path array.
187 *
188 * @private
189 * @param {string} string The string to convert.
190 * @returns {Array} Returns the property path array.
191 */
192var stringToPath = _memoizeCapped(function(string) {
193 var result = [];
194 if (string.charCodeAt(0) === 46 /* . */) {
195 result.push('');
196 }
197 string.replace(rePropName, function(match, number, quote, subString) {
198 result.push(quote ? subString.replace(reEscapeChar, '$1') : (number || match));
199 });
200 return result;
201});
202
203var _stringToPath = stringToPath;
204
205/**
206 * A specialized version of `_.map` for arrays without support for iteratee
207 * shorthands.
208 *
209 * @private
210 * @param {Array} [array] The array to iterate over.
211 * @param {Function} iteratee The function invoked per iteration.
212 * @returns {Array} Returns the new mapped array.
213 */
214function arrayMap(array, iteratee) {
215 var index = -1,
216 length = array == null ? 0 : array.length,
217 result = Array(length);
218
219 while (++index < length) {
220 result[index] = iteratee(array[index], index, array);
221 }
222 return result;
223}
224
225var _arrayMap = arrayMap;
226
227/** Used as references for various `Number` constants. */
228var INFINITY = 1 / 0;
229
230/** Used to convert symbols to primitives and strings. */
231var symbolProto = index.Symbol ? index.Symbol.prototype : undefined,
232 symbolToString = symbolProto ? symbolProto.toString : undefined;
233
234/**
235 * The base implementation of `_.toString` which doesn't convert nullish
236 * values to empty strings.
237 *
238 * @private
239 * @param {*} value The value to process.
240 * @returns {string} Returns the string.
241 */
242function baseToString(value) {
243 // Exit early for strings to avoid a performance hit in some environments.
244 if (typeof value == 'string') {
245 return value;
246 }
247 if (index.isArray(value)) {
248 // Recursively convert values (susceptible to call stack limits).
249 return _arrayMap(value, baseToString) + '';
250 }
251 if (isSymbol_1(value)) {
252 return symbolToString ? symbolToString.call(value) : '';
253 }
254 var result = (value + '');
255 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
256}
257
258var _baseToString = baseToString;
259
260/**
261 * Converts `value` to a string. An empty string is returned for `null`
262 * and `undefined` values. The sign of `-0` is preserved.
263 *
264 * @static
265 * @memberOf _
266 * @since 4.0.0
267 * @category Lang
268 * @param {*} value The value to convert.
269 * @returns {string} Returns the converted string.
270 * @example
271 *
272 * _.toString(null);
273 * // => ''
274 *
275 * _.toString(-0);
276 * // => '-0'
277 *
278 * _.toString([1, 2, 3]);
279 * // => '1,2,3'
280 */
281function toString(value) {
282 return value == null ? '' : _baseToString(value);
283}
284
285var toString_1 = toString;
286
287/**
288 * Casts `value` to a path array if it's not one.
289 *
290 * @private
291 * @param {*} value The value to inspect.
292 * @param {Object} [object] The object to query keys on.
293 * @returns {Array} Returns the cast property path array.
294 */
295function castPath(value, object) {
296 if (index.isArray(value)) {
297 return value;
298 }
299 return _isKey(value, object) ? [value] : _stringToPath(toString_1(value));
300}
301
302var _castPath = castPath;
303
304/** Used as references for various `Number` constants. */
305var INFINITY$1 = 1 / 0;
306
307/**
308 * Converts `value` to a string key if it's not a string or symbol.
309 *
310 * @private
311 * @param {*} value The value to inspect.
312 * @returns {string|symbol} Returns the key.
313 */
314function toKey(value) {
315 if (typeof value == 'string' || isSymbol_1(value)) {
316 return value;
317 }
318 var result = (value + '');
319 return (result == '0' && (1 / value) == -INFINITY$1) ? '-0' : result;
320}
321
322var _toKey = toKey;
323
324/**
325 * The base implementation of `_.get` without support for default values.
326 *
327 * @private
328 * @param {Object} object The object to query.
329 * @param {Array|string} path The path of the property to get.
330 * @returns {*} Returns the resolved value.
331 */
332function baseGet(object, path) {
333 path = _castPath(path, object);
334
335 var index = 0,
336 length = path.length;
337
338 while (object != null && index < length) {
339 object = object[_toKey(path[index++])];
340 }
341 return (index && index == length) ? object : undefined;
342}
343
344var _baseGet = baseGet;
345
346/**
347 * Gets the value at `path` of `object`. If the resolved value is
348 * `undefined`, the `defaultValue` is returned in its place.
349 *
350 * @static
351 * @memberOf _
352 * @since 3.7.0
353 * @category Object
354 * @param {Object} object The object to query.
355 * @param {Array|string} path The path of the property to get.
356 * @param {*} [defaultValue] The value returned for `undefined` resolved values.
357 * @returns {*} Returns the resolved value.
358 * @example
359 *
360 * var object = { 'a': [{ 'b': { 'c': 3 } }] };
361 *
362 * _.get(object, 'a[0].b.c');
363 * // => 3
364 *
365 * _.get(object, ['a', '0', 'b', 'c']);
366 * // => 3
367 *
368 * _.get(object, 'a.b.c', 'default');
369 * // => 'default'
370 */
371function get(object, path, defaultValue) {
372 var result = object == null ? undefined : _baseGet(object, path);
373 return result === undefined ? defaultValue : result;
374}
375
376var get_1 = get;
377
378const webpack = {
379 name: 'webpack',
380 description: 'Inspect Nuxt webpack config',
381 usage: 'webpack [query...]',
382 options: {
383 ...index.common,
384 name: {
385 alias: 'n',
386 type: 'string',
387 default: 'client',
388 description: 'Webpack bundle name: server, client, modern'
389 },
390 depth: {
391 alias: 'd',
392 type: 'string',
393 default: 2,
394 description: 'Inspection depth'
395 },
396 colors: {
397 type: 'boolean',
398 default: process.stdout.isTTY,
399 description: 'Output with ANSI colors'
400 },
401 dev: {
402 type: 'boolean',
403 default: false,
404 description: 'Inspect development mode webpack config'
405 }
406 },
407 async run (cmd) {
408 const { name } = cmd.argv;
409 const queries = [...cmd.argv._];
410
411 const config = await cmd.getNuxtConfig({ dev: cmd.argv.dev, server: false });
412 const nuxt = await cmd.getNuxt(config);
413 const builder = await cmd.getBuilder(nuxt);
414 const { bundleBuilder } = builder;
415 const webpackConfig = bundleBuilder.getWebpackConfig(name);
416
417 let queryError;
418 const match = queries.reduce((result, query) => {
419 const m = advancedGet(result, query);
420 if (m === undefined) {
421 queryError = query;
422 return result
423 }
424 return m
425 }, webpackConfig);
426
427 const serialized = formatObj(match, {
428 depth: parseInt(cmd.argv.depth),
429 colors: cmd.argv.colors
430 });
431
432 consola.log(serialized + '\n');
433
434 if (serialized.includes('[Object]' )) {
435 consola.info('You can use `--depth` or add more queries to inspect `[Object]` and `[Array]` fields.');
436 }
437
438 if (queryError) {
439 consola.warn(`No match in webpack config for \`${queryError}\``);
440 }
441 }
442};
443
444function advancedGet (obj = {}, query = '') {
445 let result = obj;
446
447 if (!query || !result) {
448 return result
449 }
450
451 const [l, r] = query.split('=');
452
453 if (!Array.isArray(result)) {
454 return typeof result === 'object' ? get_1(result, l) : result
455 }
456
457 result = result.filter((i) => {
458 const v = get_1(i, l);
459
460 if (!v) {
461 return
462 }
463
464 if (
465 (v === r) ||
466 (typeof v.test === 'function' && v.test(r)) ||
467 (typeof v.match === 'function' && v.match(r)) ||
468 (r && r.match(v))
469 ) {
470 return true
471 }
472 });
473
474 if (result.length === 1) {
475 return result[0]
476 }
477
478 return result.length ? result : undefined
479}
480
481function formatObj (obj, formatOptions) {
482 if (!util.formatWithOptions) {
483 return util.format(obj)
484 }
485 return util.formatWithOptions(formatOptions, obj)
486}
487
488exports.default = webpack;