问题1
转载自 webpack打包font时错误css文件路径导致报错 - 简书
根本原理请看:got wrong bootstrap font path after building · Issue #166 · vuejs-templates/webpack · GitHub
在我的项目中遇到的情形是:
打包后,css里加载的font文件路径变成了:/static/css/static/fonts/element-icons.535877f.woff,
而期望的应该是/static/fonts/element-icons.535877f.woff。
修改方式:在build/utils的ExtractTextPlugin.extract里加上 publicPath: ‘…/…/’:
// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
const loaders = [cssLoader]
if (loader) {
loaders.push({
loader: loader + '-loader',
options: Object.assign({}, loaderOptions, {
sourceMap: options.sourceMap
})
})
}
// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
fallback: 'vue-style-loader',
publicPath: '../../' //加我叫我加我加我加我
})
} else {
return ['vue-style-loader'].concat(loaders)
}
}
问题2
更改config/index.js中生产模式下(build)的assetsPublicPth, 原本为 /, 改为 ./ 。
build: {
index: path.resolve(__dirname, '../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './', //改这里
....
}