diff --git a/lib/GetFilenameFromUrl.js b/lib/GetFilenameFromUrl.js index 5c6527a95..d74297972 100644 --- a/lib/GetFilenameFromUrl.js +++ b/lib/GetFilenameFromUrl.js @@ -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); +}; diff --git a/middleware.js b/middleware.js index dedcb1996..cdb089f86 100644 --- a/middleware.js +++ b/middleware.js @@ -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(); @@ -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; diff --git a/test/GetFilenameFromUrl.test.js b/test/GetFilenameFromUrl.test.js index d38b56e4c..173061a2a 100644 --- a/test/GetFilenameFromUrl.test.js +++ b/test/GetFilenameFromUrl.test.js @@ -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); } @@ -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: "/", @@ -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); }); diff --git a/test/Server.test.js b/test/Server.test.js index c4aecfbb3..07039afa8 100644 --- a/test/Server.test.js +++ b/test/Server.test.js @@ -153,7 +153,7 @@ describe("Server", function() { var instance = middleware(compiler, { stats: "errors-only", quiet: true, - publicPath: "/", + publicPath: "/" }); app.use(instance); listen = listenShorthand(done); @@ -161,9 +161,9 @@ describe("Server", function() { 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); }); }); diff --git a/test/fixtures/server-test/webpack.array.config.js b/test/fixtures/server-test/webpack.array.config.js index 59f2ca27b..d6e7db06e 100644 --- a/test/fixtures/server-test/webpack.array.config.js +++ b/test/fixtures/server-test/webpack.array.config.js @@ -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/" } }];