NJAldwin / eleventy-plugin-gen-favicons

Favicon generator plugin for eleventy
MIT License
15 stars 4 forks source link

Eleventy Favicon Generation

eleventy-plugin-gen-favicons

An Eleventy plugin to generate favicons based on the 2022 best practices.

Include as a {% favicons 'source-image.svg' %} in your template. On build, all relevant favicons will be generated, and HTML referencing the icons will be placed in your template.

Given a single square input icon file (preferably .svg), the following is generated:

Installation

Available on npm.

npm install --save-dev eleventy-plugin-gen-favicons

In .eleventy.js (or your config file if differently named), use addPlugin:

// .eleventy.js
const faviconsPlugin = require("eleventy-plugin-gen-favicons");

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(faviconsPlugin, {});
};

NOTE If you already have module.exports, only copy the require and addPlugin lines, as with all other plugins.

The plugin accepts a few options (replace {} as necessary):

Example:

  eleventyConfig.addPlugin(faviconsPlugin, {'outputDir': './generated_site', 'manifestData': {'name': 'My Website'}});

Usage

Include the favicons shortcode somewhere within <head> in your template (all languages except Handlebars are supported):

<head>
{% favicons 'my-source-image.svg', appleIconBgColor='#123' %}
</head>

NOTE Do not use multiple favicons shortcodes with different images or bg colors. Favicons are site-wide and this will result in undefined behavior. It is fine to conditionally set a favicon, as long as it's site-wide.

The shortcode accepts additional options:

Examples:

{% favicons 'favicon.svg' %}
{% favicons 'favicon.png' %}
{% favicons 'favicon.svg', appleIconBgColor='#000' %}
{% favicons 'favicon.svg', appleIconBgColor='black', manifestData={name:'My Website'} %}
{% favicons 'favicon.svg', appleIconBgColor='#f00', appleIconPadding=10, generateManifest=false %}

Example generated HTML:

<link rel="icon" href="https://github.com/NJAldwin/eleventy-plugin-gen-favicons/blob/master/favicon.ico" sizes="any">
<link rel="icon" href="https://github.com/NJAldwin/eleventy-plugin-gen-favicons/blob/master/favicon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="https://github.com/NJAldwin/eleventy-plugin-gen-favicons/blob/master/apple-touch-icon.png">
<link rel="manifest" href="https://github.com/NJAldwin/eleventy-plugin-gen-favicons/blob/master/manifest.webmanifest">

Security

This package uses sharp and png-to-ico and at the time of publish shows no vulnerabilities with npm audit. It is fully tested and should not alter any files other than the favicons it generates.

More Favicons

This generates the "Six files that fit most needs" for favicons in 2022. For sites that require more icons, a custom solution based on the favicons package is recommended.

Contributing

PRs to the GitHub repo are welcome. Please make sure the tests pass.