简体   繁体   English

Webpack输出错误的图像路径

[英]Webpack outputs wrong path for images

I'm building a site with react and webpack. 我正在使用react和webpack构建一个站点。 When I build the app with webpack and try to include images, the images are written to the build folder along with the other assets, but the link to the image that webpack outputs is incorrect. 当我使用webpack构建应用程序并尝试包含图像时,图像将与其他资源一起写入构建文件夹,但是webpack输出的图像链接不正确。 I can go in the build folder and view the image, so it is being copied correctly it is the link that is wrong. 我可以进入构建文件夹并查看图像,因此正确复制它是错误的链接。

my react component looks like this: 我的react组件看起来像这样:

 'use strict'; var React = require('react'); var newsFeedIcon = require('../../img/news-feed-icon.png'); //create story component module.exports = React.createClass({ render: function () { return ( <div className="col_12 footer-right mobile-foot"> <img src={newsFeedIcon} /> <p><a href="/about">About Us</a> | <a href="/contact">Contact Us</a> | <a href="/faq">FAQ</a> | <a href="/media">Media</a> | <a href="#">Privacy Statement</a> | <a href="#">Terms of Service</a></p> </div> ); } }) 

My Webpack configuration looks like this: 我的Webpack配置如下所示:

  webpack: { client: { entry: __dirname + '/app/js/client.jsx', output: { path: 'build/', file: 'bundle.js' }, module: { loaders: [ { test: /\\.jsx$/, loader:'jsx-loader' }, { test: /\\.css$/, loader: "style-loader!css-loader" }, { test: /\\.(eot|woff|woff2|ttf|svg|png|jpg)$/, loader: 'url-loader?limit=30000&name=[name]-[hash].[ext]' }, { test: /\\.jpe?g$|\\.gif$|\\.png$/i, loader: 'file-loader' }, { test: /\\.jpg/, loader: 'file' }] } }, test: { entry: __dirname + '/test/client/test.js', output: { path: 'test/client/', file: 'bundle.js' }, module: { loaders: [ { test: /\\.jsx$/, loader:'jsx-loader' }] } }, karma_test: { entry: __dirname + '/test/karma_tests/test_entry.js', output: { path: 'test/karma_tests/', file: 'bundle.js' }, module: { loaders: [ { test: /\\.jsx$/, loader:'jsx-loader' }] }, background: true } }, 

I've been banging my head against the wall on this since yesterday, so any help would be appreciated. 从昨天开始,我一直在撞墙,所以任何帮助都会受到赞赏。 Let me know you need more info. 让我知道您需要更多信息。

Thanks! 谢谢!

You could try setting the name option for file-loader, as well as output.publicPath . 您可以尝试为file-loader设置name选项,以及output.publicPath

 output: {
     path: 'build/',
     file: 'bundle.js',
     publicPath: '/assets'
}

... ...

{ 
     test: /\.(png|jpg)$/, 
     loader: 'file-loader?name=/img/[name].[ext]' 
}

Then the resolved url in your require will be: 然后,您的要求中已解析的网址将是:

/assets/img/news-feed-icon.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM