Skip to content

Commit 8b2dfa4

Browse files
committed
COM-2479:
- add rounding to getCompressionRatio - rename getPixelRations -> getPixelRatios - refactoring use getOriginalExtensionSrcSet
1 parent 9c2a469 commit 8b2dfa4

7 files changed

Lines changed: 11 additions & 39 deletions

File tree

src/utils/__tests__/getCompressionRatio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ test('Pixel ratios 1x, 2x', () => {
1010

1111
test('Pixel ratios 1x, 2x, 3x', () => {
1212
expect(getCompressionRatio(['1x', '2x', '3x'])).toStrictEqual({
13-
'1x': 0.3333333333333333,
14-
'2x': 0.6666666666666666,
13+
'1x': 0.33333,
14+
'2x': 0.66667,
1515
'3x': 0,
1616
});
1717
});

src/utils/__tests__/getOriginalExtensionSrcSet.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/utils/getCompressionRatio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const getCompressionRatio = (pixelRatios: Dpr[]): CompressionRatio => {
99
return acc;
1010
}
1111

12-
acc[item] = (index + 1) / length;
12+
acc[item] = Number(((index + 1) / length).toFixed(5));
1313
return acc;
1414
}, {} as CompressionRatio);
1515
};

src/utils/getOriginalExtensionSrcSet.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Dpr } from '../types';
22

3-
export const getPixelRations = (originalPixelRatio: Dpr): Dpr[] => {
3+
export const getPixelRatios = (originalPixelRatio: Dpr): Dpr[] => {
44
switch (originalPixelRatio) {
55
case '1x':
66
return ['1x'];

src/utils/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ export { getBreakpointMedia } from './getBreakpointMedia';
33
export { getSrcSetString } from './getSrcSetString';
44
export { getSources } from './getSources';
55
export { getOriginal } from './getOriginal';
6-
export { getPixelRations } from './getPixelRations';
6+
export { getPixelRatios } from './getPixelRatios';
77
export { getCompressionRatio } from './getCompressionRatio';
8-
export { getOriginalExtensionSrcSet } from './getOriginalExtensionSrcSet';

src/webpack/loader.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { getImgproxyUrlBuilder } from './imgproxyUrlBuilder';
66
import { Breakpoint, OrderedBreakpointSource, SrcSet, Dpr } from '../types';
77
import { imageUrls } from './plugin';
88
import { schema } from './loaderOptionsSchema';
9-
import { getBreakpointMedia, getPixelRations, getOriginalExtensionSrcSet } from '../utils';
9+
import { getBreakpointMedia, getPixelRatios } from '../utils';
1010

1111
// Такое имя используется, если нужна одна картинка для всех разрешений
1212
// В таком случаем не будут сгенерированы медиа выражения для разных breakpoint'ов
@@ -30,7 +30,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
3030

3131
validateOptions(schema, options, { name: 'Imgproxy responsive loader', baseDataPath: 'options' });
3232

33-
const pixelRatios: Dpr[] = getPixelRations(options.originalPixelRatio);
33+
const pixelRatios: Dpr[] = getPixelRatios(options.originalPixelRatio);
3434
const breakpoints: Breakpoint[] = options.breakpoints;
3535
// Такой результат приходит от file-loader 'module.exports = "/build/myImage/mobile.all-4b767a7b.png";'
3636
// Получаем оригинальное имя файла изображения (originalImageFileName = mobile.all.png)
@@ -76,7 +76,10 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
7676
srcSets: [
7777
{
7878
extension: originalExtension,
79-
srcSet: getOriginalExtensionSrcSet(pixelRatios, outputImagePath),
79+
srcSet: pixelRatios.reduce((acc, item): SrcSet => {
80+
acc[item] = outputImagePath;
81+
return acc;
82+
}, {} as SrcSet),
8083
},
8184
],
8285
};

0 commit comments

Comments
 (0)