@@ -100,35 +100,42 @@ module.exports = function(compiler, options) {
100
100
}
101
101
}
102
102
103
- // The middleware function
104
- function webpackDevMiddleware ( req , res , next ) {
105
- // publicPrefix ist the folder our bundle should be in
103
+ function pathJoin ( a , b ) {
104
+ return a == "/" ? "/" + b : ( a || "" ) + "/" + b
105
+ }
106
+
107
+ function getFilenameFromUrl ( url ) {
108
+ // publicPrefix is the folder our bundle should be in
106
109
var localPrefix = options . publicPath || "/" ;
107
110
if ( / ^ h t t p s ? : \/ \/ / . test ( localPrefix ) ) {
108
111
localPrefix = "/" + localPrefix . replace ( / ^ h t t p s ? : \/ \/ [ ^ \/ ] + \/ / , "" ) ;
109
112
}
110
113
// fast exit if another directory requested
111
- if ( req . url . indexOf ( localPrefix ) != 0 ) return next ( ) ;
114
+ if ( url . indexOf ( localPrefix ) != 0 ) return next ( ) ;
112
115
// get filename from request
113
- var filename = req . url . substr ( localPrefix . length ) ;
116
+ var filename = url . substr ( localPrefix . length ) ;
117
+ return pathJoin ( compiler . outputPath , filename ) ;
118
+ }
119
+
120
+ // The middleware function
121
+ function webpackDevMiddleware ( req , res , next ) {
122
+ var filename = getFilenameFromUrl ( req . url ) ;
114
123
// in lazy mode, rebuild on bundle request
115
- if ( options . lazy && filename === options . filename )
124
+ if ( options . lazy && filename === pathJoin ( compiler . outputPath , options . filename ) )
116
125
rebuild ( ) ;
117
126
// delay the request until we have a vaild bundle
118
127
ready ( function ( ) {
119
- // check if it is a generated file
120
- var fsPath = compiler . outputPath == "/" ? "/" + filename : ( compiler . outputPath || "" ) + "/" + filename ;
121
128
try {
122
- var stat = fs . statSync ( fsPath ) ;
129
+ var stat = fs . statSync ( filename ) ;
123
130
if ( ! stat . isFile ( ) ) throw "next" ;
124
131
} catch ( e ) {
125
132
return next ( ) ;
126
133
}
127
134
128
135
// server content
129
- var content = fs . readFileSync ( fsPath ) ;
136
+ var content = fs . readFileSync ( filename ) ;
130
137
res . setHeader ( "Access-Control-Allow-Origin" , "*" ) ; // To support XHR, etc.
131
- res . setHeader ( "Content-Type" , mime . lookup ( fsPath ) ) ;
138
+ res . setHeader ( "Content-Type" , mime . lookup ( filename ) ) ;
132
139
if ( options . headers ) {
133
140
for ( var name in options . headers ) {
134
141
res . setHeader ( name , options . headers [ name ] ) ;
@@ -137,12 +144,14 @@ module.exports = function(compiler, options) {
137
144
res . end ( content ) ;
138
145
} , req ) ;
139
146
}
140
-
147
+
148
+ webpackDevMiddleware . getFilenameFromUrl = getFilenameFromUrl ;
149
+
141
150
webpackDevMiddleware . invalidate = function ( ) {
142
151
if ( watching ) watching . invalidate ( ) ;
143
152
} ;
144
-
153
+
145
154
webpackDevMiddleware . fileSystem = fs ;
146
-
155
+
147
156
return webpackDevMiddleware ;
148
157
}
0 commit comments