Skip to content

Commit e1083a1

Browse files
committed
Add width and height to <img> tags
1 parent 1d68091 commit e1083a1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/lib/SvelteImg.svelte

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<script>
22
export let src = []
33
export let sizes = undefined
4+
export let width = undefined
5+
export let height = undefined
46
export let loading = 'lazy'
57
export let decoding = 'async'
68
export let ref = undefined
79
10+
const priority = ['heic', 'heif', 'avif', 'webp', 'jpeg', 'jpg', 'png', 'gif', 'tiff']
811
let image = {}
912
let sources = []
1013
@@ -18,14 +21,17 @@ $: if (src.length) {
1821
{ list: [], lqip: undefined }
1922
)
2023
const groups = []
21-
for (const format of ['heic', 'heif', 'avif', 'webp', 'jpeg', 'jpg', 'png', 'gif', 'tiff']) {
24+
for (const format of priority) {
2225
const group = list.filter((i) => i.format === format)
2326
if (group.length) {
2427
group.sort((a, b) => a.width - b.width)
28+
const { src, width, height } = group[group.length - 1]
2529
groups.push({
2630
format: format === 'jpg' ? 'jpeg' : format,
2731
srcset: group.reduce((a, c) => [...a, `${c.src} ${c.width}w`], []).join(','),
28-
src: group[group.length - 1].src
32+
src,
33+
width,
34+
height
2935
})
3036
}
3137
}
@@ -44,6 +50,8 @@ $: if (src.length) {
4450
src={image.src}
4551
srcset={image.srcset}
4652
{sizes}
53+
width={width || image.width || undefined}
54+
height={height || image.height || undefined}
4755
{loading}
4856
{decoding}
4957
style:background={image.lqip}

src/lib/vite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function main(overrides = {}) {
2020
extendTransforms: (builtins) => [...builtins, lqip],
2121
extendOutputFormats: (builtinOutputFormats) => ({
2222
...builtinOutputFormats,
23-
run: () => metadataFormat(['format', 'src', 'width', 'base64'])
23+
run: () => metadataFormat(['format', 'src', 'width', 'height', 'base64'])
2424
}),
2525
resolveConfigs: (e, f) => {
2626
if (e.findIndex((i) => i[0] === 'run') > -1) {

0 commit comments

Comments
 (0)