Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

posthtml-webp

posthtml210MIT2.2.0

Add webp supporting in your html

html, posthtml, posthtml-plugin, webp

readme

posthtml-webp

NPM Deps Build Coverage Standard Code Style Chat

This plugin add webp supporting in your html. Also supports <amp-img>

Install

$ npm i posthtml posthtml-webp

Usage

const fs = require('fs');
const posthtml = require('posthtml');
const posthtmlWebp = require('posthtml-webp');

posthtml()
    .use(posthtmlWebp(/* Plugin options */))
    .process(html/*, options */)
    .then(result => fs.writeFileSync('./after.html', result.html));

Example

Before:

<img src="image.jpg">
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png"></amp-img>

After:

<picture>
    <source type="image/webp" srcset="image.jpg.webp">
    <img src="image.jpg">
</picture>
<amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png.webp">
    <amp-img alt="photo" width="550" height="368" layout="responsive" src="photo.png" fallback=""></amp-img>
</amp-img>

Options

extension

Type: string Default: .webp Description: Add custom extension or even prefix Example: image.jpg => image.jpg?as=webp (instead of image.jpg.webp)

replaceExtension

Type: Boolean Default: false Description: Replace the extension of the source image with .webp instead of appending .webp to the original filename Example: image.jpg => image.webp (instead of image.jpg.webp)

classIgnore

Type: Array<string> Default: [] Description: list of classes for which the transformation will be ignored Example: classIgnore: ['ignore-webp'] will ignore transformation for images with the class ignore-web

extensionIgnore

Type: Array<string> Default: [] Description: list of extension for which the transformation will be ignored Example: extensionIgnore: ['svg'] will ignore transformation for images with the svg extension

lazySrcset

Type: String Default: data-srcset Description: The attribute used for lazy webp loading. Use it if you have lazy attribute for srcset on your images. It will be set on created <source> to later be processed by external lazy loading library. Example: lazySrcset: 'my-srcset' will set my-srcset attribute on <source>

lazySrc

Type: String Default: data-src Description: The attribute used for lazy webp loading. The original <img> may not contain src at all, but instead some custom lazy-loading attribute. Or it may contain just a placeholder image inside src which shouldn't be used for webp conversion. lazySrc will define a custom attribute name to look at when processing your lazy loaded images. Note that lazySrcset is still needed even if <img> has only lazySrc defined, because srcset is the mechanism for defining a source file for the <source>. See lazySrcset option description. Example: lazySrc: 'my-src' will convert an image inside my-src attribute, instead of regular src.

License MIT

changelog

2.2.0 (2022-02-28)

  • build: update changelog (2c106ea)
  • build: update dep dev (ede17a7)
  • build(deps): bump handlebars from 4.7.6 to 4.7.7 (decb352)
  • build(deps): bump hosted-git-info from 2.8.8 to 2.8.9 (45a6939)
  • build(deps): bump ini from 1.3.5 to 1.3.7 (b7a2e87)
  • build(deps): bump normalize-url from 4.5.0 to 4.5.1 (d8bb942)
  • build(deps): bump path-parse from 1.0.6 to 1.0.7 (0314d82)
  • build(deps): bump y18n from 4.0.0 to 4.0.1 (7beb82e)
  • perf: fix after lint (56d0de9)
  • Option extension added (5e04605)

2.1.0 (2020-08-28)

2.0.0 (2020-08-27)

  • build: update changelog (aa3f841)
  • build: update dep dev (3332933)
  • build: v2.0.0 (09f7fd2)
  • ci: drop support old node (28e2ab6)
  • Use unchanged srcset as <source> srcset (cf0ff28)

1.5.1 (2020-05-29)

  • 1.5.1 (0b2ad11)
  • build: drop mversion (89e7273)
  • build: perf config to file (11397da)
  • build: update changelog (e765f25)
  • build: update changelog (97dcc39)
  • build: update commit lint (110471e)
  • build: update dep dev (2de382c)
  • build: update lock (836009d)
  • build(deps): bump acorn from 7.0.0 to 7.1.1 (3762ced)
  • build(deps): bump handlebars from 4.1.2 to 4.7.3 (f45ea6b)
  • ci: update coveralls (3cbd9a0)
  • fix: fixture ignore indentation (314d9f0)
  • fix: handle classIgnore for img with no class attribute (3233f0f)

1.5.0 (2020-02-25)

  • build: bump version (7f869de)
  • build: update dep dev (2002e72)
  • refactor: separate ignore to two separate methods (ffba43d)
  • refactor: use explicit comparisons (c232f5c)
  • docs: fix wrong example for (923087b)
  • docs: update readme with extensionIgnore option (e5c62de)
  • feat: add the ability to ignore by extension (a390853)

1.4.0 (2019-10-22)

1.3.0 (2019-09-26)

1.2.0 (2019-09-20)

1.1.0 (2019-07-31)

  • chore: rename to upper (fffa27f)
  • chore: update depDev (84433e8)
  • chore: v1.1.0 (f77ed1d)
  • build: add conventional-changelog-cli (d6680c9)
  • build: bump (c53c66e)
  • build: bump (574fd38)
  • build: bump (aa50a08)
  • build: update changelog (622ecd0)
  • build: update hooks and script system (2940b65)
  • ci: add travis config (13d213c)
  • ci: drop support old node (11e929f)
  • ci: fix travis config ext (fd1b18d)
  • test: replaceExtension feature disabled (851a714)
  • test: replaceExtension feature enabled (8120ad1)
  • test: replaceExtension feature unspecified (a2c512e)
  • test: style according linter (c05c33a)
  • feat: Replace file extension instead of appending .webp (9921f53)
  • feat: restore from delete git repository (f81e404)
  • perf: add node 10 and 11 (2e8f2e0)
  • perf: add nyc and coverals (39625dc)
  • fix: path to this pkg (645e85a)
  • docs: add correct pkg name (74f6789)