gulp iconfont
参考如下网站
https://github.com/hjzheng/CUF_meeting_knowledge_share/tree/master/2015-7-24/gulp-test-iconfont
gulpfile.js
var gulp = require('gulp');
var iconfont = require('gulp-iconfont');
var iconfontCss = require('gulp-iconfont-css');
var template = require('gulp-template');
var fs = require('fs');
var icons = fs.readdirSync('src/style/svg');
icons = icons.map(function(icon) {
return icon.replace(/\.\w+$/, '');
});
var cssName = 'iconfont'; //生成的css名称
gulp.task('icon', function() {
let dirname = 'src/style';
return gulp
.src([`${dirname}/svg/*.svg`])
.pipe(
iconfontCss({
fontName: 'iconfont', // iconfont.css的font-family值
path: `${dirname}/template/iconfont.template.less`, //css模版文件
targetPath: `../${cssName}.css`, //css生成文件
fontPath: 'fonts/' //iconfont.template.less文件中的fontPath
})
)
.pipe(
iconfont({
fontName: 'iconfont',
formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
normalize: true
})
)
.pipe(gulp.dest(`${dirname}/fonts`)); //svg的字体文件存放位置
});
gulp.task('example', function() {
let dirname = 'src/style';
gulp.src(`${dirname}/example/index.html`) //样式例子文件
.pipe(template({ icons: icons, cssName: cssName }))
.pipe(gulp.dest(`${dirname}`)); //样式例子文件存放位置
});
gulp.task('default', ['icon', 'example']);
iconfont-template.less
@font-face {
font-family: "<%= fontName %>";
src: url('<%= fontPath %><%= fontName %>.eot');
src: url('<%= fontPath %><%= fontName %>.eot?#iefix') format('eot'),
url('<%= fontPath %><%= fontName %>.woff') format('woff'),
url('<%= fontPath %><%= fontName %>.ttf') format('truetype'),
url('<%= fontPath %><%= fontName %>.svg#<%= fontName %>') format('svg');
font-weight: normal;
font-style: normal;
}
.icon:before {
font-family: "<%= fontName %>";
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-style: normal;
font-variant: normal;
font-weight: normal;
/* speak: none; only necessary if not using the private unicode range (firstGlyph option) */
text-decoration: none;
text-transform: none;
}
<% _.each(glyphs, function(glyph) { %>
.icon-<%= glyph.fileName %>:before {
/* content: "\<%= JSON.stringify(glyph) %>";*/
content: "\<%= glyph.codePoint %>";
}
<% }); %>
example/index.html
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>icon font test</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<%= cssName%>.css">
<style>
.icon_lists li{
float:left;
width: 120px;
height:170px;
text-align: center;
list-style: none;
}
.icon_lists .icon{
font-size: 42px;
margin: 10px 0;
color:#333;
-webkit-transition: font-size 0.25s ease-out 0s;
-moz-transition: font-size 0.25s ease-out 0s;
transition: font-size 0.25s ease-out 0s; }
.icon_lists .icon:hover{
font-size: 120px;
}
.name {
font-size: 16px;
} .clear:after {
content: '\20';
display: block;
height: 0;
clear: both;
}
</style>
</head> <body>
<h2>Icon Font</h2>
<ul class="icon_lists clear">
<% _.each(icons, function(icon) { %>
<li class="icon icon-<%= icon %>">
<div class="name"><%= icon%></div>
</li>
<% }); %>
</ul>
<div>
<h2>Use Icon Font</h2>
<pre>
<span class="icon icon-add"></span>
</pre>
</div>
</body>
</html>
执行语句
gulp icon
gulp example
与
gulp.task('example', function() {....
中的task值相同
gulpfile.js文件中增加
=====缺点
生成的font字体 1.只能是黑白色调, 2.svg图片中如果有线条重合,就会被重叠消除为白色【这个问题在icomoon网站中也存在】
=====总结
https://icomoon.io/app/ 这个网站生成font字体,还是不错的 【第一种缺点是没有颜色】 第二种生成iconfont方式【可以有颜色】 gulpfile.js
var gulp = require('gulp');
var rename = require('gulp-rename');
var iconfont = require('gulp-iconfont');
var consolidate = require('gulp-consolidate');
var template = require('gulp-template');
var fs = require('fs');
var icons = fs.readdirSync('src/style/svg');
icons = icons.map(function(icon) {
return icon.replace(/\.\w+$/, '');
});
gulp.task('icon', function() {
let dirname = 'src/style';
return gulp
.src([`${dirname}/svg/*.svg`])
.pipe(
iconfont({
fontName: 'iconfont1',
formats: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
normalize: true,
options: {
fixedWidth: false,
normalize: false,
// fontHeight: 512,
// descent: -32
},
// prependUnicode: true // 会修改掉svg的名字
})
)
.on('glyphs', function(glyphs, options) {
gulp.src(`${dirname}/iconfont.template.1.less`)
.pipe(
consolidate('lodash', {
glyphs: glyphs.map(function(glyph) {
glyph.unicode = glyph.unicode[0]
.charCodeAt(0)
.toString(16)
.toUpperCase(); // unicode是16进制的
return glyph;
}),
fontName: options.fontName,
// glyphs: glyphs,
fontPath: 'fonts/'
})
)
.pipe(rename(`${options.fontName}.css`))
.pipe(gulp.dest(dirname));
})
.pipe(gulp.dest(`${dirname}/fonts`));
});
gulp.task('example', function() {
let dirname = 'src/style';
gulp.src(`${dirname}/example/index.1.html`)
.pipe(template({ icons: icons }))
.pipe(gulp.dest(`${dirname}`));
});
gulp.task('default', ['icon', 'example']);
iconfont-template.less
中循环语句修改
<% _.each(glyphs, function(glyph) { %>
.icon-<%= glyph.name %>:before {
/* content: "\<%= JSON.stringify(glyph) %>";*/
content: "\<%= glyph.unicode %>";
<%= glyph.color?'color:'+glyph.color :null %>
}
<% }); %>
gulp iconfont的更多相关文章
- webapp项目前端总结
提纲 整体把握,从设计稿入手——技术选型 并行开发,从实现静态页面开始 前端自动化 前端js逻辑 前后端集成 小问题集合 总结 1.整体把握,从设计稿入手 —— 技术选型 新项目到手,算是运气好,设计 ...
- 前端自动化构建工具gulp记录
一.安装 1)安装nodejs 通过nodejs的npm安装gulp,插件也可以通过npm安装.windows系统是个.msi工具,只要一直下一步即可,软件会自动在写入环境变量中,这样就能在cmd命令 ...
- 前端构建工具gulp使用
前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中文文档 https://g ...
- 前端们,gulp该用起来了,简单的demo入门——gulp系列(一)
gulp.grunt前端自动化工具,只有用过才知道多么重要. 它们问世这么久了?你真的用过吗? 写个简单的less.watch任务的demo分享———— 1.准备: 安装全局node.npm,这个教程 ...
- (转载)前端构建工具gulp使用
前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中 ...
- gulp编译less简单demo
写个简单的less.watch任务的demo分享———— 1.准备: 安装全局node.npm,这个教程很多不作详细介绍: 安装全局gulp npm install -g gulp 新建getstar ...
- nodejs iconfont处理
做前端优化,iconfont可以替换掉很多图片,减少请求,并有很好的兼容性,颜色大小也有很好的自由度.现在网上已经有很多公开的iconfont供我们使用.但是每个项目有不同的应用场景,网上的并不能满足 ...
- 前端构建工具gulp
前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...
- gulp生成发布包脚本
var formPost = require('./tools/submit.js');var gulp = require('gulp'), zip = require('gulp-zip'), h ...
随机推荐
- java并发中ExecutorService的使用
文章目录 创建ExecutorService 为ExecutorService分配Tasks 关闭ExecutorService Future ScheduledExecutorService Exe ...
- 重识TCP/IP协议族与HTTP基础
不忘初心 砥砺前行, Tomorrow Is Another Day ! 本文概要: TCP/IP协议族的网络分层 TCP三次握手四次挥手 Http简介 报文结构 Http的请求方法及状态码 常用的H ...
- [软件共享]将数据库中的数据导出为SQL脚本
可以直接将数据库中的数据导出为脚本,并可以自己设置过滤条件.使用方法很简单,不在多说了.下面是软件截图.123 下载:http://files.cnblogs.com/pw/mssql2.rar
- vue-cli3.0 gui初体验
为什么80%的码农都做不了架构师?>>> 介绍 新版的vuecli3.0提供了一个vue ui这个命令,这个命令是做什么的呢,这里引用官网的一段介绍 vue ui 你可以通过 v ...
- webpack4.x开发环境配置
写这篇文章的初衷在于,虽然网络上关于webpack的教程不少,但是大多已经过时,由于webpack版本更新后许多操作变化很大,很多教程的经验已经不适合.当我们使用npm安装webpack时,若不指定w ...
- Ansible安装部署
Ansible安装部署 Ansible是一种集成IT系统的配置管理, 应用部署, 执行特定任务的开源平台. 它基于Python语言实现, 部署只需在主控端部署Ansible环境, 被控端无需安装代理工 ...
- 网速慢?NO可能是路由器的原因?
先排除DNS的问题:看这个! 为什么我家300M的网,而且wifi信号满格,还是网速很慢? 这时候不排除是路由器的原因! 第一步首先我们要知道自己家的网关IP: 什么是网关? 网关(Gateway)又 ...
- P3306 [SDOI2013]随机数生成器(bzoj3122)
洛谷 bzoj 特判+多测真恶心 . \(0\le a\le P−1,0\le b\le P−1,2\le P\le 10^9\) Sample Input 3 7 1 1 3 3 7 2 2 2 0 ...
- VMware15.5.0安装MacOS10.15.0系统 安装步骤(上)
VMware15.5.0安装MacOS10.15.0系统安装步骤(上)超详细! 说明: 本文是目前最新的安装和调配教程且MacOS10.15和10.16版本搭建方法相同,我也会在一些细节地方加上小技巧 ...
- 在html中使用vue组件
最近在维护公司的项目,当我拿到项目时,发现他用了vue. 但是~~仅仅是引用vue.js文件,整体的架构还是html那种,没有用webpack! 当时觉得~哇~原来还可以这样! 然后了解了业务逻辑和代 ...
