Icons loader (generate iconfonts for webpack)
This is a webpack loader for generating an iconfont from SVG dependencies.
Based on iconfont-loader by Jussi Kalliokoski thanks and <3.
It uses gulp-iconfont to create the font.
Features
- Automatically generates fonts (iconfont) from
.svg
files - Webpack loader + plugin for seamless workflow integration
Installation
npm install icons-loader --save-dev
Basic usage
Add the loader and plugin to your webpack config:
const RUN_TIMESTAMP = Math const webpackConfig = loaders: test: /\.svg$/ loader: 'icons-loader' plugins: fontName: 'icons' timestamp: RUN_TIMESTAMP normalize: true formats: 'ttf' 'eot' 'woff' 'svg'
Now you can require the icons in your code:
console /*{ css: '@font-face(...)', // you could inject this into your body by using style-inject package? fontName: 'icons', glyphs: [1], <generated_icon_id>: { character: 'ea01', fontName: 'icons', unicode: [''] } ...}*/ console /*{ character: 'ea01', fontName: 'icons', unicode: ['']}*/
Example workflow integration
So how can you integrate icons-loader
into your webpack workflow? Here is how I use it:
- Create an
icons
directory in your projectsrc
- Add
.svg
icons to theicons
directory, check out these websites for free and excellent.svg
icons:- Noun Project) (recommended)
- UX Repo
- Create an
index.js
file in the newicons
directory:
menu: menu cross: cross
- Create an
icon
component:
src/components/icon.scss
src/components/icon.js
static propTypes = name: PropTypesstringisRequired { let icon = iconsthispropsname if icon === undefined console return return <span className=stylesicon style= fontFamily: iconfontName > iconunicode </span> }
- Use the icon component in other components like so:
src/components/header.js
<Icon name='icon-file-name' /><Icon name='menu' />
- Finally use style-inject or similar package to inject the css returned from
import iconFont from 'icons-loader'
in your body
The best way to do this is in your main app
component. For example:
... const injectIconFont = { } ...
Options
Plugin
filenameTemplate
naming options for the font assetsfilenameTemplate.name
the template to use. See loader-utils docsfilenameTemplate.regExp
the regexp passed toloader-utils
You can also add gulp-iconfont options.
Recommended options
const RUN_TIMESTAMP = Mathconst iconsPluginOptions = fontName: 'icons' timestamp: RUN_TIMESTAMP normalize: true formats: 'ttf' 'eot' 'woff' 'svg'
Loader
template
The template option of the loader is the template for the module generated by the loader. By default the template is:
moduleexport = __ICON__;
where __ICON__
is an object that has the properties fontName
(the name of the generated font, passed in the plugin options) and text
(a string representation of the character of icon in the font).
This allows you to for example export a React element instead:
const iconModuleTemplate = ; const webpackConfig = loaders: test: /\.svg$/ loader: "icons-loader?template=" + iconModuleTemplate ...