项目场景:
使用vue2+webpack3.6.0搭建的项目
问题描述:
使用npm run build打包后没有出现favicon.ico文件
解决方法:
1)刚开始看到在webpack.dev.conf.js的配置的new HtmlWebpackPlugin使用了favicon: path.resolve('./favicon.ico'),但未生效
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true,
favicon: path.resolve('./favicon.ico')
}),
]
然后使用new CopyWebpackPlugin方法配置from: 'favicon.ico', to: 'favicon.ico'解决,在webpack.dev.conf.js和webpack.prod.conf.js都加入解决
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: true,
favicon: path.resolve('./favicon.ico')
}),
new CopyWebpackPlugin([{
from: 'favicon.ico', to: 'favicon.ico'
}])
]
2)后面发现是webpack.prod.conf.js加入favicon: path.resolve('./favicon.ico')也能解决
plugins:[
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
favicon: path.resolve('./favicon.ico'), //未加入的
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
})
]