1 Star 1 Fork 0

hzsrc/webpack-iconfont-plugin-nodejs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

webpack-iconfont-plugin-nodejs

Use svg files to build iconfont files(ttf,woff2,woff,eot,svg), css file, js data file and html-preview file.

Features:

  • Iconfont output formats: WOFF2, WOFF, EOT, TTF and SVG.
  • Generates CSS file, js data file, html-preview file.
  • Supports webpack and hot reloading at devlopment time. Iconfont files and css file will be rebuilt while any svg file changed or added, then page will refresh.
  • Supports running by nodejs directly.
  • Fixed output files with the same svg files. It means that if the svg files are not changed, all the output files(ttf,woff,css...) will not change, even though you build them again.
  • High iconfont precision.

Test for preview:

git clone https://github.com/hzsrc/webpack-iconfont-plugin-nodejs.git
cd webpack-iconfont-plugin-nodejs
npm install
npm run dev

Or visit this preview: http://test.hz300.com/webpack4/iconfontPreview.html

Install:

npm install webpack-iconfont-plugin-nodejs

Usage:

You can use it by nodejs directly or use it in webpack

1. Use by nodejs directly:

build/svg2font.js:

var WebpackIconfontPluginNodejs = require('webpack-iconfont-plugin-nodejs');
var path = require('path');
var dir = 'test/web_project/'
var options = {
  fontName: 'my-icons',
  cssPrefix: 'ico',
  svgs: path.join(dir, 'svgs/*.svg'),
  // template: path.join(dir, 'css.njk'),
  // htmlTemplate: path.join(dir, 'html.njk'),
  fontsOutput: path.join(dir, 'fonts/'),
  cssOutput: path.join(dir, 'fonts/font.css'),
  htmlOutput: path.join(dir, 'fonts/_font-preview.html'),
  jsOutput: path.join(dir, 'fonts/fonts.js'),
  // formats: ['ttf', 'woff', 'svg'],
};

new WebpackIconfontPluginNodejs(options).build()

Then you can run this command to build iconfont by svg:

node build/svg2font.js

Or you can set this command to script of package.json, and run it by npm.

2. Use by webpack:

var WebpackIconfontPluginNodejs = require('webpack-iconfont-plugin-nodejs');
var dir = 'test/web_project/'

module.exports = {
    //... others
    plugins: [
        new WebpackIconfontPluginNodejs({
          fontName: 'my-icons',
          cssPrefix: 'ico',
          svgs: path.join(dir, 'svgs/*.svg'),
          // template: path.join(dir, 'css.njk'),
          fontsOutput: path.join(dir, 'fonts/'),
          cssOutput: path.join(dir, 'fonts/font.css'),
          htmlOutput: path.join(dir, 'fonts/_font-preview.html'),
          jsOutput: path.join(dir, 'fonts/fonts.js'),
          namesOutput: path.join(dir, 'fonts/names.txt'),
          // formats: ['ttf', 'woff', 'svg'],
        }),
    ]
};

Options

svgs (required)

Type: String | [String] | [{ fileName:String, svgContent:String }, ...]
File path(s) or glob(s) to svg icons. Recommend to use *.svg like this: svgs: '/src/project/src/*.svg', this can watch svgs by a directory.

Or array of svg-data with fileName(must be *.svg) and svgContent like this:

svgs: [
    {
        fileName: 'my-svg1.svg',
        svgContent: '<svg xmlns="http://www.w3.org/2000/svg">......</svg>'
    },{
        fileName: 'mySvg2.svg',
        svgContent: '<svg xmlns="http://www.w3.org/2000/svg">......></svg>'
    }
]

fontsOutput (required)

Type: String
Destination for generated font files (directory).

cssOutput (required)

Type: String
Destination for generated css file (file name).

fontName

Type: String
Default value: iconfont
The font family name (e.g. font-family: 'iconfont').

htmlOutput

Type: String
Default value: [path of cssOutput] + /font-preview.html. Or false value.
Destination for generated html-preview file (file name). If false, no html and js output.

template

Type: String
Default value: css
Type of built in style templates ('css', 'scss', 'scss-mixins') or full path to custom template.

htmlTemplate

Type: String
Default value: templates/html.njk Full path to custom html template.

formats

Type: Array of String
Default value: ['svg', 'ttf', 'eot', 'woff2', 'woff']
The output iconfont formats.

cssPrefix

Type: String
Default value: fontName
Css className prefix.

jsOutput

Type: String
Default value: undefined.
Path of a js file which contains all svg contents. Optional.

namesOutput

Type: String
Default value: undefined.
Path of a txt file which contains all icon names. c

jsPrefix

Type: String
Default value: '/* eslint-disable */\n'
Js file content prefix.

cssFontPath

Type: String
Default value: path.relative(path.dirname(options.cssOutput), options.fontsOutput);
Font url path in cssOutput file.

glyphTransformFn

Type: Function
Default value: undefined;
A function to modify glyphData.metadata. Optional.

notWatchFile

Type: boolean
Default value: false;
Do not watch svgs files. Optional.

Other options for advanced

Other options such as startUnicode, prependUnicode etc. in defaultOptions.js,
Please refer to:
https://www.npmjs.com/package/svgicons2svgfont
https://www.npmjs.com/package/svg2ttf
https://www.npmjs.com/package/ttf2eot
https://www.npmjs.com/package/ttf2woff
https://www.npmjs.com/package/ttf2woff2

The MIT License (MIT) Copyright (c) 2017 Profound Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Use svg files to build iconfont files(ttf,woff2,woff,eot,svg), css file, js data file and html preview file. Run by nodejs or webpack. 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/hzsrc/webpack-iconfont-plugin-nodejs.git
git@gitee.com:hzsrc/webpack-iconfont-plugin-nodejs.git
hzsrc
webpack-iconfont-plugin-nodejs
webpack-iconfont-plugin-nodejs
master

搜索帮助