Giter VIP home page Giter VIP logo

eleventy-plugin-gen-favicons's Introduction

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:

  • /favicon.svg (only if a svg is provided)
  • /favicon.ico - 64x64/32x32/16x16 legacy icon
  • /apple-touch-icon.png - 180x180 Apple home screen icon including 20px (configurable) colored padding
  • /icon-192.png - Google home screen icon
  • /icon-512.png - Google loading screen
  • /manifest.webmanifest - Manifest linking the Google icons; can be customized with additional data

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):

  • outputDir: default ./_site -- where to generate the icons; should match the eleventy output directory
  • manifestData: default {} -- additional data to include in the .webmanifest (e.g. {'name': 'My Website'})
  • generateManifest: default true -- set to false to disable manifest generation (if you're already generating a manifest separately)

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:

  • appleIconBgColor: default 'white' -- the color to use for the Apple icon padding; accepts anything that can be parsed by color, such as 'white', '#fff', {r: 128, g: 0, b: 255, alpha: 0.75}, etc
  • appleIconPadding: default 20 -- the size of the Apple icon padding in px per side (must be less than 90px)
  • manifestData: default {} -- additional data to include in the .webmanifest (e.g. {'name': 'My Website'}); overrides the plugin setting of the same name
  • generateManifest: default true -- set to false to disable manifest generation (if you're already generating a manifest separately); overrides the plugin setting of the same name

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="/favicon.ico" sizes="any">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/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.

eleventy-plugin-gen-favicons's People

Contributors

njaldwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

eleventy-plugin-gen-favicons's Issues

change output href in html tags

Hello,

I successfully customized outputdir like this:

outputDir: "./static/assets/img/favicon"

but tags generated are like these:

<link rel="icon" href="/favicon.ico" sizes="any">
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="manifest" href="/manifest.webmanifest">

is it possible to customize the href path?

thank you

Error with Nunjucks shortcode `favicons` (via Template render error)

Hi,
First off , thanks for your work on this plugin.

Not sure if this is related to the the recent changes to png-to-ico.
Last night I started getting this issue coincidentally after I installed eleventy-plugin-sharp-respimg
So, I removed eleventy-plugin-sharp-respimg, but still getting a stack trace: Error.

Re-installed eleventy-plugin-gen-favicons, but my previous working setup is now repeatedly failing.

My favicon is a square.svg

Here are some of the details - not much and removed many useless characters:

[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing template: "_site/index.html" (via EleventyTemplateError)
[11ty] 2. (./src/_includes/default.html)
[11ty]   Template render error: (/opt/eleventy/src/_includes/partials/favicon.html)
[11ty]   EleventyShortcodeError: Error with Nunjucks shortcode `favicons` (via Template render error)
[11ty] 3. �PNG
[11ty] 
IHDR@�iq�       pHYs��b3y!IDATx��[{�TU?sνwvfg�vWw]��Qh%�!��m` R�P��s��>�K��KӈjK�A���EEC)� 
[11ty] �j�&:    �/
                  K����
[�Ő�]�%d>y+t�}I�+�\r�����?�\����<2�    N^'oİ$>�鷘\��A��U�b�X���a�����IEND�B`� is not or a valid PNG file. (via Template render error)
[11ty] 
[11ty] Original error stack trace: Error: �PNG
                                                                                                                                                        \��A��U�b�X���a�����IEND�B`� is not or a valid PNG file.
[11ty]     at /opt/eleventy/node_modules/png-to-ico/lib/png.js:12:10
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 0)
[11ty]     at async module.exports (/opt/eleventy/node_modules/eleventy-plugin-gen-favicons/favicon-gen.js:87:5)
[11ty]     at async Object.<anonymous> **(/opt/eleventy/node_modules/eleventy-plugin-gen-favicons/index.js:25:28)**

My .eleventy.js favicon config:

  eleventyConfig.addPlugin(faviconsPlugin, {
    outputDir: "./_site",
    manifestData: { name: "Acme Inc" },
  });

/opt/eleventy/src/_includes/partials/favicon.html

{% favicons "./src/static/img/favicon.svg", appleIconBgColor='#FFF' %}

Thanks.

Support for multiple manifests

Hi there! Thank you for this plugin :)

Is there a chance to add support for multiple manifests?
I have this structure of my _data/meta.js:

require('dotenv').config()

module.exports = {
  env: process.env.NODE_ENV,
  url: process.env.URL || 'http://localhost:8080',
  en: {
    title: 'title',
    description: 'description',
  },
  ru: {
    title: 'тайтл',
    description: 'описание',
  },
}

And with this structure, I don't see any way to provide metadata to manifestData object to generate multiple manifests and associate them with the corresponding templates. Much appreciated for any help!

Expected number between 1 and 100000 for density but received 0.5625 of type number

oleeskild/digitalgarden#161 (comment)

/opt/build/repo/src/site/_includes/components/pageheader.njk

34
35 {% favicons './src/site/favicon.svg', appleIconBgColor='#123' %}
36


6:06:49 PM: [11ty] Template render error: (/opt/build/repo/src/site/_includes/components/pageheader.njk)
6:06:49 PM: [11ty] EleventyShortcodeError: Error with Nunjucks shortcode favicons (via Template render error)
6:06:49 PM: [11ty] 3. Expected number between 1 and 100000 for density but received 0.5625 of type number (via Template render error)
6:06:49 PM: [11ty]
6:06:49 PM: [11ty] Original error stack trace: Error: Expected number between 1 and 100000 for density but received 0.5625 of type number
6:06:49 PM: [11ty] at Object.invalidParameterError (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/node_modules/sharp/lib/is.js:124:10)
6:06:49 PM: [11ty] at Sharp._createInputDescriptor (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/node_modules/sharp/lib/input.js:92:18)
6:06:49 PM: [11ty] at new Sharp (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/node_modules/sharp/lib/constructor.js:341:29)
6:06:49 PM: [11ty] at Sharp (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/node_modules/sharp/lib/constructor.js:169:12)
6:06:49 PM: [11ty] at resizedSharp (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/favicon-gen.js:34:10)
6:06:49 PM: [11ty] at /opt/build/repo/node_modules/eleventy-plugin-gen-favicons/favicon-gen.js:41:33
6:06:49 PM: [11ty] at Array.map ()
6:06:49 PM: [11ty] at icoBuf (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/favicon-gen.js:41:20)
6:06:49 PM: [11ty] at module.exports (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/favicon-gen.js:92:7)
6:06:49 PM: [11ty] at async Object. (/opt/build/repo/node_modules/eleventy-plugin-gen-favicons/index.js:25:28)
6:06:50 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
6:06:49 PM: [11ty] Copied 29 files / Wrote 0 files in 0.42 seconds (v2.0.1)
6:06:49 PM: ERROR: "build:eleventy" exited with 1.
6:06:49 PM: ​
6:06:49 PM: "build.command" failed
6:06:49 PM: ────────────────────────────────────────────────────────────────

The .svg file used.
favicon

Error with Nunjucks shortcode

Hey there,
Thanks for making this plugin!

I'm having an issue.

From what I can tell, the issue is that the plugin cannot find the svg file.

I can see that the image is in my _site/img directory at build.

I'm using the eleventy-base-blog template, which uses .njk template files.
I've followed the installation instructions to the T, twice, from the npmjs.com page, and gotten the same issue.

This is the line I've put at the end of the <head> section in my base.njk template file:
{% favicons "/img/favicon.svg" %}

I've tried formatting it as:
{% favicons "favicon.svg" %}
{% favicons "/favicon.svg" %}
{% favicons "./favicon.svg" %}
{% favicons "/img/favicon.svg" %}
{% favicons "./img/favicon.svg" %}

I've tried using ' quotes and " quotes.

I've tried renaming it favicon_0001.svg

I've tried loading .png version of the file.

So far, nothing works.

This is the error message that comes up when I try to build:

[11ty/eleventy-base-blog] Excluding drafts.
[11ty] Writing _site/sitemap.xml from ./content/sitemap/sitemap.xml.njk
[11ty] Writing _site/feed/feed.json from ./content/feed/json.njk
[11ty] Writing _site/feed/feed.xml from ./content/feed/feed.njk
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing to "_site/contact/index.html" from "./content/contact.md" (via EleventyTemplateError)
[11ty] 2. (./_includes/layouts/base.njk)
[11ty]   EleventyShortcodeError: Error with Nunjucks shortcode `favicons` (via Template render error)
[11ty] 3. ENOENT: no such file or directory, stat '/img/favicon.svg' (via Template render error)
[11ty] 
[11ty] Original error stack trace: Error: ENOENT: no such file or directory, stat '/img/favicon.svg'
[11ty] Copied 45 files / Wrote 0 files in 1.31 seconds (v2.0.1)

Any ideas?

Thank you,
Liam

Esoteric problem with nunjucks shortcode in Powershell 7

Hi!

First of all, thank you very much for your work. This plugin rocks!

I use it across many projects of mine and I recently was contacted by someone who tried to run my Eleventy template using Windows 10. When I tried reproducing their issue on a Virtualbox machine, I started getting an error Error: EBUSY: resource busy or locked: [path to favicon.svg here]

I can't reproduce it anywhere else than Powershell 7 and I'm not sure what's going on there.

I also explained this issue in detail here: lwojcik/eleventy-template-bliss#91

Any ideas?

Error when passing arguments to shortcode

I'm unable to pass arguments in the shortcode. I'm using liquid, but also tried njk.

Error:

Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble writing template: "_site/components/index.html" (via EleventyTemplateError)
[11ty] 2. invalid syntax at line 1 col 25:
[11ty]
[11ty]   favicon appleIconBgColor='blue'
[11ty]                           ^, file:C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\src\_includes\head.liquid, line:35, col:5 (via RenderError)   
[11ty] 3. invalid syntax at line 1 col 25:
[11ty]
[11ty]   favicon appleIconBgColor='blue'
[11ty]                           ^ (via Error)
[11ty]
[11ty] Original error stack trace: Error: invalid syntax at line 1 col 25:
[11ty]
[11ty]   favicon appleIconBgColor='blue'
[11ty]                           ^
[11ty]     at Lexer._token (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\moo\moo.js:533:13)
[11ty]     at Lexer.next (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\moo\moo.js:480:19)
[11ty]     at Function.parseArguments (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Engines\Liquid.js:121:21)       
[11ty]     at async Object.render (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Engines\Liquid.js:145:26)
[11ty]     at async TemplateLayout.render (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\TemplateLayout.js:161:25)   
[11ty]     at async Template.renderPageEntry (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Template.js:853:17)      
[11ty]     at async C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Template.js:883:21
[11ty]     at async Promise.all (index 0)
[11ty]     at async Promise.all (index 1)
[11ty]     at async Eleventy.executeBuild (C:\Users\silve\websites\11ty\starters\11ty-cloudcannon-starter\node_modules\@11ty\eleventy\src\Eleventy.js:1003:13)        
[11ty] Wrote 0 files in 0.05 seconds (v1.0.1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.