1
1
'use strict' ;
2
2
3
- var _typeof = typeof Symbol === " function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol ? "symbol" : typeof obj ; } ;
3
+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
4
4
5
5
var connect = require ( 'connect' ) ,
6
6
fs = require ( 'fs' ) ,
@@ -28,6 +28,8 @@ exports.setOptions = function (optimist) {
28
28
optimist . describe ( 'm' , '加载项目中间件' ) ;
29
29
optimist . alias ( 'l' , 'livereload' ) ;
30
30
optimist . describe ( 'l' , '自动刷新' ) ;
31
+ optimist . alias ( 'a' , 'all' ) ;
32
+ optimist . describe ( 'a' , '整体编译' ) ;
31
33
optimist . alias ( 's' , 'https' ) ;
32
34
optimist . describe ( 's' , '使用https协议' ) ;
33
35
} ;
@@ -39,6 +41,7 @@ exports.run = function (options) {
39
41
proxy = options . x || options . proxy ,
40
42
middlewares = options . m || options . middlewares ,
41
43
isHttps = options . s || options . https ,
44
+ isCompilingAll = options . a || options . all ,
42
45
enableLivereload = options . l || options . livereload ,
43
46
port = options . p || options . port || 80 ;
44
47
@@ -105,6 +108,18 @@ exports.run = function (options) {
105
108
106
109
if ( isGoingToStartServer ) {
107
110
( function ( ) {
111
+
112
+ // 监测配置文件变化
113
+ var watchConfig = function watchConfig ( project , middleware , caches , cacheName ) {
114
+ var projectConfigFilePath = sysPath . resolve ( project . config . _config . cwd , project . configFile ) ;
115
+ fs . watch ( projectConfigFilePath , function ( eventType , filename ) {
116
+ if ( eventType === 'change' ) {
117
+ caches [ cacheName ] = null ;
118
+ UtilFs . deleteFolderRecursive ( project . cachePath ) ;
119
+ }
120
+ } ) ;
121
+ } ;
122
+
108
123
var middlewareCache = { } ,
109
124
promiseCache = { } ;
110
125
@@ -167,80 +182,122 @@ exports.run = function (options) {
167
182
return next ( ) ;
168
183
} ) ;
169
184
185
+ // compiler
170
186
var creatingCompiler = false ;
171
187
app . use ( function ( req , res , next ) {
172
188
var url = req . url ,
173
- keys = url . split ( '/' ) ;
174
-
175
- if ( keys [ 2 ] == 'prd' ) {
176
- var _ret2 = function ( ) {
177
- var compiler = void 0 ,
178
- projectName = keys [ 1 ] ,
179
- projectCwd = sysPath . join ( cwd , projectName ) ,
180
- middleware = middlewareCache [ projectName ] ,
181
- compilerPromise = promiseCache [ projectName ] ;
182
-
183
- req . url = '/' + keys . slice ( 3 ) . join ( '/' ) . replace ( / ( \@ [ \d \w ] + ) ? \. ( j s | c s s ) / , '.$2' ) ;
184
- if ( ! middleware && ! creatingCompiler ) {
185
- var _ret3 = function ( ) {
186
- var project = Manager . getProject ( projectCwd , { cache : false } ) ,
187
- resolve = void 0 ,
188
- reject = void 0 ;
189
-
190
- creatingCompiler = true ;
191
- compilerPromise = promiseCache [ projectName ] = new Promise ( function ( res , rej ) {
192
- resolve = res ; reject = rej ;
193
- } ) ;
189
+ keys = url . split ( '/' ) ,
190
+ compiler = null ,
191
+ projectName = keys [ 1 ] ,
192
+ projectCwd = sysPath . join ( cwd , projectName ) ;
194
193
195
- if ( project . check ( ) ) {
196
- compiler = project . getServerCompiler ( ) ;
194
+ // 处理prd资源
195
+ if ( keys [ 2 ] === 'prd' ) {
196
+ req . url = '/' + keys . slice ( 3 ) . join ( '/' ) . replace ( / ( \@ [ \d \w ] + ) ? \. ( j s | c s s ) / , '.$2' ) ;
197
197
198
- compiler . watch ( { } , function ( err , stats ) {
199
- // compiler complete
200
- middleware = middlewareCache [ projectName ] = webpackDevMiddleware ( compiler , {
201
- quiet : true
202
- } ) ;
198
+ // 只编译所请求的资源
199
+ if ( ! isCompilingAll ) {
200
+ ( function ( ) {
201
+ // 去掉版本号和打头的"/"
202
+ var urlNoVer = req . url . replace ( / @ [ \d \w ] + (? = \. \w + $ ) / , '' ) ;
203
+ urlNoVer = urlNoVer [ 0 ] === '/' ? urlNoVer . slice ( 1 ) : urlNoVer ;
203
204
204
- // 输出server运行中 error/warning 信息
205
- middleware ( req , res , next ) ;
205
+ // 从编译cache中取,map文件不必生成重复middleware
206
+ var middleware = middlewareCache [ urlNoVer . replace ( '.map' , '' ) ] ;
206
207
207
- creatingCompiler = false ;
208
+ if ( ! middleware ) {
209
+ var project = Manager . getProject ( projectCwd , { cache : false } ) ;
208
210
209
- resolve ( middleware ) ;
211
+ if ( project . check ( ) ) {
212
+ compiler = project . getServerCompiler ( function ( config ) {
213
+ var nextConfig = Object . assign ( { } , config ) ;
214
+ Object . keys ( config . entry ) . map ( function ( entryKey ) {
215
+ var entryItem = config . entry [ entryKey ] ;
216
+
217
+ var isRequestingEntry = false ,
218
+ entryPath = '' ;
219
+
220
+ if ( Array . isArray ( entryItem ) ) {
221
+ entryPath = entryItem [ entryItem . length - 1 ] ;
222
+ } else {
223
+ entryPath = entryItem ;
224
+ }
225
+
226
+ var cssReg = new RegExp ( config . entryExtNames . css . join ( '|' ) ) ;
227
+ entryPath = entryPath . replace ( cssReg , '.css' ) ; // 将入口的.scss/.less后缀替换为.css
228
+ isRequestingEntry = entryPath . indexOf ( urlNoVer ) > - 1 ;
229
+
230
+ if ( isRequestingEntry ) {
231
+ nextConfig . entry = _defineProperty ( { } , entryKey , entryItem ) ;
232
+ }
233
+ } ) ;
234
+ return nextConfig ;
210
235
} ) ;
211
236
212
- // 检测config文件变化
213
- var projectConfigFilePath = sysPath . resolve ( project . config . _config . cwd , project . configFile ) ;
214
- fs . watch ( projectConfigFilePath , function ( eventType , filename ) {
215
- if ( eventType === 'change' ) {
216
- middlewareCache [ projectName ] = null ;
217
- UtilFs . deleteFolderRecursive ( project . cachePath ) ;
218
- }
237
+ compiler . watch ( { } , function ( err , stats ) {
238
+ // compiler complete
239
+ middleware = middlewareCache [ urlNoVer ] = webpackDevMiddleware ( compiler , { quiet : true } ) ;
240
+ middleware ( req , res , next ) ;
219
241
} ) ;
242
+ // 检测config文件变化
243
+ watchConfig ( project , middleware , middlewareCache , urlNoVer ) ;
220
244
} else {
221
245
next ( ) ;
222
- return {
223
- v : {
224
- v : void 0
225
- }
226
- } ;
227
246
}
228
- } ( ) ;
229
-
230
- if ( ( typeof _ret3 === 'undefined' ? 'undefined' : _typeof ( _ret3 ) ) === "object" ) return _ret3 . v ;
231
- } else {
232
- if ( compilerPromise ) {
233
- compilerPromise . then ( function ( middleware ) {
234
- middleware ( req , res , next ) ;
235
- } ) ;
236
247
} else {
237
- next ( ) ;
248
+ middleware ( req , res , next ) ;
238
249
}
239
- }
240
- } ( ) ;
250
+ } ) ( ) ;
251
+ } else {
252
+ ( function ( ) {
253
+ // 一次编译全部资源
254
+ var middleware = middlewareCache [ projectName ] ,
255
+ compilerPromise = promiseCache [ projectName ] ;
256
+
257
+ if ( ! middleware && ! creatingCompiler ) {
258
+ ( function ( ) {
259
+ var project = Manager . getProject ( projectCwd , { cache : false } ) ,
260
+ resolve = void 0 ,
261
+ reject = void 0 ;
262
+
263
+ creatingCompiler = true ;
264
+ compilerPromise = promiseCache [ projectName ] = new Promise ( function ( res , rej ) {
265
+ resolve = res ; reject = rej ;
266
+ } ) ;
267
+
268
+ if ( project . check ( ) ) {
269
+ compiler = project . getServerCompiler ( function ( config ) {
270
+ return config ;
271
+ } ) ;
272
+
273
+ compiler . watch ( { } , function ( err , stats ) {
274
+ // compiler complete
275
+ middleware = middlewareCache [ projectName ] = webpackDevMiddleware ( compiler , { quiet : true } ) ;
241
276
242
- if ( ( typeof _ret2 === 'undefined' ? 'undefined' : _typeof ( _ret2 ) ) === "object" ) return _ret2 . v ;
277
+ // 输出server运行中 error/warning 信息
278
+ creatingCompiler = false ;
279
+ middleware ( req , res , next ) ;
280
+ resolve ( middleware ) ;
281
+ } ) ;
282
+ // 检测config文件变化
283
+ watchConfig ( project , middleware , middlewareCache , projectName ) ;
284
+ } else {
285
+ next ( ) ;
286
+ }
287
+ } ) ( ) ;
288
+ } else {
289
+ if ( compilerPromise ) {
290
+ compilerPromise . then ( function ( middleware ) {
291
+ middleware ( req , res , next ) ;
292
+ } ) ;
293
+ } else {
294
+ next ( ) ;
295
+ }
296
+ }
297
+ } ) ( ) ;
298
+ }
243
299
} else {
300
+ // 非prd资源不做处理
244
301
next ( ) ;
245
302
}
246
303
} ) ;
0 commit comments