困扰一段时间的图片打包问题:
1、在react组件里的src:
<img src={require('../../images/test2.png')} alt=""/>
2、在css里的图片引用:
background: url("images/test3.png") 0 0 no-repeat;
webpack配置图片打包:
{ test: /.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2)(?.*)?$/, loader: 'file-loader', query: { name: isDebug ? '[path][name].[ext]?[hash:8]' : '[hash:8].[ext]', }, }
webpack里配置文件输出:
output: { path: path.resolve(__dirname, '../build/public/assets'), publicPath: '/assets/', sourcePrefix: ' ', pathinfo: isVerbose, }
只要loaders配置和output配置正确了,路径就不是问题了。