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

icons-loader

v0.0.6

Published

Icons loader for webpack

Downloads

13

Readme

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:

import IconsPlugin from 'icons-loader/IconsPlugin'

const RUN_TIMESTAMP = Math.round(Date.now() / 1000)

const webpackConfig = {
  loaders: [{
      test: /\.svg$/,
      loader: 'icons-loader',
  }],
  plugins: [
    new IconsPlugin({
      fontName: 'icons',
      timestamp: RUN_TIMESTAMP,
      normalize: true,
      formats: ['ttf', 'eot', 'woff', 'svg']
    })
  ]
}

Now you can require the icons in your code:

import iconFont from 'icons-loader'
import menu from './menu.svg'

console.log(iconFont) /*
{
  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.log(menu) /*
{
  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 project src
  • Add .svg icons to the icons directory, check out these websites for free and excellent .svg icons:
  • Create an index.js file in the new icons directory:
import menu from './menu.svg'
import cross from './cross.svg'

export default {
  menu: menu,
  cross: cross
}
  • Create an icon component:

src/components/icon.scss

.icon {
  font-weight: normal;
  font-style: normal;
  font-decoration: none;
  text-transform: none;
  vertical-align: middle;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

src/components/icon.js

import React, { Component, PropTypes } from 'react'

import styles from './icon.scss'
import * as icons from 'icons'

export default class Icon extends Component {
  static propTypes = {
    name: PropTypes.string.isRequired
  }

  render () {
    let icon = icons[this.props.name]

    if (icon === undefined) {
      console.warn('Unknown icon: ' + icon)
      return
    }

    return (
      <span
        className={styles.icon}
        style={
          fontFamily: icon.fontName
        }>
        {icon.unicode}
      </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:

import React from 'react'
import { render } from 'react-dom'

...

import styleInject from 'style-inject'
import iconFont from `icons-loader`

const injectIconFont = function () {
  styleInject(iconFont.css)
}

injectIconFont()

...

Options

Plugin

  • filenameTemplate naming options for the font assets
  • filenameTemplate.name the template to use. See loader-utils docs
  • filenameTemplate.regExp the regexp passed to loader-utils

You can also add gulp-iconfont options.

Recommended options


const RUN_TIMESTAMP = Math.round(Date.now() / 1000)
const 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:

module.export = __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 = encodeURIComponent('module.exports = require("react").createElement("span", { className: "icon" }, __ICON__.text);');

const webpackConfig = {
  loaders: [{
      test: /\.svg$/,
      loader: "icons-loader?template=" + iconModuleTemplate,
  }],
  ...
}