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

Always create webp version #44

Closed
klase opened this issue Nov 30, 2018 · 2 comments
Closed

Always create webp version #44

klase opened this issue Nov 30, 2018 · 2 comments

Comments

@klase
Copy link

klase commented Nov 30, 2018

How can I configure this plugin to always create a webp version when requiring an image (I.e without having to require twice )?

E.g so require('./images/someimage.jpg') should create two output files, one optimised .jpg and one optimised .webp

@cyrilwanner
Copy link
Owner

Hi @klase
Unfortunately, this isn't possible at the moment. But I take it on the todo list, I think this could be quite useful. Thank you for creating this issue :)

@majksner
Copy link

If I understand correctly what you're trying to achieve you can do it this way:

import React from 'react'

export default function Image(props) {
  return (
    <picture>
      <source
        srcSet={require(`../static/images/${props.src}?webp`)}
        type="image/webp"
      />
      <source
        srcSet={require(`../static/images/${props.src}`)}
        type={`image/${props.src.split('.').pop()}`}
      />
      <img
        src={require(`../static/images/${props.src}`)}
        width={props.width}
        height={props.height}
        alt={props.alt}
        className={props.className}
      />
    </picture>
  )
}

Then then use component anywhere in your pages or other components:

<Image src="image.png" width="237" height="237" className="img-fluid" alt="Improve comfort" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants