@@ -9,8 +9,7 @@ let connect = require('connect'),
9
9
serveIndex = require ( 'serve-index' ) ,
10
10
moment = require ( 'moment' ) ,
11
11
child_process = require ( 'child_process' ) ,
12
- requireg = require ( 'requireg' ) ,
13
- webpackDevMiddleware = require ( 'webpack-dev-middleware' ) ;
12
+ requireg = require ( 'requireg' ) ;
14
13
15
14
let Manager = require ( '../modules/manager.js' ) ;
16
15
let UtilFs = require ( '../utils/fs.js' ) ;
@@ -40,8 +39,7 @@ exports.run = (options) => {
40
39
isCompilingAll = options . a || options . all ,
41
40
port = options . p || options . port || 80 ;
42
41
43
- let isWatcherRunning = false ,
44
- middlewareCache = { } ,
42
+ let middlewareCache = { } ,
45
43
promiseCache = { } ,
46
44
watchCacheNames = { } ;
47
45
@@ -64,10 +62,11 @@ exports.run = (options) => {
64
62
res . end = end ;
65
63
res . end ( chunk , encoding ) ;
66
64
const format = '%date %status %method %url (%route%contentLength%time)' ;
67
- const message = parse ( req , res , format ) ;
65
+ const parseResult = parse ( req , res , format ) ;
68
66
return process . nextTick ( ( ) => {
69
- spinner . text = message ;
70
- return spinner . succeed ( ) ;
67
+ spinner . text = parseResult . message ;
68
+ parseResult . status >= 400 ? spinner . fail ( ) : spinner . succeed ( ) ;
69
+ spinner . text = '' ;
71
70
} ) ;
72
71
} ;
73
72
@@ -102,7 +101,11 @@ exports.run = (options) => {
102
101
format = format . replace ( / % r o u t e / g, '\x1b[90m' + ( req . route ? req . route . path + ' ' : '\x1b[31m' ) + '\x1b[0m' ) ;
103
102
format = format . replace ( / % c o n t e n t L e n g t h / g, '\x1b[90m' + contentLength + '\x1b[31m' + '\x1b[0m' ) ;
104
103
format = format . replace ( / % ( d a t e | t i m e ) / g, '\x1b[90m' + ( new Date - req . _startTime ) + 'ms\x1b[0m' ) ;
105
- return format ;
104
+
105
+ return {
106
+ message : format ,
107
+ status : res . statusCode
108
+ } ;
106
109
}
107
110
108
111
return next ( ) ;
@@ -148,18 +151,16 @@ exports.run = (options) => {
148
151
149
152
// 处理prd资源
150
153
if ( keys [ 2 ] === 'prd' ) {
151
- const requestUrl = '/' + keys . slice ( 3 ) . join ( '/' ) . replace ( / ( \@ [ \d \w ] + ) ? \. ( j s | c s s ) / , '.$2' ) ;
154
+ // 去掉 query & 版本号
155
+ const requestUrl = keys . slice ( 3 ) . join ( '/' ) . replace ( / \? [ \w = & ] + $ / , '' ) . replace ( '.map' , '' ) ;
156
+
152
157
// 只编译所请求的资源
153
158
if ( ! isCompilingAll ) {
154
- // 去掉版本号和打头的"/"
155
- let pureSourcePath = requestUrl . replace ( / @ [ \d \w ] + (? = \. \w + $ ) / , '' ) ;
156
- pureSourcePath = pureSourcePath [ 0 ] === '/' ? pureSourcePath . slice ( 1 ) : pureSourcePath ;
157
159
158
- // 去掉url query
159
- pureSourcePath = pureSourcePath . replace ( / \? [ \w = & ] + $ / , '' ) ;
160
+ let requestUrlNoVer = requestUrl . replace ( / @ [ \d \w ] + (? = \. \w + $ ) / , '' ) ;
160
161
161
- // 从编译cache中取,map文件不必生成重复middleware
162
- const cacheId = sysPath . join ( projectName , pureSourcePath . replace ( '.map' , '' ) ) ;
162
+ // 从编译cache中取,map文件不必生成重复middleware TODO
163
+ const cacheId = sysPath . join ( projectName , requestUrlNoVer ) ;
163
164
let middleware = middlewareCache [ cacheId ] ;
164
165
165
166
if ( ! middleware ) {
@@ -168,10 +169,11 @@ exports.run = (options) => {
168
169
if ( project . check ( ) ) {
169
170
compiler = project . getServerCompiler ( function ( config ) {
170
171
let nextConfig = extend ( { } , config ) ;
171
- // entry应该是个空对象, 这样如果没有找到请求对应的entry, 就不会编译全部入口
172
+
173
+ // entry 应该是个空对象, 这样如果没有找到请求对应的 entry, 就不会编译全部入口
172
174
nextConfig . entry = { } ;
173
175
174
- // 将webpack entry设置为当前请求的资源
176
+ // 将 webpack entry 设置为当前请求的资源
175
177
Object . keys ( config . entry ) . map ( ( entryKey ) => {
176
178
const entryItem = config . entry [ entryKey ] ;
177
179
@@ -184,15 +186,22 @@ exports.run = (options) => {
184
186
entryPath = entryItem ;
185
187
}
186
188
189
+ // 将入口的 .scss/.less 后缀替换为.css
187
190
const cssReg = new RegExp ( '\\' + config . entryExtNames . css . join ( '|\\' ) ) ;
188
- entryPath = entryPath . replace ( cssReg , '.css' ) ; // 将入口的.scss/.less后缀替换为.css
191
+ entryPath = entryPath . replace ( cssReg , '.css' ) ;
189
192
190
- // 如果是 ykit 处理过的样式文件
193
+ // 如果是 ykit 处理过的样式文件,将其变为正常的请求路径(../.ykit_cache/main/index.css.js => main/index.css)
191
194
if ( entryPath . indexOf ( '.css.js' ) && entryPath . indexOf ( '.ykit_cache/' ) > 1 ) {
192
195
entryPath = entryPath . split ( '.ykit_cache/' ) [ 1 ] . replace ( '.css.js' , '.css' ) ;
193
196
}
194
197
195
- isRequestingEntry = sysPath . normalize ( entryPath ) === sysPath . normalize ( pureSourcePath ) ;
198
+ // 判断所请求的资源是否在入口配置中
199
+ if ( sysPath . normalize ( entryPath ) === sysPath . normalize ( requestUrl ) ) {
200
+ isRequestingEntry = true ;
201
+ } else if ( sysPath . normalize ( entryPath ) === sysPath . normalize ( requestUrlNoVer ) ) {
202
+ req . url = req . url . replace ( / @ [ \d \w ] + (? = \. \w + $ ) / , '' ) ;
203
+ isRequestingEntry = true ;
204
+ }
196
205
197
206
if ( isRequestingEntry ) {
198
207
nextConfig . entry = {
@@ -207,36 +216,34 @@ exports.run = (options) => {
207
216
return nextConfig ;
208
217
} ) ;
209
218
210
- isWatcherRunning = false ;
211
-
212
219
compiler . watch ( { } , ( err ) => {
213
220
if ( err ) {
214
221
error ( err ) ;
215
222
} else {
223
+ middlewareCache [ cacheId ] = req . url ;
216
224
next ( ) ;
217
225
}
218
226
} ) ;
219
227
220
- // if (!middlewareCache[cacheId]) {
221
- // middleware = middlewareCache[cacheId] = webpackDevMiddleware(compiler, { quiet: true });
222
- // middleware(req, res, next);
223
- // } else {
224
- // next();
225
- // }
226
-
227
228
// 检测config文件变化
228
- // watchConfig(project, middleware, cacheId);
229
+ watchConfig ( project , middleware , cacheId ) ;
229
230
} else {
230
231
next ( ) ;
231
232
}
232
233
} else {
233
- middleware ( req , res , next ) ;
234
+ req . url = middleware ;
235
+ setTimeout ( ( ) => {
236
+ next ( ) ;
237
+ } , 300 ) ;
234
238
}
235
239
} else { // 一次编译全部资源
236
240
let middleware = middlewareCache [ projectName ] ,
237
241
compilerPromise = promiseCache [ projectName ] ,
238
242
project = Manager . getProject ( projectCwd , { cache : false } ) ;
239
243
244
+ // 统一去掉版本号
245
+ req . url = req . url . replace ( / @ [ \d \w ] + (? = \. \w + $ ) / , '' ) ;
246
+
240
247
// 如果entry发生变化,则需要进行重新编译
241
248
const nextEntry = extend ( { } , project . config . _config . entry ) ;
242
249
if ( compilerPromise && compilerPromise . entry ) {
@@ -266,22 +273,23 @@ exports.run = (options) => {
266
273
promiseCache [ projectName ] . entry = extend ( { } , nextEntry ) ;
267
274
268
275
if ( project . check ( ) ) {
269
- compiler = project . getServerCompiler ( function ( config ) {
276
+ compiler = project . getServerCompiler ( ( config ) => {
270
277
config . plugins . push ( require ( '../plugins/progressBarPlugin.js' ) ) ;
278
+ config . plugins . push ( require ( '../plugins/compileInfoPlugin.js' ) ) ;
271
279
return config ;
272
280
} ) ;
273
281
274
- // compiler complete
275
- if ( ! middlewareCache [ projectName ] ) {
276
- middleware = middlewareCache [ projectName ] = webpackDevMiddleware ( compiler , { quiet : true } ) ;
277
-
278
- // 输出server运行中 error/warning 信息
282
+ compiler . watch ( { } , ( err ) => {
279
283
creatingCompiler = false ;
280
- middleware ( req , res , next ) ;
281
- resolve ( middleware ) ;
282
- } else {
283
- next ( ) ;
284
- }
284
+
285
+ if ( err ) {
286
+ error ( err ) ;
287
+ } else {
288
+ middlewareCache [ projectName ] = projectName ;
289
+ resolve ( ) ;
290
+ next ( ) ;
291
+ }
292
+ } ) ;
285
293
286
294
// 检测config文件变化
287
295
watchConfig ( project , middleware , projectName ) ;
@@ -290,8 +298,8 @@ exports.run = (options) => {
290
298
}
291
299
} else {
292
300
if ( compilerPromise ) {
293
- compilerPromise . then ( ( middleware ) => {
294
- middleware ( req , res , next ) ;
301
+ compilerPromise . then ( ( ) => {
302
+ next ( ) ;
295
303
} ) ;
296
304
} else {
297
305
next ( ) ;
@@ -362,12 +370,7 @@ exports.run = (options) => {
362
370
function exitHandler ( ) {
363
371
// cleanup
364
372
proxyProcess && proxyProcess . kill ( 'SIGINT' ) ;
365
-
366
- if ( isWatcherRunning ) {
367
- process . kill ( process . pid , 'SIGKILL' ) ;
368
- } else {
369
- process . exit ( 0 ) ;
370
- }
373
+ process . exit ( 0 ) ;
371
374
}
372
375
373
376
// 监测配置文件变化
0 commit comments