Skip to content

Commit f6bfb63

Browse files
committedMay 11, 2018
feat(inspect): improve vue inspect output with webpack-chain hints
close #881
1 parent e5d7893 commit f6bfb63

File tree

1 file changed

+121
-8
lines changed

1 file changed

+121
-8
lines changed
 

‎packages/@vue/cli-service/lib/commands/inspect.js

+121-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,82 @@
1+
let patched = false
2+
function patchWebpackChain () {
3+
if (patched) return
4+
5+
const Config = require('webpack-chain')
6+
7+
const toConfig = Config.prototype.toConfig
8+
Config.prototype.toConfig = function () {
9+
const config = toConfig.call(this)
10+
11+
// inject plugin metadata
12+
const { entries: plugins, order: pluginNames } = this.plugins.order()
13+
config.plugins.forEach((p, i) => {
14+
Object.defineProperties(p, {
15+
__pluginName: {
16+
value: pluginNames[i]
17+
},
18+
__pluginArgs: {
19+
value: plugins[pluginNames[i]].get('args')
20+
}
21+
})
22+
})
23+
24+
// inject rule metadata
25+
const { entries: rules, order: ruleNames } = this.module.rules.order()
26+
config.module.rules.forEach((rawRule, i) => {
27+
const ruleName = ruleNames[i]
28+
const rule = rules[ruleName]
29+
Object.defineProperties(rawRule, {
30+
__ruleName: {
31+
value: ruleName
32+
}
33+
})
34+
35+
if (rawRule.oneOf) {
36+
const { entries: oneOfs, order: oneOfNames } = rule.oneOfs.order()
37+
rawRule.oneOf.forEach((o, i) => {
38+
const oneOfName = oneOfNames[i]
39+
const oneOf = oneOfs[oneOfName]
40+
Object.defineProperties(o, {
41+
__ruleName: {
42+
value: ruleName
43+
},
44+
__oneOfName: {
45+
value: oneOfName
46+
}
47+
})
48+
patchUse(oneOf, ruleName, oneOfName, o)
49+
})
50+
}
51+
52+
patchUse(rule, ruleName, null, rawRule)
53+
})
54+
55+
return config
56+
}
57+
58+
patched = true
59+
}
60+
61+
function patchUse (rule, ruleName, oneOfName, rawRule) {
62+
if (Array.isArray(rawRule.use)) {
63+
const { order: useNames } = rule.uses.order()
64+
rawRule.use.forEach((use, i) => {
65+
Object.defineProperties(use, {
66+
__ruleName: {
67+
value: ruleName
68+
},
69+
__oneOfName: {
70+
value: oneOfName
71+
},
72+
__useName: {
73+
value: useNames[i]
74+
}
75+
})
76+
})
77+
}
78+
}
79+
180
module.exports = (api, options) => {
281
api.registerCommand('inspect', {
382
description: 'inspect internal webpack config',
@@ -7,6 +86,8 @@ module.exports = (api, options) => {
786
'--verbose': 'show full function definitions in output'
887
}
988
}, args => {
89+
patchWebpackChain()
90+
1091
const get = require('get-value')
1192
const stringify = require('javascript-stringify')
1293
const config = api.resolveWebpackConfig()
@@ -26,17 +107,49 @@ module.exports = (api, options) => {
26107

27108
const pluginRE = /(?:function|class) (\w+Plugin)/
28109
console.log(stringify(res, (value, indent, stringify) => {
29-
if (!args.verbose) {
30-
if (typeof value === 'function' && value.toString().length > 100) {
31-
return `function () { /* omitted long function */ }`
110+
// shorten long functions
111+
if (
112+
!args.verbose &&
113+
typeof value === 'function' &&
114+
value.toString().length > 100
115+
) {
116+
return `function () { /* omitted long function */ }`
117+
}
118+
119+
// improve plugin output
120+
if (value && value.__pluginName) {
121+
let match = (
122+
value.constructor &&
123+
value.constructor.toString().match(pluginRE)
124+
)
125+
// copy-webpack-plugin
126+
if (value.__pluginName === 'copy') {
127+
match = [null, `CopyWebpackPlugin`]
32128
}
33-
if (value && typeof value.constructor === 'function') {
34-
const match = value.constructor.toString().match(pluginRE)
35-
if (match) {
36-
return `/* ${match[1]} */ ` + stringify(value)
37-
}
129+
const name = match[1]
130+
const prefix = `/* config.plugin('${value.__pluginName}') */\n`
131+
132+
if (name) {
133+
return prefix + `new ${name}(${
134+
value.__pluginArgs.map(arg => stringify(arg)).join(',\n')
135+
})`
136+
} else {
137+
return prefix + stringify({
138+
args: value.__pluginArgs || []
139+
})
38140
}
39141
}
142+
143+
// improve rule output
144+
if (value && value.__ruleName) {
145+
const prefix = `/* config.module.rule('${value.__ruleName}')${
146+
value.__oneOfName ? `.oneOf('${value.__oneOfName}')` : ``
147+
}${
148+
value.__useName ? `.use('${value.__useName}')` : ``
149+
} */\n`
150+
return prefix + stringify(value)
151+
}
152+
40153
return stringify(value)
41154
}, 2))
42155
})

0 commit comments

Comments
 (0)
Please sign in to comment.