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-
801module . exports = ( api , options ) => {
812 api . registerCommand ( 'inspect' , {
823 description : 'inspect internal webpack config' ,
@@ -90,20 +11,18 @@ module.exports = (api, options) => {
9011 '--verbose' : 'show full function definitions in output'
9112 }
9213 } , args => {
93- patchWebpackChain ( )
94-
9514 const get = require ( 'get-value' )
96- const stringify = require ( 'javascript-stringify ' )
15+ const { toString } = require ( 'webpack-chain ' )
9716 const config = api . resolveWebpackConfig ( )
98- const paths = args . _
17+ const { _ : paths , verbose } = args
9918
10019 let res
10120 if ( args . rule ) {
102- res = config . module . rules . find ( r => r . __ruleName === args . rule )
21+ res = config . module . rules . find ( r => r . __ruleNames [ 0 ] === args . rule )
10322 } else if ( args . plugin ) {
10423 res = config . plugins . find ( p => p . __pluginName === args . plugin )
10524 } else if ( args . rules ) {
106- res = config . module . rules . map ( r => r . __ruleName )
25+ res = config . module . rules . map ( r => r . __ruleNames [ 0 ] )
10726 } else if ( args . plugins ) {
10827 res = config . plugins . map ( p => p . __pluginName )
10928 } else if ( paths . length > 1 ) {
@@ -117,54 +36,7 @@ module.exports = (api, options) => {
11736 res = config
11837 }
11938
120- const pluginRE = / (?: f u n c t i o n | c l a s s ) ( \w + P l u g i n ) /
121- const output = stringify ( res , ( value , indent , stringify ) => {
122- // shorten long functions
123- if (
124- ! args . verbose &&
125- typeof value === 'function' &&
126- value . toString ( ) . length > 100
127- ) {
128- return `function () { /* omitted long function */ }`
129- }
130-
131- // improve plugin output
132- if ( value && value . __pluginName ) {
133- let match = (
134- value . constructor &&
135- value . constructor . toString ( ) . match ( pluginRE )
136- )
137- // copy-webpack-plugin
138- if ( value . __pluginName === 'copy' ) {
139- match = [ null , `CopyWebpackPlugin` ]
140- }
141- const name = match [ 1 ]
142- const prefix = `/* config.plugin('${ value . __pluginName } ') */\n`
143-
144- if ( name ) {
145- return prefix + `new ${ name } (${
146- value . __pluginArgs . map ( arg => stringify ( arg ) ) . join ( ',\n' )
147- } )`
148- } else {
149- return prefix + stringify ( {
150- args : value . __pluginArgs || [ ]
151- } )
152- }
153- }
154-
155- // improve rule output
156- if ( value && value . __ruleName ) {
157- const prefix = `/* config.module.rule('${ value . __ruleName } ')${
158- value . __oneOfName ? `.oneOf('${ value . __oneOfName } ')` : ``
159- } ${
160- value . __useName ? `.use('${ value . __useName } ')` : ``
161- } */\n`
162- return prefix + stringify ( value )
163- }
164-
165- return stringify ( value )
166- } , 2 )
167-
39+ const output = toString ( config , { verbose } )
16840 console . log ( output )
16941 } )
17042}
0 commit comments