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
+
1
80
module . exports = ( api , options ) => {
2
81
api . registerCommand ( 'inspect' , {
3
82
description : 'inspect internal webpack config' ,
@@ -7,6 +86,8 @@ module.exports = (api, options) => {
7
86
'--verbose' : 'show full function definitions in output'
8
87
}
9
88
} , args => {
89
+ patchWebpackChain ( )
90
+
10
91
const get = require ( 'get-value' )
11
92
const stringify = require ( 'javascript-stringify' )
12
93
const config = api . resolveWebpackConfig ( )
@@ -26,17 +107,49 @@ module.exports = (api, options) => {
26
107
27
108
const pluginRE = / (?: f u n c t i o n | c l a s s ) ( \w + P l u g i n ) /
28
109
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` ]
32
128
}
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
+ } )
38
140
}
39
141
}
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
+
40
153
return stringify ( value )
41
154
} , 2 ) )
42
155
} )
0 commit comments