Skip to content

Commit

Permalink
Fix getPaths for publicPaths that are URLs (#255) (#257)
Browse files Browse the repository at this point in the history
* Fix getPaths for publicPaths that are URLs (#255)

* Coding-style changes for for #255

* Use indexOf instead of startsWith for performance reasons
  • Loading branch information
mwhansen authored and shellscape committed Feb 9, 2018
1 parent fed8046 commit 6489fa1
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/util.js
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions test/tests/util.js
Expand Up @@ -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: [
Expand All @@ -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: [
Expand All @@ -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: [
Expand All @@ -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: [
Expand All @@ -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: [
Expand All @@ -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: [
Expand All @@ -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'
}
];

Expand Down

0 comments on commit 6489fa1

Please sign in to comment.