问题:
类型:.ttf, .woff, .svg等等
打包前文件为**/public/style/xx.css**,打包后icon资源,打包后生成icon图标资源访问路径:/static/css/static/fonts/,为404;
正确的资源访问路径**/static/fonts/**;
解决方案:
vue-cli2:
// utils文件
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath: '../../', // 打包后生成的css文件=》icon地址
})
} else {
return ['vue-style-loader'].concat(loaders)
}
// config/index.js文件,修改assetsPublicPath: './'
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './', // 解决打包完路径不对的问题
...
}
// index: 模板
// assetRoot: 打包后文件要存放的路径
// assetsSubDirectory: 除了 index.html 之外的静态资源要存放的路径,
// assetsPublicPath: 代表打包后,index.html里面引用资源的的相对地址
vue-cli3
// vue.config.js文件
module.exports = {
publicPath: './',
}
// webpack4需要在rule内配置
const mode = process.env.NODE_ENV === 'development'; // 正式版环境
let miniCssExtractPluginOption = {};
if (!mode) {
miniCssExtractPluginOption = {
publicPath: '../../'
};
}
plugins:[
...
new MiniCssExtractPlugin({
filename: "static/css/style.css",
chunkFilename: "static/css/[name].[hash:5].css"
}),
...
],
module: {
rules:[
...
{
test: /\.(sa|sc|c)ss$/,
use: [{
loader: devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
options: miniCssExtractPluginOption
},
'css-loader',
'sass-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf|otf|svg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 10000,
name: 'static/fonts/[hash:8].[name].[ext]'
}
}]
},
...
]