简体   繁体   English

如何在webpack中加载sass样式表中使用的图像?

[英]How can I load image(s) used in sass stylesheet in webpack?

I am using Webpack to bundle my modules and using sass for styling purpose in a react based application. 我正在使用Webpack捆绑我的模块,并在基于反应的应用程序中使用sass进行样式化。

Using a seperate style-sheet I am setting the background-image of a component and loaded that style-sheet in index.js . 使用单独的样式表我正在设置组件的背景图像并在index.js加载该样式表。 All the styles in style-sheet gets applied to that component except background image . 样式表中的所有样式都应用于除背景图像之外的该组件。

Here is my sample style-sheet 这是我的样本样式表

$bg-img: url('/img/bg.jpg');

.show {
    position: relative;
    background: $black $bg-img center center;
    width: 100%;
    height: 100%;
    background-size: cover;
    overflow: hidden;
}

One way to solve that problem would be explicitly require the image and then create inline style for react components. 解决该问题的一种方法是明确要求图像,然后为反应组件创建内联样式。

img1 = document.createElement("img");
img1.src = require("./image.png");

But I want to load all the images from my image folder as soon as my style-sheet loads not by requiring each image separately. 但是,一旦我的样式表加载而不是分别要求每个图像,我想加载我的图像文件夹中的所有图像。

My webpack config file is 我的webpack配置文件是

module.exports = {
  devtool: 'source-map',
  entry: {
    main: [
      'webpack-dev-server/client?http://localhost:8080',
      'webpack/hot/only-dev-server',
      './src/index.js'
    ]
  },
  output: {
    path: path.join(__dirname, 'public'),
    publicPath: '/public/',
    filename: 'bundle.js'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  module: {
   loaders: [
      {
        test: /\.jsx?$/,
        include: path.join(__dirname, 'src'),
        loader: 'react-hot!babel'
      },
      {
        test: /\.scss$/,
        include: path.join(__dirname, 'sass'),
        loaders: ["style", "css?sourceMap", "sass?sourceMap"]
      },
      { 
        test: /\.(png|jpg)$/,
        include: path.join(__dirname, 'img'),
        loader: 'url-loader?limit=10000' 
     } // inline base64 URLs for <=10k images, direct URLs for the rest
    ]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

I may be missing some trivial points. 我可能会遗漏一些微不足道的观点。 Any help is appreciated. 任何帮助表示赞赏。

Ahh.... After a long hours of debugging I have finally found the issue. 啊......经过长时间的调试,我终于找到了问题。 It was my foolishness because of which I struggled. 这是我的愚蠢,因为我挣扎。 I wanted to delete this question but thought that it would help new comers like me when the stuck to similar issues. 我想删除这个问题,但认为当遇到类似问题时,它会帮助像我这样的新人。

Problem was here 问题在这里

loader: 'url-loader?limit=10000'

I was setting the limit to file size of 10kb without really knowing what it does. 我正在设置文件大小限制为10kb而不知道它的作用。 And the image which I was loading was of size 21kb ! 我加载的图像大小为21kb This happens when you copy some else webpack config files :) Sign of javascript fatigue ! 当你复制其他一些webpack配置文件时会发生这种情况:) javascript疲劳的迹象!

When using webpack to load your css and the assets they access, your css needs to follow the same module naming rules you use in your JavaScript import or require calls. 当使用webpack加载你的css及其访问的资产时,你的css需要遵循你在JavaScript import使用的相同模块命名规则或者require调用。

Assuming the img folder is in the root of your source tree, you should refer to it like this in scss: 假设img文件夹位于源树的根目录中,您应该在scss中像这样引用它:

// note there is no leading /, which tells "require()" to start from the root of your source tree
$bg-img: url('img/bg.jpg');

Or just go completely relative. 或者只是完全相对。 Assuming you your source code is like this: 假设你的源代码是这样的:

/src/styles.scss
/img/bg.jpg

Your styles.scss could use a relative path: 您的styles.scss可以使用相对路径:

// note the path is relative to where the style.scss is in your source tree.
$bg-img: url('../img/bg.jpg');

I had similar problem but with images with '.jpeg' extension. 我有类似的问题,但图片的扩展名为“.jpeg”。 Change extension to '.jpg' helped me for some reason. 将扩展名更改为“.jpg”有助于我出于某种原因。

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

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