Skip to content

Commit bf9bfe3

Browse files
committed
allows public path to be set as a URL
1 parent 78c46ce commit bf9bfe3

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/utils.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const getOutputAndPublicPath: GetOutputAndPublicPath = (
146146
if (typeof configOutputPath === 'function') {
147147
outputPath = configOutputPath(fileName)
148148
} else {
149-
outputPath = path.join(configOutputPath, fileName)
149+
outputPath = path.posix.join(configOutputPath, fileName)
150150
}
151151
}
152152
let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`
@@ -155,7 +155,15 @@ const getOutputAndPublicPath: GetOutputAndPublicPath = (
155155
if (typeof configPublicPath === 'function') {
156156
publicPath = configPublicPath(fileName)
157157
} else {
158-
publicPath = path.join(configPublicPath, fileName)
158+
// publicPath can be a url or local path
159+
// check if it's a valid url
160+
if (isValidUrl(configPublicPath)) {
161+
const url = new URL(configPublicPath)
162+
url.pathname = path.posix.join(url.pathname, fileName)
163+
publicPath = url.toString()
164+
} else {
165+
publicPath = path.posix.join(configPublicPath, fileName)
166+
}
159167
}
160168
publicPath = JSON.stringify(publicPath)
161169
}
@@ -165,5 +173,12 @@ const getOutputAndPublicPath: GetOutputAndPublicPath = (
165173
publicPath,
166174
}
167175
}
176+
const isValidUrl = (urlString: string) => {
177+
try {
178+
return Boolean(new URL(urlString))
179+
} catch (e) {
180+
return false
181+
}
182+
}
168183

169184
export { parseOptions, getOutputAndPublicPath, createPlaceholder }

test/sharp/build/__snapshots__/test.js.snap

+8-8
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,12 @@ Object {
307307
"images": Array [
308308
Object {
309309
"height": 595,
310-
"path": "foobar/6f6751159d27af0a-513.avif",
310+
"path": "foobar/22e433fe295fe71f-513.avif",
311311
"width": 513,
312312
},
313313
],
314-
"src": "foobar/6f6751159d27af0a-513.avif",
315-
"srcSet": "foobar/6f6751159d27af0a-513.avif 513w",
314+
"src": "foobar/22e433fe295fe71f-513.avif",
315+
"srcSet": "foobar/22e433fe295fe71f-513.avif 513w",
316316
"toString": [Function],
317317
"width": 513,
318318
}
@@ -324,22 +324,22 @@ Object {
324324
"images": Array [
325325
Object {
326326
"height": 450,
327-
"path": "foobar/399c087b36a41dc7-500.avif",
327+
"path": "foobar/714fef5a20dd125d-500.avif",
328328
"width": 500,
329329
},
330330
Object {
331331
"height": 675,
332-
"path": "foobar/2b2c71d7c209afa8-750.avif",
332+
"path": "foobar/52e04abc8f536b03-750.avif",
333333
"width": 750,
334334
},
335335
Object {
336336
"height": 900,
337-
"path": "foobar/41db421bee56e18f-1000.avif",
337+
"path": "foobar/9effc76d2d205f82-1000.avif",
338338
"width": 1000,
339339
},
340340
],
341-
"src": "foobar/41db421bee56e18f-1000.avif",
342-
"srcSet": "foobar/399c087b36a41dc7-500.avif 500w,foobar/2b2c71d7c209afa8-750.avif 750w,foobar/41db421bee56e18f-1000.avif 1000w",
341+
"src": "foobar/9effc76d2d205f82-1000.avif",
342+
"srcSet": "foobar/714fef5a20dd125d-500.avif 500w,foobar/52e04abc8f536b03-750.avif 750w,foobar/9effc76d2d205f82-1000.avif 1000w",
343343
"toString": [Function],
344344
"width": 1000,
345345
}

test/utils.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ describe('Utils package', () => {
1818
expect(outputPath).toBe('dist/img/file.png')
1919
expect(publicPath).toBe('"public/file.png"')
2020
})
21+
22+
it('https:// slashes are kept on public path', () => {
23+
const { outputPath, publicPath } = getOutputAndPublicPath('file.png', {
24+
outputPath: 'dist/img/',
25+
publicPath: 'https://example.com/public/',
26+
})
27+
expect(publicPath).toBe('"https://example.com/public/file.png"')
28+
})
2129
})

0 commit comments

Comments
 (0)