diff --git a/lib/util.js b/lib/util.js index e26a41b84..02276f252 100644 --- a/lib/util.js +++ b/lib/util.js @@ -15,11 +15,24 @@ function getPaths(publicPath, compiler, url) { const compilers = compiler && compiler.compilers; if (Array.isArray(compilers)) { let compilerPublicPath; + + // the path portion of compilerPublicPath + let compilerPublicPathBase; + for (let i = 0; i < compilers.length; i++) { compilerPublicPath = compilers[i].options && compilers[i].options.output && compilers[i].options.output.publicPath; - if (url.indexOf(compilerPublicPath) === 0) { + + if (compilerPublicPath.indexOf('/') === 0) { + compilerPublicPathBase = compilerPublicPath; + } else { + // handle the case where compilerPublicPath is a URL with hostname + compilerPublicPathBase = parse(compilerPublicPath).pathname; + } + + // check the url vs the path part of the compilerPublicPath + if (url.indexOf(compilerPublicPathBase) === 0) { return { publicPath: compilerPublicPath, outputPath: compilers[i].outputPath diff --git a/test/tests/util.js b/test/tests/util.js index 0edd18e46..02bb19bd0 100644 --- a/test/tests/util.js +++ b/test/tests/util.js @@ -131,6 +131,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/', expected: '/foo/sample.js' }, + { + url: '/js/sample.js', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/', + expected: '/foo/sample.js' + }, { url: '/css/sample.css', compilers: [ @@ -141,6 +151,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/', expected: '/bar/sample.css' }, + { + url: '/css/sample.css', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/', + expected: '/bar/sample.css' + }, { url: '/other/sample.txt', compilers: [ @@ -151,6 +171,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/', expected: '/root/other/sample.txt' }, + { + url: '/other/sample.txt', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/', + expected: '/root/other/sample.txt' + }, { url: '/js/sample.js', compilers: [ @@ -161,6 +191,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/test/', expected: '/foo/sample.js' }, + { + url: '/js/sample.js', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/test/', + expected: '/foo/sample.js' + }, { url: '/css/sample.css', compilers: [ @@ -171,6 +211,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/test/', expected: '/bar/sample.css' }, + { + url: '/css/sample.css', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/test/', + expected: '/bar/sample.css' + }, { url: '/other/sample.txt', compilers: [ @@ -181,6 +231,16 @@ describe('GetFilenameFromUrl', () => { publicPath: '/test/', expected: false }, + { + url: '/other/sample.txt', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/test/', + expected: false + }, { url: '/test/sample.txt', compilers: [ @@ -190,6 +250,16 @@ describe('GetFilenameFromUrl', () => { outputPath: '/root', publicPath: '/test/', expected: '/root/sample.txt' + }, + { + url: '/test/sample.txt', + compilers: [ + { outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } }, + { outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } } + ], + outputPath: '/root', + publicPath: '/test/', + expected: '/root/sample.txt' } ];