@@ -19,6 +19,7 @@ var _keys2 = _interopRequireDefault(_keys);
19
19
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
20
20
21
21
var connect = require ( 'connect' ) ,
22
+ compression = require ( 'compression' ) ,
22
23
fs = require ( 'fs' ) ,
23
24
http = require ( 'http' ) ,
24
25
https = require ( 'https' ) ,
@@ -55,6 +56,8 @@ exports.setOptions = function (optimist) {
55
56
optimist . describe ( 'v' , '显示详细编译信息' ) ;
56
57
optimist . alias ( 'm' , 'middlewares' ) ;
57
58
optimist . describe ( 'm' , '加载项目中间件' ) ;
59
+ optimist . alias ( 'c' , 'compress' ) ;
60
+ optimist . describe ( 'c' , '启用响应压缩' ) ;
58
61
} ;
59
62
60
63
exports . run = function ( options ) {
@@ -65,7 +68,8 @@ exports.run = function (options) {
65
68
hot = options . hot === 'false' ? false : true ,
66
69
middlewares = options . mw || options . middlewares ,
67
70
isHttps = options . s || options . https ,
68
- port = options . p || options . port || 80 ;
71
+ port = options . p || options . port || 80 ,
72
+ useCompress = options . c || options . compress ;
69
73
70
74
var middlewareCache = { } ,
71
75
promiseCache = { } ,
@@ -82,42 +86,21 @@ exports.run = function (options) {
82
86
var usingHotServer = false ;
83
87
var dateFormat = 'HH:mm:ss' ;
84
88
85
- if ( middlewares ) {
86
- middlewares . split ( '|' ) . forEach ( function ( proName ) {
87
- var pro = Manager . getProject ( sysPath . join ( cwd , proName ) ) ;
88
- if ( pro . check ( ) && Array . isArray ( pro . middlewares ) ) {
89
- pro . middlewares . forEach ( function ( mw ) {
90
- return app . use ( mw ) ;
91
- } ) ;
92
- }
93
- } ) ;
94
- }
95
-
96
- // a simple middleware
97
- app . use ( favicon ( sysPath . join ( __dirname , '../../static/imgs/favicon.ico' ) ) ) ;
98
-
99
- // 预处理
100
- app . use ( function ( req , res , next ) {
101
- var extName = sysPath . extname ( req . url ) ;
102
- extName === '.js' && res . setHeader ( 'Content-Type' , 'text/javascript; charset=UTF-8' ) ;
103
- extName === '.css' && res . setHeader ( 'Content-Type' , 'text/css; charset=UTF-8' ) ;
104
- res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
105
- next ( ) ;
106
- } ) ;
107
-
108
89
// logger
109
90
app . use ( function ( req , res , next ) {
110
91
var end = res . end ;
111
92
req . _startTime = new Date ( ) ;
112
93
113
94
res . end = function ( chunk , encoding ) {
114
- res . end = end ;
115
- res . end ( chunk , encoding ) ;
95
+ // res.end = end;
96
+ end . call ( res , chunk , encoding , function ( ) {
97
+ log ( req . url + ' cost ' + ( Date . now ( ) - req . _startTime ) ) ;
98
+ } ) ;
116
99
117
100
var isNotMap = sysPath . extname ( req . url ) !== '.map' ;
118
101
var isNotHotUpdate = req . url . indexOf ( 'hot-update' ) === - 1 ;
119
102
if ( isNotMap && isNotHotUpdate ) {
120
- var format = '%date %status %method %url %contentLength' ;
103
+ var format = '%date %status %method %url %contentLength %cost ' ;
121
104
122
105
var parseResult = parse ( req , res , format ) ;
123
106
return process . nextTick ( function ( ) {
@@ -155,6 +138,7 @@ exports.run = function (options) {
155
138
format = format . replace ( / % u r l / g, decodeURI ( req . originalUrl ) ) ;
156
139
format = format . replace ( / % s t a t u s / g, String ( res . statusCode ) [ statusColor ] ) ;
157
140
format = format . replace ( / % c o n t e n t L e n g t h / g, contentLength . grey ) ;
141
+ format = format . replace ( / % c o s t / g, Date . now ( ) - req . _startTime ) ;
158
142
159
143
return {
160
144
message : format ,
@@ -165,6 +149,34 @@ exports.run = function (options) {
165
149
return next ( ) ;
166
150
} ) ;
167
151
152
+ // use compression
153
+ if ( useCompress ) {
154
+ app . use ( compression ( ) ) ;
155
+ }
156
+
157
+ if ( middlewares ) {
158
+ middlewares . split ( '|' ) . forEach ( function ( proName ) {
159
+ var pro = Manager . getProject ( sysPath . join ( cwd , proName ) ) ;
160
+ if ( pro . check ( ) && Array . isArray ( pro . middlewares ) ) {
161
+ pro . middlewares . forEach ( function ( mw ) {
162
+ return app . use ( mw ) ;
163
+ } ) ;
164
+ }
165
+ } ) ;
166
+ }
167
+
168
+ // a simple middleware
169
+ app . use ( favicon ( sysPath . join ( __dirname , '../../static/imgs/favicon.ico' ) ) ) ;
170
+
171
+ // 预处理
172
+ app . use ( function ( req , res , next ) {
173
+ var extName = sysPath . extname ( req . url ) ;
174
+ extName === '.js' && res . setHeader ( 'Content-Type' , 'text/javascript; charset=UTF-8' ) ;
175
+ extName === '.css' && res . setHeader ( 'Content-Type' , 'text/css; charset=UTF-8' ) ;
176
+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
177
+ next ( ) ;
178
+ } ) ;
179
+
168
180
// 在package.json中配置别名
169
181
// 当按照别名访问的时候转到对应的Project的目录
170
182
app . use ( function ( req , res , next ) {
0 commit comments