1+ var path = require ( "path" ) ;
2+ var resolve = require ( "enhanced-resolve" ) ;
3+
4+ module . exports = function ( optimist , argv , convertOptions ) {
5+
6+ var options = { } ;
7+
8+ function ifArg ( name , fn , init ) {
9+ if ( Array . isArray ( argv [ name ] ) ) {
10+ if ( init ) init ( ) ;
11+ argv [ name ] . forEach ( fn ) ;
12+ } else if ( typeof argv [ name ] != "undefined" ) {
13+ if ( init ) init ( ) ;
14+ fn ( argv [ name ] , - 1 ) ;
15+ }
16+ }
17+
18+ function ifArgPair ( name , fn , init ) {
19+ ifArg ( name , function ( content , idx ) {
20+ var i = content . indexOf ( "=" ) ;
21+ if ( i < 0 ) return fn ( null , content , idx ) ;
22+ else return fn ( content . substr ( 0 , i ) , content . substr ( i + 1 ) , idx ) ;
23+ } , init ) ;
24+ }
25+
26+ function mapArgToBoolean ( name , optionName ) {
27+ ifArg ( name , function ( bool ) {
28+ options [ optionName ] = bool ;
29+ } ) ;
30+ }
31+
32+ function mapArgToPath ( name , optionName ) {
33+ ifArg ( name , function ( str ) {
34+ options [ optionName ] = path . resolve ( str ) ;
35+ } ) ;
36+ }
37+
38+ function loadPlugin ( name ) {
39+ try {
40+ var path = resolve . sync ( process . cwd ( ) , name ) ;
41+ } catch ( e ) {
42+ console . log ( "Cannot resolve plugin " + name + "." ) ;
43+ process . exit ( - 1 ) ;
44+ }
45+ try {
46+ var Plugin = require ( path ) ;
47+ } catch ( e ) {
48+ console . log ( "Cannot load plugin " + name + ". (" + path + ")" ) ;
49+ throw e ;
50+ }
51+ try {
52+ return new Plugin ( ) ;
53+ } catch ( e ) {
54+ console . log ( "Cannot instantiate plugin " + name + ". (" + path + ")" ) ;
55+ throw e ;
56+ }
57+ }
58+
59+ function ensureObject ( parent , name ) {
60+ if ( typeof parent [ name ] != "object" || parent [ name ] === null )
61+ parent [ name ] = { } ;
62+ }
63+
64+ function ensureArray ( parent , name ) {
65+ if ( ! Array . isArray ( parent [ name ] ) )
66+ parent [ name ] = [ ] ;
67+ }
68+
69+ if ( argv . config ) {
70+ options = require ( path . resolve ( argv . config ) ) ;
71+ } else {
72+ try {
73+ options = require ( path . resolve ( "webpack.config.js" ) ) ;
74+ } catch ( e ) {
75+ options = { } ;
76+ }
77+ }
78+ if ( typeof options != "object" || options === null ) {
79+ console . log ( "Config did not export a object." ) ;
80+ process . exit ( - 1 ) ;
81+ }
82+
83+ mapArgToPath ( "context" , "context" ) ;
84+ if ( ! options . context ) options . context = process . cwd ( ) ;
85+
86+ ifArgPair ( "entry" , function ( name , entry ) {
87+ options . entry [ name ] = entry ;
88+ } , function ( ) {
89+ ensureObject ( options , "entry" ) ;
90+ } ) ;
91+
92+ ifArgPair ( "module-bind" , function ( name , binding ) {
93+ if ( name === null ) name = binding ;
94+ options . module . loaders . push ( {
95+ test : new RegExp ( "\\." + name . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, "\\$&" ) + "$" ) ,
96+ loader : binding
97+ } ) ;
98+ } , function ( ) {
99+ ensureObject ( options , "module" ) ;
100+ ensureArray ( options . module , "loaders" ) ;
101+ } ) ;
102+
103+ ifArgPair ( "module-bind-pre" , function ( name , binding ) {
104+ if ( name === null ) name = binding ;
105+ options . module . preLoaders . push ( {
106+ test : new RegExp ( "\\." + name . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, "\\$&" ) + "$" ) ,
107+ loader : binding
108+ } ) ;
109+ } , function ( ) {
110+ ensureObject ( options , "module" ) ;
111+ ensureArray ( options . module , "preLoaders" ) ;
112+ } ) ;
113+
114+ ifArgPair ( "module-bind-post" , function ( name , binding ) {
115+ if ( name === null ) name = binding ;
116+ options . module . postLoaders . push ( {
117+ test : new RegExp ( "\\." + name . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, "\\$&" ) + "$" ) ,
118+ loader : binding
119+ } ) ;
120+ } , function ( ) {
121+ ensureObject ( options , "module" ) ;
122+ ensureArray ( options . module , "postLoaders" ) ;
123+ } ) ;
124+
125+ ifArg ( "output-path" , function ( value ) {
126+ ensureObject ( options , "output" ) ;
127+ options . output . path = value ;
128+ } ) ;
129+
130+ ifArg ( "output-file" , function ( value ) {
131+ ensureObject ( options , "output" ) ;
132+ options . output . filename = value ;
133+ } ) ;
134+
135+ ifArg ( "output-chunk-file" , function ( value ) {
136+ ensureObject ( options , "output" ) ;
137+ options . output . chunkFilename = value ;
138+ } ) ;
139+
140+ ifArg ( "output-named-chunk-file" , function ( value ) {
141+ ensureObject ( options , "output" ) ;
142+ options . output . namedChunkFilename = value ;
143+ } ) ;
144+
145+ ifArg ( "output-public-path" , function ( value ) {
146+ ensureObject ( options , "output" ) ;
147+ options . output . publicPath = value ;
148+ } ) ;
149+
150+ ifArg ( "output-pathinfo" , function ( bool ) {
151+ ensureObject ( options , "output" ) ;
152+ options . output . pathinfo = bool ;
153+ } ) ;
154+
155+ ifArg ( "output-library" , function ( value ) {
156+ ensureObject ( options , "output" ) ;
157+ options . output . library = value ;
158+ } ) ;
159+
160+ ifArg ( "output-library-target" , function ( value ) {
161+ ensureObject ( options , "output" ) ;
162+ options . output . libraryTarget = value ;
163+ } ) ;
164+
165+ mapArgToBoolean ( "console" , "console" ) ;
166+ mapArgToBoolean ( "cache" , "cache" ) ;
167+ mapArgToBoolean ( "watch" , "watch" ) ;
168+
169+ ifArg ( "watch-delay" , function ( value ) {
170+ options . watchDelay = value ;
171+ } ) ;
172+
173+ mapArgToBoolean ( "debug" , "debug" ) ;
174+
175+ ifArg ( "progress" , function ( value ) {
176+ if ( ! value ) return ;
177+ var ProgressPlugin = require ( "../lib/ProgressPlugin" ) ;
178+ ensureArray ( options , "plugins" ) ;
179+ var chars = 0 ;
180+ options . plugins . push ( new ProgressPlugin ( function ( percentage , msg ) {
181+ if ( percentage < 1 ) {
182+ percentage = Math . floor ( percentage * 100 ) ;
183+ msg = percentage + "% " + msg ;
184+ if ( percentage < 100 ) msg = " " + msg ;
185+ if ( percentage < 10 ) msg = " " + msg ;
186+ }
187+ for ( var i = 0 ; i < chars ; i ++ )
188+ process . stderr . write ( "\b" ) ;
189+ process . stderr . write ( msg ) ;
190+ chars = msg . length ;
191+ } ) ) ;
192+ } ) ;
193+
194+ ifArg ( "devtool" , function ( value ) {
195+ options . devtool = value ;
196+ } ) ;
197+
198+ ifArgPair ( "resolve-alias" , function ( name , value ) {
199+ if ( ! name ) throw new Error ( "--resolve-alias <string>=<string>" ) ;
200+ ensureObject ( options , "resolve" ) ;
201+ ensureObject ( options . resolve , "alias" ) ;
202+ options . resolve . alias [ name ] = value ;
203+ } ) ;
204+
205+ ifArgPair ( "resolve-loader-alias" , function ( name , value ) {
206+ if ( ! name ) throw new Error ( "--resolve-loader-alias <string>=<string>" ) ;
207+ ensureObject ( options , "resolveLoader" ) ;
208+ ensureObject ( options . resolveLoader , "alias" ) ;
209+ options . resolveLoader . alias [ name ] = value ;
210+ } ) ;
211+
212+ ifArg ( "optimize-max-chunks" , function ( value ) {
213+ ensureObject ( options , "optimize" ) ;
214+ options . optimize . maxChunks = parseInt ( value , 10 ) ;
215+ } ) ;
216+
217+ ifArg ( "optimize-minimize" , function ( bool ) {
218+ ensureObject ( options , "optimize" ) ;
219+ options . optimize . minimize = bool ;
220+ } ) ;
221+
222+ ifArg ( "plugin" , function ( value ) {
223+ ensureArray ( options , "plugins" ) ;
224+ options . plugins . push ( loadPlugin ( value ) ) ;
225+ } ) ;
226+
227+ mapArgToBoolean ( "bail" , "bail" ) ;
228+
229+ if ( ! options . output || ! options . output . filename ) {
230+ ensureObject ( options , "output" ) ;
231+ if ( convertOptions && convertOptions . outputFilename ) {
232+ options . output . path = path . dirname ( convertOptions . outputFilename ) ;
233+ options . output . filename = path . basename ( convertOptions . outputFilename ) ;
234+ } else if ( argv . _ . length > 0 ) {
235+ options . output . filename = argv . _ . pop ( ) ;
236+ options . output . path = path . dirname ( options . output . filename ) ;
237+ options . output . filename = path . basename ( options . output . filename ) ;
238+ } else {
239+ optimist . showHelp ( ) ;
240+ process . exit ( - 1 ) ;
241+ }
242+ }
243+
244+ if ( argv . _ . length > 1 ) {
245+ ensureObject ( options , "entry" ) ;
246+ argv . _ . forEach ( function ( content ) {
247+ var i = content . indexOf ( "=" ) ;
248+ if ( i < 0 ) throw new Error ( "Each element must be <string>=<string>." ) ;
249+ else return options . entry [ content . substr ( 0 , i ) ] = content . substr ( i + 1 ) ;
250+ } ) ;
251+ } else if ( argv . _ . length == 1 ) {
252+ options . entry = argv . _ [ 0 ] ;
253+ }
254+
255+ return options ;
256+ } ;
0 commit comments