Skip to content

Commit 820d213

Browse files
committed
COM-2479:
- refactoring backgroundCss, add tests - add tests getCompressionRatio - add getOriginalExtensionSrcSet, add test - refactoring dev mode - originalPixelRatio typing refactoring
1 parent a0e2998 commit 820d213

10 files changed

Lines changed: 92 additions & 22 deletions

src/utils/__tests__/backgroundCss.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,34 @@ test('backgroundCss multiple breakpoints, png and webp', () => {
397397
}
398398
}`);
399399
});
400+
401+
test('backgroundCss all breakpoints, png and webp, one pixel ratio', () => {
402+
expect(
403+
backgroundCss('.my-selector', [
404+
{
405+
breakpointMedia: null,
406+
srcSets: [
407+
{
408+
extension: 'png',
409+
srcSet: {
410+
'1x': '/mobile.all.1x.png',
411+
},
412+
},
413+
{
414+
extension: 'webp',
415+
srcSet: {
416+
'1x': '/mobile.all.1x.webp',
417+
},
418+
},
419+
],
420+
},
421+
]),
422+
).toBeSameCss(`
423+
.my-selector {
424+
background-image: url(/mobile.all.1x.png);
425+
}
426+
html.webp .my-selector {
427+
background-image: url(/mobile.all.1x.webp);
428+
}
429+
`);
430+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getCompressionRatio } from '../index';
2+
3+
4+
test('Pixel ratios 1x', () => {
5+
expect(
6+
getCompressionRatio(['1x']),
7+
).toStrictEqual({"1x": 0});
8+
});
9+
10+
test('Pixel ratios 1x, 2x', () => {
11+
expect(
12+
getCompressionRatio(['1x','2x']),
13+
).toStrictEqual({"1x": 0.5, "2x": 0});
14+
});
15+
16+
test('Pixel ratios 1x, 2x, 3x', () => {
17+
expect(
18+
getCompressionRatio(['1x','2x', '3x']),
19+
).toStrictEqual({"1x": 0.3333333333333333, "2x": 0.6666666666666666, "3x": 0});
20+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getOriginalExtensionSrcSet } from '../index';
2+
3+
4+
test('Pixel ratios 1x, path "/images/mayImages.png"', () => {
5+
expect(
6+
getOriginalExtensionSrcSet(['1x'],'/images/mayImages.png'),
7+
).toStrictEqual({"1x": '/images/mayImages.png'});
8+
});
9+
10+
test('Pixel ratios 1x, 2x, path "/images/mayImages.png"', () => {
11+
expect(
12+
getOriginalExtensionSrcSet(['1x','2x'],'/images/mayImages.png'),
13+
).toStrictEqual({"1x": '/images/mayImages.png', "2x": '/images/mayImages.png'});
14+
});
15+
16+
test('Pixel ratios 1x, 2x, 3x, path "/images/mayImages.png"', () => {
17+
expect(
18+
getOriginalExtensionSrcSet(['1x','2x','3x'],'/images/mayImages.png'),
19+
).toStrictEqual({"1x": '/images/mayImages.png', "2x": '/images/mayImages.png', "3x": '/images/mayImages.png'});
20+
});

src/utils/backgroundCss.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const srcSetCss = (selector: string, sources: ExtensionSrcSet[]): string => {
1717
(acc, source) => {
1818
const finalSelector = getSelector(selector, source.extension);
1919
acc['1x'].push(`${finalSelector} { background-image: url(${source.srcSet['1x']}); }`);
20-
acc['2x'].push(`${finalSelector} { background-image: url(${source.srcSet['2x']}); }`);
21-
acc['3x'].push(`${finalSelector} { background-image: url(${source.srcSet['3x']}); }`);
20+
source.srcSet['2x'] && acc['2x'].push(`${finalSelector} { background-image: url(${source.srcSet['2x']}); }`);
21+
source.srcSet['3x'] && acc['3x'].push(`${finalSelector} { background-image: url(${source.srcSet['3x']}); }`);
2222
return acc;
2323
},
2424
{ '1x': [], '2x': [], '3x': [] },
2525
);
2626

2727
return `
28-
${result['1x'].join(' ')}
29-
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) { ${result['2x'].join(' ')} }
30-
@media (-webkit-min-device-pixel-ratio: 3), (min-resolution: 288dpi) { ${result['3x'].join(' ')} }
28+
${result['1x'].join(' ')}
29+
${result['2x'].length ? `@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) { ${result['2x'].join(' ')} }`:''}
30+
${result['3x'].length ? ` @media (-webkit-min-device-pixel-ratio: 3), (min-resolution: 288dpi) { ${result['3x'].join(' ')} }`:''}
3131
`;
3232
};
3333

src/utils/getCompressionRatio.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Dpr, CompressionRatio } from '../types';
22

3-
// [1x, 2x] -> {1x: 0.5, 2x: 0}
4-
53
export const getCompressionRatio = (pixelRatios: Dpr[]): CompressionRatio => {
64
const length = pixelRatios.length;
75

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SrcSet, Dpr } from '../types';
2+
3+
export const getOriginalExtensionSrcSet = (pixelRatios: Dpr[], outputImagePath:string):SrcSet => {
4+
return pixelRatios.reduce((acc, item ) => {
5+
acc[item] = outputImagePath;
6+
return acc;
7+
}, {} as SrcSet);
8+
}

src/utils/getPixelRations.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ export const getPixelRations = (originalPixelRatio: Dpr): Dpr[] => {
88
return ['1x', '2x'];
99
case '3x':
1010
return ['1x', '2x', '3x'];
11-
default:
12-
return ['1x'];
1311
}
1412
};

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export { getSources } from './getSources';
55
export { getOriginal } from './getOriginal';
66
export { getPixelRations } from './getPixelRations';
77
export { getCompressionRatio } from './getCompressionRatio';
8+
export { getOriginalExtensionSrcSet } from './getOriginalExtensionSrcSet';

src/webpack/loader.ts

Lines changed: 5 additions & 12 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 } from '../utils';
9+
import { getBreakpointMedia, getPixelRations, getOriginalExtensionSrcSet } 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 pixelRations: Dpr[] = getPixelRations(options.originalPixelRatio);
33+
const pixelRatios: Dpr[] = getPixelRations(options.originalPixelRatio);
3434
const breakpoints: Breakpoint[] = options.breakpoints;
3535
// Такой результат приходит от file-loader 'module.exports = "/build/myImage/mobile.all-4b767a7b.png";'
3636
// Получаем оригинальное имя файла изображения (originalImageFileName = mobile.all.png)
@@ -70,28 +70,21 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
7070
let webpSrcSet: SrcSet, originalExtensionSrcSet: SrcSet, data: OrderedBreakpointSource;
7171
// Отключает процессинг картинок, генерируется srcSet только для оригинального типа изображения
7272
if (options.imgproxy.disable) {
73-
// TODO пока не смотрим disable
74-
75-
originalExtensionSrcSet = {
76-
'1x': outputImagePath,
77-
'2x': outputImagePath,
78-
'3x': outputImagePath,
79-
};
8073
data = {
8174
order,
8275
breakpointMedia,
8376
srcSets: [
8477
{
8578
extension: originalExtension,
86-
srcSet: originalExtensionSrcSet,
79+
srcSet: getOriginalExtensionSrcSet(pixelRatios,outputImagePath),
8780
},
8881
],
8982
};
9083
} else {
9184
const buildUrlsForPixelRatios = getImgproxyUrlBuilder(options.imgproxy);
92-
webpSrcSet = buildUrlsForPixelRatios(pixelRations, outputImagePath, 'webp');
85+
webpSrcSet = buildUrlsForPixelRatios(pixelRatios, outputImagePath, 'webp');
9386
originalExtensionSrcSet = buildUrlsForPixelRatios(
94-
pixelRations,
87+
pixelRatios,
9588
outputImagePath,
9689
originalExtension,
9790
);

src/webpack/loaderOptionsSchema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ export const schema: JSONSchema7 = {
5050
},
5151
originalPixelRatio: {
5252
type: 'string',
53+
pattern: "^(1x|2x|3x)$",
5354
},
5455
},
55-
required: ['breakpoints', 'imgproxy'],
56+
required: ['breakpoints', 'imgproxy','originalPixelRatio'],
5657
additionalProperties: false,
5758
};

0 commit comments

Comments
 (0)