Skip to content

Commit e7df240

Browse files
committed
Add ts import doc
1 parent 6d5a81f commit e7df240

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

README.md

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ const config = {
3434
export default config
3535
```
3636

37+
Optionally, to silent typescript
38+
[warnings](https://github.com/JonasKruckenberg/imagetools/issues/160) on image imports, create a new
39+
file at `src/ambient.d.ts`:
40+
41+
```js
42+
// Squelch warnings of all imports from your image assets dir
43+
declare module '$lib/assets/*' {
44+
const image: Record<string, any>
45+
export default image
46+
}
47+
```
48+
49+
### Under the hood
50+
51+
Local image transformations are delegated to the excellent
52+
[vite-imagetools](https://github.com/JonasKruckenberg/imagetools) with a custom `?run` directive.
53+
This preset generates optimized images with sensible defaults, including a `base64` low-quality
54+
image placeholder.
55+
56+
Invoke the preset with the `?run` query param:
57+
58+
```js
59+
import 'path/to/asset?run`
60+
```
61+
3762
## Usage
3863
3964
Use anywhere in your Svelte app:
@@ -79,10 +104,8 @@ The image component renders into:
79104
80105
#### Change default widths/formats
81106
82-
Local image manipulation is delegated to the excellent
83-
[vite-imagetools](https://github.com/JonasKruckenberg/imagetools) with a custom `?run` directive. By
84-
default, this generates 10 variants of every image - `avif/webp/jpg` formats at `480/1024/1920`
85-
widths; and a `16px webp/base64` low-quality image placeholder (LQIP).
107+
By default, `svelte-img` generates 10 variants of an original full-sized image - in `avif/webp/jpg`
108+
formats at `480/1024/1920` widths; and a `16px webp/base64` low-quality image placeholder (LQIP).
86109
87110
To change this globally, edit your `vite.config.js`:
88111
@@ -113,7 +136,7 @@ Widths/formats can be applied to a particular image. From your `.svelte` file:
113136
```html
114137
<script>
115138
// We override defaults to generate 5 variants instead - `avif/jpg` formats at `768/1024` + LQIP
116-
import src from '$lib/a/cat.jpg?width=640;1024&format=avif;jpg&run'
139+
import src from '$lib/a/cat.jpg?run&width=640;1024&format=avif;jpg'
117140
import Img from '@zerodevx/svelte-img'
118141
</script>
119142
@@ -124,12 +147,12 @@ import Img from '@zerodevx/svelte-img'
124147
125148
By default, LQIPs are 16px in width and set to `cover` the full image dimension. Increase for a
126149
higher quality LQIP at the expense of a larger `base64`, or set to 1px for a dominant single-colour
127-
background. To disable LQIP completely, set `?lqip=0&run`.
150+
background. To disable LQIP completely, set `?run&lqip=0`.
128151
129152
<!-- prettier-ignore -->
130153
```html
131154
<script>
132-
import src from '$lib/a/cat.jpg?lqip=1&run'
155+
import src from '$lib/a/cat.jpg?run&lqip=1'
133156
import Img from '@zerodevx/svelte-img'
134157
</script>
135158
@@ -146,13 +169,29 @@ of transformation directives offered by
146169
<!-- prettier-ignore -->
147170
```html
148171
<script>
149-
import src from '$lib/a/cat.jpg?width=600&height=600&fit=cover&normalize&run'
172+
import src from '$lib/a/cat.jpg?run&width=600&height=600&fit=cover&normalize'
150173
import Img from '@zerodevx/svelte-img'
151174
</script>
152175
153176
<Img {src} alt="cat" />
154177
```
155178
179+
#### Lazy loading
180+
181+
`svelte-img` utilises the browser's native lazy loading capability by setting the `loading="lazy"`
182+
attribute on the rendered `<img>` tag by default. This is supported by
183+
[most modern browsers](https://caniuse.com/loading-lazy-attr). To load an image eagerly instead:
184+
185+
<!-- prettier-ignore -->
186+
```html
187+
<script>
188+
import src from '$lib/a/cat.jpg?run'
189+
import Img from '@zerodevx/svelte-img'
190+
</script>
191+
192+
<Img {src} alt="cat" loading="eager" />
193+
```
194+
156195
#### Remote images from an API
157196

158197
Use the `svelte-img` component on its own by passing an array of image objects into `src` like so:

0 commit comments

Comments
 (0)