From 67a6d6e43b76da7ae5e41a700fa479e797e4204f Mon Sep 17 00:00:00 2001 From: Josh Spinney Date: Thu, 27 Apr 2023 04:17:21 -0700 Subject: [PATCH] Only add default directives if `run` is present (#13) --- src/lib/vite.js | 2 +- src/routes/test/+page.svelte | 4 +++- tests/test.js | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/vite.js b/src/lib/vite.js index cafb238..b809830 100644 --- a/src/lib/vite.js +++ b/src/lib/vite.js @@ -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, diff --git a/src/routes/test/+page.svelte b/src/routes/test/+page.svelte index 4389e74..715dd59 100644 --- a/src/routes/test/+page.svelte +++ b/src/routes/test/+page.svelte @@ -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) } ] diff --git a/tests/test.js b/tests/test.js index b8a6f64..41c7546 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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`) +}) \ No newline at end of file