Skip to content

Commit

Permalink
Query string should do replace instead of add
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Aug 13, 2024
1 parent d8102d5 commit b100fa7
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,21 @@ export default async function loader(this: LoaderContext<LoaderOptions>, content

const preset = options.presets[queryOptions.get('preset') || 'default']

const presetSizes = typeof preset?.sizes === 'number' ? [preset.sizes] : preset?.sizes
const sizes: number[] = [...(presetSizes || [])]
const sizes: number[] = []
const w = queryOptions.get('w')
if (w) {
sizes.push(...new Set(w.split(',').map((size) => Math.min(Number(size), orginalWidth))))
} else {
const presetSizes = typeof preset?.sizes === 'number' ? [preset.sizes] : preset?.sizes
sizes.push(...(presetSizes || []))
}
if (sizes.length === 0) {
sizes.push(orginalWidth)
}
// https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers
sizes.sort((a, b) => a - b)

const presetFormats = typeof preset?.formats === 'string' ? [preset.formats] : preset?.formats
const formats: SupportedOutputTypes[] = [...(presetFormats || [])]
const formats: SupportedOutputTypes[] = []
const formatQuery = queryOptions.get('format')
if (formatQuery) {
for (const format of formatQuery.split(',')) {
Expand All @@ -82,6 +83,9 @@ export default async function loader(this: LoaderContext<LoaderOptions>, content
console.warn(`Unknown format ${format} for ideal image`)
}
}
} else {
const presetFormats = typeof preset?.formats === 'string' ? [preset.formats] : preset?.formats
formats.push(...(presetFormats || []))
}
if (formats.length === 0) {
formats.push('webp')
Expand Down

0 comments on commit b100fa7

Please sign in to comment.