Skip to content

Commit 6489fa1

Browse files
mwhansenshellscape
authored andcommitted
Fix getPaths for publicPaths that are URLs (#255) (#257)
* Fix getPaths for publicPaths that are URLs (#255) * Coding-style changes for for #255 * Use indexOf instead of startsWith for performance reasons
1 parent fed8046 commit 6489fa1

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

lib/util.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,24 @@ function getPaths(publicPath, compiler, url) {
1515
const compilers = compiler && compiler.compilers;
1616
if (Array.isArray(compilers)) {
1717
let compilerPublicPath;
18+
19+
// the path portion of compilerPublicPath
20+
let compilerPublicPathBase;
21+
1822
for (let i = 0; i < compilers.length; i++) {
1923
compilerPublicPath = compilers[i].options
2024
&& compilers[i].options.output
2125
&& compilers[i].options.output.publicPath;
22-
if (url.indexOf(compilerPublicPath) === 0) {
26+
27+
if (compilerPublicPath.indexOf('/') === 0) {
28+
compilerPublicPathBase = compilerPublicPath;
29+
} else {
30+
// handle the case where compilerPublicPath is a URL with hostname
31+
compilerPublicPathBase = parse(compilerPublicPath).pathname;
32+
}
33+
34+
// check the url vs the path part of the compilerPublicPath
35+
if (url.indexOf(compilerPublicPathBase) === 0) {
2336
return {
2437
publicPath: compilerPublicPath,
2538
outputPath: compilers[i].outputPath

test/tests/util.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ describe('GetFilenameFromUrl', () => {
131131
publicPath: '/',
132132
expected: '/foo/sample.js'
133133
},
134+
{
135+
url: '/js/sample.js',
136+
compilers: [
137+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
138+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
139+
],
140+
outputPath: '/root',
141+
publicPath: '/',
142+
expected: '/foo/sample.js'
143+
},
134144
{
135145
url: '/css/sample.css',
136146
compilers: [
@@ -141,6 +151,16 @@ describe('GetFilenameFromUrl', () => {
141151
publicPath: '/',
142152
expected: '/bar/sample.css'
143153
},
154+
{
155+
url: '/css/sample.css',
156+
compilers: [
157+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
158+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
159+
],
160+
outputPath: '/root',
161+
publicPath: '/',
162+
expected: '/bar/sample.css'
163+
},
144164
{
145165
url: '/other/sample.txt',
146166
compilers: [
@@ -151,6 +171,16 @@ describe('GetFilenameFromUrl', () => {
151171
publicPath: '/',
152172
expected: '/root/other/sample.txt'
153173
},
174+
{
175+
url: '/other/sample.txt',
176+
compilers: [
177+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
178+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
179+
],
180+
outputPath: '/root',
181+
publicPath: '/',
182+
expected: '/root/other/sample.txt'
183+
},
154184
{
155185
url: '/js/sample.js',
156186
compilers: [
@@ -161,6 +191,16 @@ describe('GetFilenameFromUrl', () => {
161191
publicPath: '/test/',
162192
expected: '/foo/sample.js'
163193
},
194+
{
195+
url: '/js/sample.js',
196+
compilers: [
197+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
198+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
199+
],
200+
outputPath: '/root',
201+
publicPath: '/test/',
202+
expected: '/foo/sample.js'
203+
},
164204
{
165205
url: '/css/sample.css',
166206
compilers: [
@@ -171,6 +211,16 @@ describe('GetFilenameFromUrl', () => {
171211
publicPath: '/test/',
172212
expected: '/bar/sample.css'
173213
},
214+
{
215+
url: '/css/sample.css',
216+
compilers: [
217+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
218+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
219+
],
220+
outputPath: '/root',
221+
publicPath: '/test/',
222+
expected: '/bar/sample.css'
223+
},
174224
{
175225
url: '/other/sample.txt',
176226
compilers: [
@@ -181,6 +231,16 @@ describe('GetFilenameFromUrl', () => {
181231
publicPath: '/test/',
182232
expected: false
183233
},
234+
{
235+
url: '/other/sample.txt',
236+
compilers: [
237+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
238+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
239+
],
240+
outputPath: '/root',
241+
publicPath: '/test/',
242+
expected: false
243+
},
184244
{
185245
url: '/test/sample.txt',
186246
compilers: [
@@ -190,6 +250,16 @@ describe('GetFilenameFromUrl', () => {
190250
outputPath: '/root',
191251
publicPath: '/test/',
192252
expected: '/root/sample.txt'
253+
},
254+
{
255+
url: '/test/sample.txt',
256+
compilers: [
257+
{ outputPath: '/foo', options: { output: { publicPath: 'http://localhost/js/' } } },
258+
{ outputPath: '/bar', options: { output: { publicPath: 'http://localhost/css/' } } }
259+
],
260+
outputPath: '/root',
261+
publicPath: '/test/',
262+
expected: '/root/sample.txt'
193263
}
194264
];
195265

0 commit comments

Comments
 (0)