Skip to content

Commit d197094

Browse files
verdelArtyomResh
authored andcommitted
Use ssh key instead of PAT to clone CSSSR/actions
fix(docs): COM-3370 update README, translate comments to english
1 parent 8077640 commit d197094

7 files changed

Lines changed: 271 additions & 305 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
uses: actions/checkout@v2
1515
with:
1616
repository: CSSSR/actions
17-
token: ${{ secrets.DOWNLOAD_ACTIONS_TOKEN }}
17+
ssh-key: ${{ secrets.DOWNLOAD_ACTIONS_SSH_KEY }}
1818
path: actions
1919

2020
- uses: actions/checkout@v2

README.md

Lines changed: 240 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,65 @@ desktop: (min-width: 1280px)
1515
and images structured like that
1616
```
1717
images/
18-
myImage/ <- picture name
18+
myImage/ <-| picture name
1919
mobile.png <-| different image variations,
2020
tablet.png <-| names should be the same as breakpoint names above,
2121
desktop.png <-| images should be in 3x resolution
2222
```
2323
this lib helps you to get
24+
2425
```html
25-
<picture><source media="(max-width: 767px)" type="image/webp" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 3x"><source media="(max-width: 767px)" type="image/png" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 3x"><source media="(min-width: 768px) and (max-width: 1279px)" type="image/webp" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 3x"><source media="(min-width: 768px) and (max-width: 1279px)" type="image/png" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 3x"><source media="(min-width: 1280px)" type="image/webp" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 3x"><source media="(min-width: 1280px)" type="image/png" srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 3x"><img srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 1x, http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 2x, http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 3x" src="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png" alt="Image with different breakpoints"></picture>
26+
<picture>
27+
<source
28+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 1x,
29+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 2x,
30+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@webp 3x"
31+
media="(max-width: 767px)"
32+
type="image/webp"
33+
>
34+
<source
35+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 1x,
36+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 2x,
37+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/mobile-a78dac89.png@png 3x"
38+
media="(max-width: 767px)"
39+
type="image/png"
40+
>
41+
<source
42+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 1x,
43+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 2x,
44+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@webp 3x"
45+
media="(min-width: 768px) and (max-width: 1279px)"
46+
type="image/webp"
47+
>
48+
<source
49+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 1x,
50+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 2x,
51+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/tablet-4dcebf08.png@png 3x"
52+
media="(min-width: 768px) and (max-width: 1279px)"
53+
type="image/png"
54+
>
55+
<source
56+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 1x,
57+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 2x,
58+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@webp 3x"
59+
media="(min-width: 1280px)"
60+
type="image/webp"
61+
>
62+
<source
63+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 1x,
64+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 2x,
65+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 3x"
66+
media="(min-width: 1280px)"
67+
type="image/png"
68+
>
69+
<img
70+
srcset="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 1x,
71+
http://localhost:8080/insecure/dpr:0.6666/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 2x,
72+
http://localhost:8080/insecure/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png 3x"
73+
src="http://localhost:8080/insecure/dpr:0.3333/plain/http://192.168.1.134:8081/build/example/images/differentBreakpoints/desktop-3b835739.png@png"
74+
alt="Image with different breakpoints"
75+
>
76+
</picture>
2677
```
2778

2879

