Skip to content

Commit

Permalink
fix(image): use defu for options and fix default format (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Farnabaz <[email protected]>
Co-authored-by: pooya parsa <[email protected]>
  • Loading branch information
3 people authored Jan 27, 2021
1 parent 0b10c22 commit cf2e9c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { allowList } from 'allowlist'
import defu from 'defu'
import { hasProtocol, joinURL } from 'ufo'
import type { ImageOptions, CreateImageOptions, ResolvedImage, MapToStatic, ImageCTX } from '../types/image'
import { imageMeta } from './utils/meta'
Expand Down Expand Up @@ -95,7 +96,10 @@ function resolveImage (ctx: ImageCTX, input: string, options: ImageOptions): Res
const { provider, defaults } = getProvider(ctx, options.provider || ctx.options.provider)
const preset = getPreset(ctx, options.preset)

const _options = { ...defaults, ...preset, ...options }
const _options: ImageOptions = defu(options, preset, defaults)
_options.modifiers = { ..._options.modifiers }
const expectedFormat = _options.modifiers.format

if (_options.modifiers?.width) {
_options.modifiers.width = parseSize(_options.modifiers.width)
}
Expand All @@ -105,9 +109,7 @@ function resolveImage (ctx: ImageCTX, input: string, options: ImageOptions): Res

const image = provider.getImage(input, _options, ctx)

if (_options.modifiers?.format && !image.format) {
image.format = _options.modifiers.format
}
image.format = image.format || expectedFormat || ''

return image
}
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function generateAlt (src: string = '') {
return src.split(/[?#]/).shift().split('/').pop().split('.').shift()
}

export function parseSize (input = '') {
export function parseSize (input: string | number = '') {
if (typeof input === 'number') {
return input
}
Expand Down

0 comments on commit cf2e9c1

Please sign in to comment.