npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iconfont-webpack-plugin

v5.0.1

Published

Simple icon font handling for webpack

Downloads

12,385

Readme

Icon Font Webpack Plugin

npm version Dependency Status CI

icon font webpack plugin demo
This plugin tries to keep the usage and maintenance of icon fonts as simple as possible.

a:before {
  font-icon: url('./account.svg');
}

Browser Support: IE9+
Preprocessor Support: All - works with sass, less, stylus, postcss, vanilla css, ...

Requirements

This plugin requires:

  • webpack 3.x or higher
  • postcss-loader 2.x or higher
  • css-loader 3.x or higher
  • node 10 or higher

Installation

npm i --save-dev postcss-loader webpack css-loader
npm i --save-dev iconfont-webpack-plugin

Configuration

All you have to do is to add the plugin to your postcss loader plugins inside your webpack.config.js:

const IconfontWebpackPlugin = require('iconfont-webpack-plugin');

  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'css-loader',
          {
            loader: 'postcss-loader',
            options: {
              postcssOptions: (loader) => {
                return {
                  plugins: [
                    new IconfontWebpackPlugin({
                      resolve: loader.resolve
                    })
                  ]
                };
              }
            }
          }
        ]
      }
    ]
  }

Advanced Configuration

Probably you won't need this but you can also pass some additional options.

|Name|Type|Default|Description| |:--:|:--:|:-----:|:----------| |resolve|{Function}||Required - A function which resolves the svg paths. See resolve| |fontNamePrefix|{String}|''| Allows to prefix the generated font name | |enforcedSvgHeight|{number}|1000| Scales all svg to the given height |

const IconfontWebpackPlugin = require('iconfont-webpack-plugin');

  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'css-loader',
          {
            loader: 'postcss-loader',
            postcssOptions: (loader) => {
              return {
                plugins: [
                  new IconfontWebpackPlugin({
                    resolve: loader.resolve,
                    fontNamePrefix: 'custom-',
                    enforcedSvgHeight: 3000,
                  })
                ]
              };
            }
          }
        ]
      }
    ]
  }

Usage

After setting up the plugin your css has now a new feature:
font-icon declarations

a:before {
  font-icon: url('./account.svg');
  transition: 0.5s color;
}

a:hover:before {
  color: red;
}

and it will be compiled into:

@font-face {
  font-family: i96002e;
  src: url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAAA.....IdAA==") format('woff');
}

a:before {
  font-family: i96002e;
  content: '\E000';
  transition: 0.5s color;
}

a:hover:before {
  color: red;
}

Icon sizes

Shorthand for setting the icon size and the icon:

a:before {
  font-icon: 80% url('./account.svg');
}

SVG Requirements

The main work of the iconfont-webpack-plugin is done by svg2ttf which converts svgs into fonts.
Therefore it inherits all its limitations.

  1. Remove all fills and colors. You can probably leave black fills. In fonts, fill is defined by contour direction. Make sure that you don't have any complex rules like evenodd fills.
  2. Remove all FAT line attributes. This is not supported by Fontello. In fonts, fat lines are drown by 2 nested contours.
  3. Join all contours to a single outline. This is the last and the most important step. Usually editors automatically set the correct contour direction depending on nesting and black fills.

Should you use icon fonts for everything?

No.

Icon fonts are really good for decorative icons (where the icon is purely ornamental and doesn’t incorporate core meaning or functionality).

For critical icons without fallbacks (like a hamburger menu icon) you should rather use SVGs. But also JPEGs, PNGs and even GIFs have their use cases.

Pick the best solution for your problem - there is no silver bullet. With this plugin it is pretty easy to use pixel images, svgs and font-icons side by side.

When should you use icon fonts over pure SVGs?

SVGs have some disadvantages and lack certain features especially when used inside pseudo elements:

What about all the rant on icon fonts?

Like all technologies there are disadvantages when using icon fonts.
We tried to apply best practices to solve the main issues for you.

License

This project is licensed under MIT.