const initValue = 0xe000 // 相当于iconfont的Unicode
const font = fontCarrier.create()
const char = String.fromCharCode(initValue)
font.setSvg(char,res)
font.output({
path: '输出地址'
})
// iconfont 模板
let content = `
@font-face {
font-family: 'iconfont';
src: url('iconfonts.eot'); /* IE9*/
src: url('iconfonts.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('iconfonts.woff') format('woff'), /* chrome、firefox */
url('iconfonts.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/
url('iconfonts.svg#uxiconfont') format('svg'); /* iOS 4.1- */
}
.iconfont {
font-family: "iconfont";
font-size: 16px;
font-style: normal;
}
`
const value = '\\' + initValue.toString(16).toUpperCase()
const name = className // icon的className
content += `
.icon-${name}::before {
content:'${value}'
}
3.彩色图标(其实就是把svg 放到了html中,利用id取值)
cheerio包nodejs中的JQ
const $ = cheerio.load('');
const file = item.icon
const svgNode = $(file);
const symbolNode = $(" ");
symbolNode.attr("viewBox", svgNode.attr("viewBox"));
symbolNode.attr("id", item.className);
symbolNode.append(svgNode.html());
$('svg').append(symbolNode+'\n');
createjs($.html("svg"))
const createjs = (symbol) =>{
const js = `(function (win){
let listenerDOM = null;
const insertBefore = function (a, l) {
l.parentNode.insertBefore(a, l)
}
const complete = function () {
Loaded()
}
const Loaded = ()=>{
let contnet = document.createElement('div')
contnet.innerHTML =` + '`'+`${symbol}`+'`' + '\n' +
`symbol = null
contnet = contnet.getElementsByTagName('svg')[0]
contnet.setAttribute('aria-hidden', 'true')
contnet.style.position = 'absolute'
contnet.style.width = 0
contnet.style.height = 0
contnet.style.overflow = 'hidden'
contnet = contnet
const firstChild = document.body.firstChild
if(firstChild){
insertBefore(contnet, document.body.firstChild)
}else{
document.body.appendChild(contnet)
}
}
document.addEventListener
?
~ ["complete", "loaded", "interactive"].indexOf(document.readyState)
?
setTimeout(Loaded, 0)
:
(
listenerDOM = function() {
document.removeEventListener("DOMContentLoaded", listenerDOM, !1),
Loaded()
},
document.addEventListener("DOMContentLoaded", listenerDOM, !1)
)
:
document.attachEvent && (window.document.onreadystatechange = function() {
"complete" == window.document.readyState && (window.document.onreadystatechange = null, complete())
})
})(window)`
fs.writeFileSync(path.join(outputPath,'/iconfonts/iconfont.js'),js)
}
project_id: 属性那么项目,传项目id,
name: 需要在前端界面中展示图标的名称,
className: 用户使用className, 使用的iconfont,
unicode: 为了迁移之前的iconfont做准备
icon: icon字符串svg