Skip to content

Commit

Permalink
Only add default directives if run is present (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jspn-creative authored Apr 27, 2023
1 parent c118285 commit 67a6d6e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function lqip(cfg, ctx) {

function main(overrides = {}) {
return imagetools({
defaultDirectives: () => new URLSearchParams('width=480;1024;1920&format=avif;webp;jpg'),
defaultDirectives: (url) => url.searchParams.has('run') ? new URLSearchParams('width=480;1024;1920&format=avif;webp;jpg') : new URLSearchParams(''),
extendTransforms: (builtins) => [...builtins, lqip],
extendOutputFormats: (builtinOutputFormats) => ({
...builtinOutputFormats,
Expand Down
4 changes: 3 additions & 1 deletion src/routes/test/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import testBasic from '../cat01.jpg?run'
import testNoLqip from '../cat01.jpg?lqip=0&run'
import testWidthOverride from '../cat01.jpg?width=480;1024&run'
import testDefault from '../cat01.jpg'
const s = (obj = {}) => JSON.stringify(obj)
const tests = [
{ id: 'basic', name: 'default variants are generated', view: s(testBasic) },
{ id: 'nolqip', name: 'no lqip', view: s(testNoLqip) },
{ id: 'width', name: 'width override', view: s(testWidthOverride) }
{ id: 'width', name: 'width override', view: s(testWidthOverride) },
{ id: 'default', name: 'returns a path by default', view: s(testDefault) }
]
</script>

Expand Down
6 changes: 6 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ test('width override', async ({ page }) => {
expect(data.filter((i) => i.width === 480).length).toBe(3)
expect(data.filter((i) => i.width === 1024).length).toBe(3)
})

test('returns a path by default', async ({ page }) => {
await page.goto('/test')
const data = JSON.parse((await page.getByTestId('default').textContent()) || '')
expect(data).toBe(`/src/routes/cat01.jpg`)
})

0 comments on commit 67a6d6e

Please sign in to comment.