• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple icon font handling for webpack

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.

More Repositories

1

html-webpack-plugin

Simplifies creation of HTML files to serve your webpack bundles
JavaScript
10,686
star
2

favicons-webpack-plugin

Let webpack generate all your favicons and icons for you
JavaScript
1,200
star
3

html-webpack-harddisk-plugin

Write html files to hard disk even when using the webpack dev server or middleware
JavaScript
226
star
4

cpuprofile-webpack-plugin

Generate a cpuprofile for your webpack builds
TypeScript
153
star
5

next-yak

a streamlined CSS-in-JS solution tailor-made for Next.js, seamlessly combining the expressive power of styled-components syntax with efficient build-time extraction and minimal runtime footprint, ensuring optimal performance and easy integration with existing atomic CSS frameworks like Tailwind CSS
TypeScript
116
star
6

resource-hints-webpack-plugin

Adds automatically resource hints to your html-files
JavaScript
100
star
7

css-variable

Define CSSVariables in JS
JavaScript
89
star
8

text-box-trim-examples

text-box-trim examples and playground (previously known as leading-trim)
TypeScript
52
star
9

next-relay-demo

Relay React Concurrency Example
TypeScript
33
star
10

css-framework-performance

Compare different css approaches
TypeScript
30
star
11

extra-entry-webpack-plugin

Allows to add entry points with a defined output filename.
JavaScript
18
star
12

remote-web-worker

Cross origin web worker
TypeScript
15
star
13

chrome-profile-sourcemap-resolver

Apply sourcemaps for a recorded chrome profile
TypeScript
11
star
14

react-use-ref-effect

an useEffect hook optimized for useRef
TypeScript
10
star
15

storybook-puppeteer

`storybook-puppeteer` is a CLI to run end-to-end tests for storybook stories.
TypeScript
10
star
16

cpuprofile-to-flamegraph

Convert a nodejs .cpuprofile to a renderable flame graph node
TypeScript
10
star
17

svg-placeholder

A fast svg image mock placeholder service which can be used as a stand-alone-server or as a express-middleware
JavaScript
9
star
18

htl-template-loader

Webpack loader for HTL/Sightly data-sly-template templates
JavaScript
8
star
19

webpack-recompilation-simulator

Helper to test and measure webpack recompilations
JavaScript
8
star
20

next-swc-plugin-demo

basic swc wasm demo for nextjs
TypeScript
7
star
21

grunt-githash

Grunt plugin to get the current git hash, branch and tag
JavaScript
6
star
22

stylelint-safari-background-clip

Try to detect possible safari background clip bugs - https://bugs.webkit.org/show_bug.cgi?id=169125
JavaScript
6
star
23

requirejs-modernizr

Attemp to use requirejs with modernizr
JavaScript
6
star
24

css-sourcemaps-webpack-plugin

Simple CSS Sourcemap configuration for CSS, Less, Stylus and Sass
JavaScript
5
star
25

banner-loader

Webpack loader to prefix or postfix files
JavaScript
4
star
26

webpack-dependency-stats

Extracts a flat list of all dependencies and dependents of a given webpack module
JavaScript
4
star
27

postcss-inline

PostCSS plugin that puts fonts / and images as data URIs into your CSS
JavaScript
3
star
28

ariaMenu

Accessible menu with Aria support
JavaScript
3
star
29

handlebars-faker

Small helper to use faker inside your handlebars template
JavaScript
3
star
30

opentype-playground

Playground for openType.js and React
TypeScript
2
star
31

css-exclude

Allows to exclude css with anotations
JavaScript
2
star
32

hot-file-cache

A file cache for which will invalidate automatically if the source changes
JavaScript
2
star
33

mediaqueries-with-sass-modernizr

Example usage of sass with modenizr and media queries
JavaScript
2
star
34

zx-packed

A lightweight version of 🐚 zx
JavaScript
2
star
35

meteor-gpio

Getting and setting GPIO values for Raspberry Pi / Odroid / BeagleBone
JavaScript
2
star
36

auto-peer

An experimental webRTC library based on peer.js - for game and collaboration app prototyping
JavaScript
2
star
37

grunt-wizard

A wizard to select the desired task and options
JavaScript
2
star
38

sync-cpuprofiler

Create a new cpuprofile for the currend node process
JavaScript
1
star
39

layout-recalculate-detector

Experiment to measure layout recalculations during hydration
JavaScript
1
star
40

ng-directive-parser

Extracts angular directive information from source
JavaScript
1
star
41

drupal8-vagrant

Simple Drupal sandbox
Shell
1
star
42

yarn-plugin-leaf

Yarn plugin experiment
TypeScript
1
star
43

clean-commit-gui

Electron git interface
JavaScript
1
star
44

grunt-tpl-wrap

A grunt plugin to wrap file contents with a tpl template
JavaScript
1
star
45

handlebars-to-ecmascript

Convert handlebars to an ecmascript AST
JavaScript
1
star
46

useWorkerPromise

extrem lightweight WebWorker react integration
TypeScript
1
star
47

https-to-http-proxy

Allows to access an insecure HTTPS page over http
JavaScript
1
star
48

terrific-singleton

helper module to use terrific in react projects - still alpha - don't use
JavaScript
1
star