A Webp polyfill for Angular, using webp-hero. This module applies to polyfill as Angular Pipes/Directives for:
- Image elements
- Elements with a
background-imagestyle
You can view a demo here.
NPM
npm install --save ngx-webp-polyfill webp-hero p-queue
Yarn
yarn add ngx-webp-polyfill webp-hero p-queue
To allow the polyfill to be applied to images one-at-a-time (as enforced by webp-hero) we use p-queue to queue the decoding.
Configure the default and/or override providers in your ApplicationConfig.
import { ApplicationConfig, Inject } from '@angular/core';
import { provideRouter } from '@angular/router';
import { externalPolyfillFactory, DEFAULT_WEBP_OPTIONS, polyfillServiceFactory, WEBP_POLYFILL, WEBP_POLYFILL_EXTERNAL, WEBP_POLYFILL_OPTIONS } from 'ngx-webp-polyfill';
export const appConfig: ApplicationConfig = {
providers: [
{
provide: WEBP_POLYFILL_OPTIONS,
useValue: DEFAULT_WEBP_OPTIONS
},
{
provide: WEBP_POLYFILL,
useFactory: polyfillServiceFactory,
deps: [ [new Inject(WEBP_POLYFILL_OPTIONS)], [new Inject(WEBP_POLYFILL_EXTERNAL)]]
},
{
provide: WEBP_POLYFILL_EXTERNAL,
useFactory: externalPolyfillFactory
}
]
};
Import NgxWebpPolyfillModule.forRoot(options?: WebpPolyfillOptions) into AppModule.
WebpPolyfillOptions
applyPolyfill: (url: string) => boolean(defaults to true) to apply the polyfill conditionally i.e. for specific browsers
import { NgxWebpPolyfillModule } from 'ngx-webp-polyfill';
export function webpPolyfillOptions(url: string) {
return true;
}
@NgModule({
imports: [
NgxWebpPolyfillModule.forRoot({
applyPolyfill: webpPolyfillOptions
})
]
})
export class AppModule { }
Import NgxWebpPolyfillModule.forChild() into your application child modules, where needed.
import { NgxWebpPolyfillModule } from 'ngx-webp-polyfill';
@NgModule({
imports: [
NgxWebpPolyfillModule.forChild()
]
})
export class MyFeatureModule { }
Apply the polyfill directives in your template with the following options.
<img *ngIf="myImageUrl | webpImage | async as imagePipeTransform"
[src]="imagePipeTransform"
alt="My Image"
class="thumbnail"/>
Note: The use of *ngIf is required here as the async pipe will emit null as an initial value.
<div [style.background-image]="'url(' + myImageUrl + ')' | webpBackground | async"
style="width: 200px; height: 200px"></div>
Fork and send us a pull request. Consider discussing the change with us before committing the code. The library follows semantic versioning principles, which is automated using conventional commit messages.
npm run build:libraryto watch changescd dist/ngx-webp-polyfill && npm link && cd ../../ && npm link ngx-webp-polyfillallows the demo application to develop against the locally built librarynpm startto start the demo application
cd dist/ngx-webp-polyfill && npm unlink && cd ../../ && npm unlink ngx-webp-polyfillrevertsnpm link
To use the interactive CLI when committing:
$ git add <file/s to commit>
$ npm run commit
Or use your IDE, commits messages are linted!
These instructions are for maintainers of this library, to release a new version:
- Generate new version and tag with
npm run release && git push --follow-tags origin master - Builds and package the library
npm run package:library - Publish the library to NPM
cd dist/ngx-webp-polyfill && npm publish
- To build the demo run
npm run build