Skip to content

Commit 6f5bdd4

Browse files
docs(README): improve docs
1 parent 8858d85 commit 6f5bdd4

File tree

1 file changed

+87
-31
lines changed

1 file changed

+87
-31
lines changed

README.md

Lines changed: 87 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,41 @@ All of the image transformations are powered by [sharp](https://sharp.pixelplumb
3737

3838
Add the plugin to your vite config:
3939
```js
40-
import { imagetools } from 'vite-imagetools@next'
40+
import { imagetools } from 'vite-imagetools'
4141

4242
export default defineConfig({
4343
plugins: [
4444
imagetools()
4545
]
4646
})
4747
```
48-
49-
You use the query parameters to specify which transformations to apply to your image.
50-
Each query parameter basically corresponds to a _pure function_ under the hood.
51-
We call these parameters **Import Directives** and you can do a lot of powerful stuff with them.
52-
53-
For example the following diretive will turn the _jpg_ image into a _webp_ one and resize it to be 400 pixels wide:
48+
Images can now be imported from javascript:
5449
```js
55-
import Logo from 'example.jpg?format=webp&width=400'
50+
import Image from 'example.jpg?width=200&format=webp'
51+
```
52+
or html(e.g. vue templates):
53+
```html
54+
<img src="example.jpg?width=200&format=webp">
5655
```
5756

58-
The most common directives also allow you to use **shorthands**.<br>
57+
You can also generate multiple images by adding more values:
5958
```js
60-
// the following statement
61-
import logo from 'example.jpg?format=webp'
62-
// can be shortened to
63-
import logo from 'example.jpg?webp'
59+
import Images from 'example.jpg?width=200;300;400'
6460
```
61+
This will now generate an array of 3 images with width 200, 300 and 400.
6562

66-
### Multiple values
67-
68-
Instead of providing a single value to each directive you can give it a comma-separated list of values:
63+
Or choose how you want to import your image:
64+
- **Srcset Output**
65+
```html
66+
<source srcset="example.jpg?width=200;300;500&srcset">
6967
```
70-
example.jpg?format=avif,webp&width=100,200,300
71-
```
72-
This will generate one image for each combination, so the above import statement will result in the following images:
73-
```
74-
example.jpg?format=avif,webp&width=100,200,300
75-
└-> example.avif width=100
76-
└-> example.avif width=200
77-
└-> example.avif width=300
78-
└-> example.webp width=100
79-
└-> example.webp width=200
80-
└-> example.webp width=300
68+
69+
- **Metadata Output**
70+
```js
71+
import { src, width, height, channels } from 'example.jpg?w=200'
8172
```
8273

83-
This functionality is especially useful when generating responsive images, as you can easily generate a list of different widths.
84-
(You can even generate a correct srcset as output! See [output formats](#output-formats) for details.)
74+
> See the sections [Import directives](#import-directives) and [Output formats](#output-formats) for more!
8575
8676
## Install
8777

@@ -188,10 +178,67 @@ To take full advantage of this you want to configure your CI provider to persist
188178
189179
## Import directives
190180

191-
Vite-imagetools provides a lot lot of directives out of the box, see the [big list of directives](./docs/directives.md)
181+
vite-imagetools is a collection of functions, that can transform your image at compile-time, generate multiple versions etc.<br>
182+
We call these functions **Import Directives** and they pack quite a punch!
183+
184+
> See the [big list of directives](./docs/directives.md) to see all directives vite-imagetools has built in.
185+
186+
### Shorthands
187+
188+
The most common directives allow you to use shorthands:
189+
```js
190+
// for example instead of this
191+
import Image from 'example.jpg?format=webp'
192+
// you can simply write
193+
import Image from 'example.jpg?webp'
194+
```
195+
This allows you to keep you import statements short and readable.
196+
197+
### Multiple directives
198+
199+
You can specify any number of directives by chaining them together with the `&` sign:
200+
```js
201+
import Image from 'example.jpg?width=300&height=700&rotate=45&format=webp'
202+
```
203+
The above example will resize the image, rotate it and then output as webp.
204+
205+
> Under normal circumstances directives will be executed from left to right.<br>
206+
> There are a few excpetions however, for example the `format` directive will always be applied last.
207+
208+
### Multiple arguments
209+
210+
The real power of vite-imagetools comes with specifying multiple arguments for the same directive:
211+
212+
```js
213+
import Images from 'example.jpg?width=300;500;700'
214+
```
215+
This import statement will generate 3 images with width 300, 500 and 700.
216+
```
217+
example.jpg?width=300;500;700
218+
└-> example.jpg width: 300
219+
└-> example.jpg width: 500
220+
└-> example.jpg width: 700
221+
```
222+
This is not where is stops however, you can combine multiple directives with multiple arguments for maximum ease!
223+
224+
```js
225+
import Images from 'example.jpg?width=300;400;700&format=avif;webp'
226+
```
227+
This will generate 6 images in total. One for each combination of imput arguments.
228+
```
229+
example.jpg?format=avif,webp&width=100,200,300
230+
└-> example.avif width: 100
231+
└-> example.avif width: 200
232+
└-> example.avif width: 300
233+
└-> example.webp width: 100
234+
└-> example.webp width: 200
235+
└-> example.webp width: 300
236+
```
192237

193238
### Custom import directives
194239

240+
_TODO_
241+
195242
## Output formats
196243

197244
Depending on your use-case just importing the url of the image might not be enough, <br>
@@ -237,7 +284,16 @@ const image = <img src={src}>
237284
238285
### srcset
239286
240-
_TODO_
287+
You can import a fully generated srcset for your images like so:
288+
```html
289+
<source srcset="example.jpg?width=200,300,400&srcset">
290+
```
291+
will compile to:
292+
```html
293+
<source srcset="example.jpg 200w, example.jpg 300w, example.jpg 400w">
294+
```
295+
296+
> This only works for different widths at the moment.
241297
242298
### Custom output formats
243299

0 commit comments

Comments
 (0)