1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
11 |
|
12 | const index = require('./cli-index.js');
|
13 | require('path');
|
14 | require('@nuxt/config');
|
15 | require('exit');
|
16 | require('@nuxt/utils');
|
17 | require('chalk');
|
18 | require('std-env');
|
19 | require('wrap-ansi');
|
20 | require('boxen');
|
21 | const consola = _interopDefault(require('consola'));
|
22 | require('minimist');
|
23 | require('hable');
|
24 | require('fs');
|
25 | require('execa');
|
26 | const util = _interopDefault(require('util'));
|
27 |
|
28 |
|
29 | var symbolTag = '[object Symbol]';
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function isSymbol(value) {
|
49 | return typeof value == 'symbol' ||
|
50 | (index.isObjectLike(value) && index.baseGetTag(value) == symbolTag);
|
51 | }
|
52 |
|
53 | var isSymbol_1 = isSymbol;
|
54 |
|
55 |
|
56 | var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
|
57 | reIsPlainProp = /^\w*$/;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | function 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 |
|
80 | var _isKey = isKey;
|
81 |
|
82 |
|
83 | var FUNC_ERROR_TEXT = 'Expected a function';
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function 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 |
|
150 | memoize.Cache = index.MapCache;
|
151 |
|
152 | var memoize_1 = memoize;
|
153 |
|
154 |
|
155 | var MAX_MEMOIZE_SIZE = 500;
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | function 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 |
|
177 | var _memoizeCapped = memoizeCapped;
|
178 |
|
179 |
|
180 | var rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
|
181 |
|
182 |
|
183 | var reEscapeChar = /\\(\\)?/g;
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | var 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 |
|
203 | var _stringToPath = stringToPath;
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function 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 |
|
225 | var _arrayMap = arrayMap;
|
226 |
|
227 |
|
228 | var INFINITY = 1 / 0;
|
229 |
|
230 |
|
231 | var symbolProto = index.Symbol ? index.Symbol.prototype : undefined,
|
232 | symbolToString = symbolProto ? symbolProto.toString : undefined;
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | function baseToString(value) {
|
243 |
|
244 | if (typeof value == 'string') {
|
245 | return value;
|
246 | }
|
247 | if (index.isArray(value)) {
|
248 |
|
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 |
|
258 | var _baseToString = baseToString;
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | function toString(value) {
|
282 | return value == null ? '' : _baseToString(value);
|
283 | }
|
284 |
|
285 | var toString_1 = toString;
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | function castPath(value, object) {
|
296 | if (index.isArray(value)) {
|
297 | return value;
|
298 | }
|
299 | return _isKey(value, object) ? [value] : _stringToPath(toString_1(value));
|
300 | }
|
301 |
|
302 | var _castPath = castPath;
|
303 |
|
304 |
|
305 | var INFINITY$1 = 1 / 0;
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | function 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 |
|
322 | var _toKey = toKey;
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 | function 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 |
|
344 | var _baseGet = baseGet;
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | function get(object, path, defaultValue) {
|
372 | var result = object == null ? undefined : _baseGet(object, path);
|
373 | return result === undefined ? defaultValue : result;
|
374 | }
|
375 |
|
376 | var get_1 = get;
|
377 |
|
378 | const 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 |
|
444 | function 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 |
|
481 | function formatObj (obj, formatOptions) {
|
482 | if (!util.formatWithOptions) {
|
483 | return util.format(obj)
|
484 | }
|
485 | return util.formatWithOptions(formatOptions, obj)
|
486 | }
|
487 |
|
488 | exports.default = webpack;
|