【问题标题】:Webpack outputs wrong path for imagesWebpack 输出错误的图片路径
【发布时间】:2016-01-19 12:15:43
【问题描述】:

我正在使用 react 和 webpack 构建一个站点。当我使用 webpack 构建应用程序并尝试包含图像时,图像会与其他资产一起写入构建文件夹,但 webpack 输出的图像链接不正确。我可以进入构建文件夹并查看图像,所以它被正确复制它是错误的链接。

我的反应组件是这样的:

'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>
      );
  }
})

我的 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
      }
    },

从昨天开始,我一直在努力解决这个问题,因此我们将不胜感激。让我知道您需要更多信息。

谢谢!

【问题讨论】:

    标签: javascript image reactjs webpack


    【解决方案1】:

    您可以尝试为文件加载器设置 name 选项,以及output.publicPath

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

    ...

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

    那么在你的需求中解析的 url 将是:

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

    【讨论】:

    • 在此处添加投票,因为建议的解决方案实际上解决了我的问题 - 尚未根据实际问题对此进行检查 - 这是 OP 的责任。
    • 感谢它有效。像我这样使用 django 后端的人的提示,publicPath 需要是 /static/bundles/
    • 这是怎么做到的?当我尝试放置 path: "dist/" webpack a 时抱怨它不是绝对路径
    • @PositiveGuy 好吧,dist/ 不是绝对路径。绝对路径总是以 / 开头
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2019-04-19
    • 2018-09-10
    相关资源
    最近更新 更多