目录:(packages里是对应的所有源svg文件,lib里是所有svg对应的Vue组件。通过loaders 里的svgToComponents.js对应的node方法给每一个svg文件在lib下生成对应格式的vue文件以及index.js文件。)
svgToComponents.js:
var fs = require('fs');
var path = require('path');
var util = require('util');
var filePath = path.resolve('./packages/icons');
var template = require('lodash');
var fileList = [];
//调用文件遍历方法
fileDisplay(filePath);
// 生成index.js文件
var indexContent = fileList.map(function(d) {
return `import ${d} from './${d}.vue';\n`
}).join('')+"\n const components = ["+ fileList.join(',\n')+"];\n const install = function(Vue){components.forEach(component => {Vue.component(component.name, component);});}\n"+
"export default {install,"+ fileList.join(',\n')+"}"
fs.writeFileSync(path.resolve(__dirname, `../lib/index.js`), indexContent);
//examples中本地页面显示所有图标
var svgVue = "\n return ` }).join('')+"\n
`
body{margin: 0;width: 100%;}
.container{
flex-wrap: wrap;
display: flex;
}
.master-icon {width: 140px; height: 120px; color: #666; font-size: 24px;
display: flex;
align-items: center;
flex-direction: column;
}
.master-icon aside {text-align: center; font-size: 12px; color: #99a9bf;margin-top: 30px;}
`;
fs.writeFileSync(path.resolve(__dirname, `../examples/components/svgDemo.vue`), svgVue);
//文件遍历方法
function fileDisplay(filePath) {
//根据文件路径读取文件,返回文件列表
let files = fs.readdirSync(filePath);
//遍历读取到的文件列表
files.forEach(function (filename) {
//获取当前文件的绝对路径
var filedir = path.join(filePath, filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
var stats = fs.statSync(filedir);
var isFile = stats.isFile();//是文件
var isDir = stats.isDirectory();//是文件夹
if (isFile) {
var fileName = filedir.match(/([^\/\.]*)\..*$/)[1];
var pattern = /-([a-z0-9])/g;
fileName = fileName.replace(pattern,function(all,letter){
return letter.toUpperCase();
});
fileName = fileName.charAt(0).toUpperCase() + fileName.slice(1);
if(fileName){
// 读取文件内容
var svgContent = fs.readFileSync(filedir, 'utf-8');
var render = template(`
${svgContent
.replace(/
.replace(/
.replace('