Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COM-3172]: add avif support #247

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/react-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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';
import '../src/utils/imgSupportDetector';

const oneImageForAllBreakpoints = require.context('./images/oneImageForAllBreakpoints');
const differentBreakpoints = require.context('./images/differentBreakpoints');
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
17 changes: 17 additions & 0 deletions src/utils/imgSupportDetector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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") };

avif.onerror = () => {
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.

8 changes: 7 additions & 1 deletion src/webpack/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,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 +86,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 @@ -97,6 +98,10 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
order,
breakpointMedia,
srcSets: [
{
extension: 'avif',
srcSet: avifSrcSet,
},
{
extension: 'webp',
srcSet: webpSrcSet,
Expand All @@ -109,6 +114,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
};
// Add links to images via imgproxy to the global object
imageUrls.push(
...(Object.values(avifSrcSet) as string[]),
...(Object.values(webpSrcSet) as string[]),
...(Object.values(originalExtensionSrcSet) as string[]),
);
Expand Down