Skip to content

Commit

Permalink
Merge pull request #247 from CSSSR/feat/COM-3172
Browse files Browse the repository at this point in the history
[COM-3172]: add avif support
  • Loading branch information
ArtyomResh authored May 17, 2021
2 parents 361e608 + 93d924b commit 2346c13
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
4 changes: 2 additions & 2 deletions example/react-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { PictureSmart } from '../src/react';
import { backgroundCssSmart } from '../src/utils/backgroundCss';
import { getOriginal } from '../src/utils';

// Adds 'webp' class to html element if the browser supports webp.
import '../src/utils/webpDetector';
// Adds 'webp' or 'avif' class to html element if the browser supports it.
import '../src/utils/imgSupportDetector';

const oneImageForAllBreakpoints = require.context('./images/oneImageForAllBreakpoints');
const differentBreakpoints = require.context('./images/differentBreakpoints');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@csssr/csssr.images",
"version": "3.3.2",
"version": "3.3.3",
"description": "Utilities for Webpack and React to simplify images workflow",
"main": "dist/index.js",
"types": "dist",
Expand Down
10 changes: 9 additions & 1 deletion src/utils/backgroundCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ const breakpointMedia = (media: string | null): ((nestedCss: string) => string)
};

const getSelector = (selector: string, extension: string) => {
return extension === 'webp' ? `html.webp ${selector}` : selector;
if (extension === 'avif') {
return `html.avif ${selector}`;
}

if (extension === 'webp') {
return `html.webp ${selector}`;
}

return selector;
};

const srcSetCss = (selector: string, sources: ExtensionSrcSet[]): string => {
Expand Down
14 changes: 14 additions & 0 deletions src/utils/imgSupportDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// https://avif.io/blog/tutorials/use-avif-in-css/

function imgSupportDetector () {
const avif = new Image();
avif.src = "data:image/avif;base64,AAAAFGZ0eXBhdmlmAAAAAG1pZjEAAACgbWV0YQAAAAAAAAAOcGl0bQAAAAAAAQAAAB5pbG9jAAAAAEQAAAEAAQAAAAEAAAC8AAAAGwAAACNpaW5mAAAAAAABAAAAFWluZmUCAAAAAAEAAGF2MDEAAAAARWlwcnAAAAAoaXBjbwAAABRpc3BlAAAAAAAAAAQAAAAEAAAADGF2MUOBAAAAAAAAFWlwbWEAAAAAAAAAAQABAgECAAAAI21kYXQSAAoIP8R8hAQ0BUAyDWeeUy0JG+QAACANEkA=";
avif.onload = () => { document.documentElement.classList.add("avif") };

// before we remove `generateAvif` flag, we want to detect webp support even if avif support is present
const webp = new Image();
webp.src = "data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==";
webp.onload = () => { document.documentElement.classList.add("webp") };
}

imgSupportDetector();
15 changes: 0 additions & 15 deletions src/utils/webpDetector.ts

This file was deleted.

16 changes: 15 additions & 1 deletion src/webpack/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type LoaderOptions = {
disable: boolean;
imagesHost: string;
host: string;
generateAvif: boolean;
};
originalPixelRatio: Dpr;
};
Expand Down Expand Up @@ -68,7 +69,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
(a, b, imagePath) => imagePath,
);

let webpSrcSet: SrcSet, originalExtensionSrcSet: SrcSet, data: OrderedBreakpointSource;
let avifSrcSet: SrcSet, webpSrcSet: SrcSet, originalExtensionSrcSet: SrcSet, data: OrderedBreakpointSource;
// Disables picture processing, srcSet is generated only for the original image type
if (options.imgproxy.disable) {
data = {
Expand All @@ -86,6 +87,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
};
} else {
const buildUrlsForPixelRatios = getImgproxyUrlBuilder(options.imgproxy);
avifSrcSet = buildUrlsForPixelRatios(pixelRatios, outputImagePath, 'avif');
webpSrcSet = buildUrlsForPixelRatios(pixelRatios, outputImagePath, 'webp');
originalExtensionSrcSet = buildUrlsForPixelRatios(
pixelRatios,
Expand All @@ -107,6 +109,18 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
},
],
};

if (options.imgproxy.generateAvif) {
data.srcSets.unshift({
extension: 'avif',
srcSet: avifSrcSet,
})

imageUrls.push(
...(Object.values(avifSrcSet) as string[]),
);
}

// Add links to images via imgproxy to the global object
imageUrls.push(
...(Object.values(webpSrcSet) as string[]),
Expand Down

0 comments on commit 2346c13

Please sign in to comment.