webpack使用
const path = require('path');
const handleFontWay = require('./handle-font-way')
module.exports = {
...,
configureWebpack:{
...,
plugins:[
new handleFontWay({
replaceStr:'../fonts/element-icons',
tragetStr:'/sys/assets/vue/fonts/element-icons'
})
]
},
...
}
插件编写
const fs = require('fs')
const path = require('path')
class handleFontWay {
constructor(options){
this.options = options
}
apply(compiler){
compiler.hooks.afterEmit.tap("handleFontWay",(compilation)=>{
const {assets } = compilation;
Object.keys(assets).forEach(filename=>{
const filePath = assets[filename].existsAt;
if(path.extname(filePath) === '.css'){
let content = fs.readFileSync(filePath,'utf-8');
content = content.replace(new RegExp(this.options.replaceStr,'g'),this.options.tragetStr);
fs.writeFileSync(filePath,content);
}
})
})
}
}
module.exports = handleFontWay;