question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parcel 2: HTML Image Transformer

See original GitHub issue

Create the @parcel/transformer-html-images (name TBD) package. This should include a Transformer plugin that transforms <img> tags in HTML to <picture> elements with multiple sources. It should work with the @parcel/transformer-webp and @parcel/transformer-image-resize plugins to create multiple dependencies that are transformed by those plugins and re-inserted later as URLs. This handles generating multiple versions of an image with different sizes and formats from a source HTML file and image completely automatically.

HTML like this:

<img src="./image.jpg" width="200">

should be transformed into HTML like this, with dependencies created for each referenced asset:

<picture>
  <source type="image/webp" srcset="webp:./image.jpg?width=200, webp:./image.jpg?width=400 2x">
  <source type="image/jpeg" srcset="./image.jpg?width=200, ./image.jpg?width=400 2x">
  <img src="./image.jpg?width=200" width="200">
</picture>

This automatically handles creating both WebP and JPG versions of the image, at multiple resolutions.

In addition, the srcset attribute should be supported to generate multiple versions with specific sizes from the same input image:

<img srcset="image.jpg 480w, image.jpg 1080w">

becomes:

<picture>
  <source type="image/webp" srcset="webp:image.jpg?width=480 480w, webp:image.jpg?width=1080 1080w">
  <source type="image/jpeg" srcset="image.jpg?width=480 480w image.jpg?width=1080 1080w">
  <img src="./image.jpg?width=1080w">
</picture>

Note that the protocol and query param syntax here is internal to Parcel and would not be shipped to users. After the images are transformed, the final URL would be placed into the HTML file as expected.

Open Questions

  • What should the default resolutions include? 1x, 2x, 3x? Should this happen by default at all or should we require users specify srcset in their code themselves? Seems like it should be fairly common to want this…
  • What should the default formats include? WebP and the original format? WebP and JPEG always?
  • What should happen if there is no width or height set on an <img>? Just convert to WebP without resizing?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kevincoxcommented, Feb 20, 2020

I wouldn’t want this to be specific to HTML. I have a lot of my source images as SVG but sometimes need/want them rendered into other formats. For example:

  • Background images for CSS.
  • Icons for a Web App Manifest.
  • Image URLs used by javascript.
  • Favicons.

All of these would have a use for images rendered into select formats at different resolutions. Can we try to find a solution that works uniformly wherever images are needed?

0reactions
thewilkybarkidcommented, Dec 1, 2021
  • What should the default resolutions include? 1x, 2x, 3x? Should this happen by default at all or should we require users specify srcset in their code themselves? Seems like it should be fairly common to want this…

1x and 2x is probably enough as a default (though I have found 2x images can still be fairly large and 1.5x can be a decent compromise).

  • What should the default formats include? WebP and the original format? WebP and JPEG always?

Reading https://www.smashingmagazine.com/2021/09/modern-image-formats-avif-webp/ and https://www.contentful.com/blog/2021/11/29/load-avif-webp-using-html/, I wonder if it should be like:

Input Sources
JPEG AVIF (lossy), WebP (lossy), JPEG
PNG WebP (lossless), PNG

There’s never going to be a silver bullet for this though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image
Images can be referenced from HTML, CSS, JavaScript, or any other file type. Resizing and converting images. #. Parcel includes an image transformer...
Read more >
parcel/transformer-image NPM
Parcel takes all of your files and dependencies, transforms them, and merges them together into a smaller set of output files that can...
Read more >
parcel/transformer-image
Start using @parcel/transformer-image in your project by running `npm i @parcel/transformer-image`. There are 5 other projects in the npm ...
Read more >
@parcel/transformer-image | Yarn - Package Manager
The format is based on Keep a Changelog and Parcel adheres to Semantic Versioning. [2.8.2] - 2022-12-14. Core. Ensure maxListeners for process.stdout accounts ......
Read more >
What you need to know about Parcel 2
Parcel comes with an image transformer that lets you resize photos, convert them to a new format, and minimize file size by adjusting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found