Skip to content

Commit ae0b1e5

Browse files
committed
export getFilenameFromUrl
1 parent e1ce70e commit ae0b1e5

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

middleware.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,35 +100,42 @@ module.exports = function(compiler, options) {
100100
}
101101
}
102102

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
106109
var localPrefix = options.publicPath || "/";
107110
if(/^https?:\/\//.test(localPrefix)) {
108111
localPrefix = "/" + localPrefix.replace(/^https?:\/\/[^\/]+\//, "");
109112
}
110113
// fast exit if another directory requested
111-
if(req.url.indexOf(localPrefix) != 0) return next();
114+
if(url.indexOf(localPrefix) != 0) return next();
112115
// 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);
114123
// in lazy mode, rebuild on bundle request
115-
if(options.lazy && filename === options.filename)
124+
if(options.lazy && filename === pathJoin(compiler.outputPath, options.filename))
116125
rebuild();
117126
// delay the request until we have a vaild bundle
118127
ready(function() {
119-
// check if it is a generated file
120-
var fsPath = compiler.outputPath == "/" ? "/" + filename : (compiler.outputPath||"") + "/" + filename;
121128
try {
122-
var stat = fs.statSync(fsPath);
129+
var stat = fs.statSync(filename);
123130
if(!stat.isFile()) throw "next";
124131
} catch(e) {
125132
return next();
126133
}
127134

128135
// server content
129-
var content = fs.readFileSync(fsPath);
136+
var content = fs.readFileSync(filename);
130137
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));
132139
if(options.headers) {
133140
for(var name in options.headers) {
134141
res.setHeader(name, options.headers[name]);
@@ -137,12 +144,14 @@ module.exports = function(compiler, options) {
137144
res.end(content);
138145
}, req);
139146
}
140-
147+
148+
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl;
149+
141150
webpackDevMiddleware.invalidate = function() {
142151
if(watching) watching.invalidate();
143152
};
144-
153+
145154
webpackDevMiddleware.fileSystem = fs;
146-
155+
147156
return webpackDevMiddleware;
148157
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-dev-middleware",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"author": "Tobias Koppers @sokra",
55
"description": "Offers a dev middleware for webpack, which arguments a live bundle to a directory",
66
"peerDependencies": {

0 commit comments

Comments
 (0)