@@ -48,9 +99,15 @@ module.exports = {
4899
test: /\.(png|jpe?g|webp)$/i,
49100
use: [
50101
{
51-
loader: 'image-resolution-loader',
102+
loader: '@csssr/csssr.images',
52103
options: {
53-
// loader options, see below
104+
// @csssr/csssr.images loader options, see below
105+
},
106+
},
107+
// file-loader is required
108+
loader: 'file-loader',
109+
options: {
110+
// file-loader options by your preference
54111
},
55112
},
56113
],
@@ -59,3 +116,182 @@ module.exports = {
59116
},
60117
}
61118
```
119+
120+
Then `import` the target file or `require.context()` target files:
121+
122+
**file.js**
123+
124+
```js
125+
import images from ('./images/foo/boo.png')
126+
```
127+
128+
or
129+
130+
```js
131+
const images = require.context('./images/foo')
132+
```
133+
134+
The loader will accept your image as the initial and will generate optimized version. Next, it will emit this image on the specified path and return the object, which will contain the path to the all versions of the image and srcSet.
135+
136+
## Options
137+
### `resolution`
138+
Type: `String`
139+
Required: `true`
140+
141+
Resolution of original image. Could be `1x`, `2x` or `3x`.
142+
143+
> ℹ️ It is recommended to set the height and width of the initial image multiple of value you specify in this option
144+
145+
### `breakpoints`
146+
Type: `Array`
147+
Required: `true`
148+
149+
Array of breakpoints for which you want generate optimized images. Each breakpoint require a `name` and `minWidth` or `maxWidth` fields. Example:
150+
```javascript
151+
[
152+
{
153+
name: 'mobile',
154+
maxWidth: 767,
155+
},
156+
{
157+
name: 'tablet',
158+
minWidth: 768,
159+
maxWidth: 1279,
160+
},
161+
{
162+
name: 'desktop',
163+
minWidth: 1280,
164+
}
165+
]
166+
```
167+
168+
Order of breakpoints matter — you should start from the smallest.
169+
170+
> ℹ️ You should name your images according to the breakpoints for which they will be used, with one exception - name `all`. Image with name `all` will be last in source list and will be default, if there is no image for particular breakpoint. See [the example](./example).
171+
172+
### `imgproxy`
173+
Type: `Object`
174+
Required: `true`
175+
176+
Object that contains settings for [imgproxy](https://github.com/imgproxy/imgproxy) server.
177+
178+
#### `disable`
179+
Type: `Boolean`
180+
Default: `false`
181+
Required: `false`
182+
183+
Disable processing of images by this loader (useful in development). Images data will still be generated but only for the original resolution.
184+
185+
#### `imagesHost`
186+
Type: `string`
187+
Required: `true`
188+
189+
URL of your images host.
190+
191+
#### `host`
192+
Type: `string`
193+
Required: `true`
194+
195+
URL of your imgproxy host.
196+
197+
## Components
198+
### `Picture`
199+
Returns <picture> tag with <source> tags according to your breakpoints.
200+
201+
#### `Props`
202+
##### `sources`
203+
Type: [`BreakpointSource`](./src/types.ts)
204+
Required: `true`
205+
206+
Accepts an array of objects that contains information about images srsSets for different breakpoints.
207+
208+
##### `alt`
209+
Type: `String`
210+
Required: `false`
211+
212+
Alt text for <img>.
213+
214+
##### `testId`
215+
Type: `String`
216+
Required: `false`
217+
218+
`data-testid` attribute for <img>.
219+
220+
##### `loading`
221+
Type: `String`
222+
Required: `false`
223+
224+
`loading` attribute for <img>. Can be one of: `eager`, `lazy`.
225+
226+
##### `className`
227+
Type: `String`
228+
Required: `false`
229+
230+
`className` attribute for <picture>.
231+
232+
### `PictureSmart`
233+
234+
Accepts all the same props as [`Picture`](#Picture), except `requireImages`.
235+
236+
#### `Props`
237+
##### `requireImages`
238+
Type: `__WebpackModuleApi.RequireContext`
239+
Required: `true`
240+
241+
Accepts webpack `require.context()` object and builds from it [`sources`](#sources) array. See [the example](./example).
242+
243+
## Utils
244+
245+
### getOriginal
246+
Accepts [`BreakpointSource`](./src/types.ts) array and return biggest URL to original image
247+
```js
248+
const pictureData = require('./images/foo/boo.png')
249+
getOriginal(pictureData)
250+
```
251+
#### pictureData
252+
Type: [`BreakpointSource`](./src/types.ts)
253+
An array of objects that contains information about images srsSets for different breakpoints
254+
255+
### backgroundCss
256+
Accepts CSS selector and [`BreakpointSource`](./src/types.ts) object and return css string that contains rules for `background-image` for your breakpoints for provided selector.
257+
```js
258+
const pictureData = require('./images/foo/boo.png')
259+
const selector = '.boo'
260+
backgroundCss(selector, pictureData)
261+
```
262+
Will return
263+
```css
264+
.boo {
265+
background-image:url(*generated url to your image*);
266+
}
267+
268+
@media *media rules for one of your breakpoints* {
269+
.boo {
270+
background-image: url(*generated url to your image*);
271+
}
272+
}
273+
274+
@media *media rules for one of your breakpoints* {
275+
.boo{
276+
background-image:url(*generated url to your image*)
277+
}
278+
}
279+
```
280+
#### selector
281+
Type: `String`
282+
CSS selector for which you want add optimized images
283+
284+
#### pictureData
285+
Type: [`BreakpointSource`](./src/types.ts)
286+
An array of objects that contains information about images srsSets for different breakpoints
287+
288+
### backgroundCssSmart
289+
Works the same as [`backgroundCss`](#backgroundCss) but instead of [`BreakpointSource`](./src/types.ts) it accepts `__WebpackModuleApi.RequireContext` object.
290+
```js
291+
const pictureData = require.context('./images/foo')
292+
backgroundCss('.boo', pictureData)
293+
```
294+
295+
## License
296+
297+
[MIT](./LICENSE)

0 commit comments

Comments
 (0)