@@ -37,51 +37,41 @@ All of the image transformations are powered by [sharp](https://sharp.pixelplumb
37
37
38
38
Add the plugin to your vite config:
39
39
``` js
40
- import { imagetools } from ' vite-imagetools@next '
40
+ import { imagetools } from ' vite-imagetools'
41
41
42
42
export default defineConfig ({
43
43
plugins: [
44
44
imagetools ()
45
45
]
46
46
})
47
47
```
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:
54
49
``` 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" >
56
55
```
57
56
58
- The most common directives also allow you to use ** shorthands ** .< br >
57
+ You can also generate multiple images by adding more values:
59
58
``` 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'
64
60
```
61
+ This will now generate an array of 3 images with width 200, 300 and 400.
65
62
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" >
69
67
```
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'
81
72
```
82
73
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!
85
75
86
76
## Install
87
77
@@ -188,10 +178,67 @@ To take full advantage of this you want to configure your CI provider to persist
188
178
189
179
## Import directives
190
180
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
+ ```
192
237
193
238
### Custom import directives
194
239
240
+ _ TODO_
241
+
195
242
## Output formats
196
243
197
244
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}>
237
284
238
285
### srcset
239
286
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.
241
297
242
298
### Custom output formats
243
299
0 commit comments