Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/GetFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,31 @@ function getFilenameFromUrl(publicPath, outputPath, url) {

}

module.exports = getFilenameFromUrl;
// support for multi-compiler configuration
// see: https://github.com/webpack/webpack-dev-server/issues/641
function getPaths(publicPath, compiler, url) {
var compilers = compiler && compiler.compilers;
if(Array.isArray(compilers)) {
var compilerPublicPath;
for(var i = 0; i < compilers.length; i++) {
compilerPublicPath = compilers[i].options
&& compilers[i].options.output
&& compilers[i].options.output.publicPath;
if(url.indexOf(compilerPublicPath) === 0) {
return {
publicPath: compilerPublicPath,
outputPath: compilers[i].outputPath
};
}
}
}
return {
publicPath: publicPath,
outputPath: compiler.outputPath
};
}

module.exports = function(publicPath, compiler, url) {
var paths = getPaths(publicPath, compiler, url);
return getFilenameFromUrl(paths.publicPath, paths.outputPath, url);
};
4 changes: 2 additions & 2 deletions middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = function(compiler, options) {
return goNext();
}

var filename = getFilenameFromUrl(context.options.publicPath, context.compiler.outputPath, req.url);
var filename = getFilenameFromUrl(context.options.publicPath, context.compiler, req.url);
if(filename === false) return goNext();


Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = function(compiler, options) {
}
}

webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, context.options.publicPath, context.compiler.outputPath);
webpackDevMiddleware.getFilenameFromUrl = getFilenameFromUrl.bind(this, context.options.publicPath, context.compiler);
webpackDevMiddleware.waitUntilValid = shared.waitUntilValid;
webpackDevMiddleware.invalidate = shared.invalidate;
webpackDevMiddleware.close = shared.close;
Expand Down
85 changes: 74 additions & 11 deletions test/GetFilenameFromUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var should = require("should");
var getFilenameFromUrl = require("../lib/GetFilenameFromUrl");

function testUrl(options) {
var url = getFilenameFromUrl(options.publicPath, options.outputPath, options.url);
var url = getFilenameFromUrl(options.publicPath, options, options.url);
should.strictEqual(url, options.expected);
}

Expand Down Expand Up @@ -33,47 +33,47 @@ describe("GetFilenameFromUrl", function() {
url: "/test.html?foo=bar",
outputPath: "/",
publicPath: "/",
expected: "/test.html",
expected: "/test.html"
}, {
url: "/a.js",
outputPath: "/dist",
publicPath: "/",
expected: "/dist/a.js",
expected: "/dist/a.js"
}, {
url: "/b.js",
outputPath: "/",
publicPath: undefined,
expected: "/b.js",
expected: "/b.js"
}, {
url: "/c.js",
outputPath: undefined,
publicPath: undefined,
expected: "/c.js",
expected: "/c.js"
}, {
url: "/more/complex/path.js",
outputPath: "/a",
publicPath: "/",
expected: "/a/more/complex/path.js",
expected: "/a/more/complex/path.js"
}, {
url: "/more/complex/path.js",
outputPath: "/a",
publicPath: "/complex",
expected: false,
expected: false
}, {
url: "c.js",
outputPath: "/dist",
publicPath: "/",
expected: false, // publicPath is not in url, so it should fail
expected: false // publicPath is not in url, so it should fail
}, {
url: "/bar/",
outputPath: "/foo",
publicPath: "/bar/",
expected: "/foo",
expected: "/foo"
}, {
url: "/bar/",
outputPath: "/",
publicPath: "http://localhost/foo/",
expected: false,
expected: false
}, {
url: "http://test.domain/test/sample.js",
outputPath: "/",
Expand All @@ -94,7 +94,70 @@ describe("GetFilenameFromUrl", function() {
outputPath: "/",
publicPath: "/",
expected: "/pathname with spaces.js"
},
}, {
url: "/js/sample.js",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/",
expected: "/foo/sample.js"
}, {
url: "/css/sample.css",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/",
expected: "/bar/sample.css"
}, {
url: "/other/sample.txt",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/",
expected: "/root/other/sample.txt"
}, {
url: "/js/sample.js",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/test/",
expected: "/foo/sample.js"
}, {
url: "/css/sample.css",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/test/",
expected: "/bar/sample.css"
}, {
url: "/other/sample.txt",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/test/",
expected: false
}, {
url: "/test/sample.txt",
compilers: [
{ outputPath: "/foo", options: { output: { publicPath: "/js/" } } },
{ outputPath: "/bar", options: { output: { publicPath: "/css/" } } }
],
outputPath: "/root",
publicPath: "/test/",
expected: "/root/sample.txt"
}
];
results.forEach(testUrl);
});
Expand Down
6 changes: 3 additions & 3 deletions test/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,17 @@ describe("Server", function() {
var instance = middleware(compiler, {
stats: "errors-only",
quiet: true,
publicPath: "/",
publicPath: "/"
});
app.use(instance);
listen = listenShorthand(done);
});
after(close);

it("request to both bundle files", function(done) {
request(app).get("/foo.js")
request(app).get("/js1/foo.js")
.expect(200, function() {
request(app).get("/bar.js")
request(app).get("/js2/bar.js")
.expect(200, done);
});
});
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/server-test/webpack.array.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module.exports = [{
entry: "./foo.js",
output: {
filename: "foo.js",
path: "/"
path: "/js1",
publicPath: "/js1/"
}
}, {
context: __dirname,
entry: "./bar.js",
output: {
filename: "bar.js",
path: "/"
path: "/js2",
publicPath: "/js2/"
}
}];