background-image URL未使用Webpack加载

时间:2018-02-27 21:54:13

标签: css reactjs webpack webpack-style-loader sass-loader

基本上我试图在容器的背景中显示图像,但它没有加载。我尝试了各种路径,因为我不完全清楚URL需要相对于:package.jsonwebpack.config.js,React组件或login.scss我是工作。

除了webpack.config.js

之外,我还将提供目录结构
/app
    /src
        /assets
            /img
                slide_blue.png
            /scss
                login.scss
        /config
            webpack.config.js
        /react
            /containers
                /authentication
                    signin.js
            index.js // entry point
    package.json

到目前为止,login.scss很短,其他风格正在发挥作用:

// logic.scss
@import 'declarations';

.login {
    height: 381px;
    // tried just about every combination of path... this is just the last one I tried
    background-image: url('/src/assets/img/slide_blue.png') !important;

    form {
        padding-top: 75px;
        padding-bottom: -75px;
    }
}

最后是webpack.config.js

var path = require('path');
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const VENDOR_LIBS = [
  'axios', 'history', 'lodash', 'prop-types', 'react', 'react-dom', 'react-dropzone', 'react-redux', 'react-router-dom', 'react-router-redux', 'redux', 'redux-form', 'redux-thunk', 'reactstrap'
]

module.exports = {
  context: __dirname,

  entry: {
    app: '../react/index',
    vendor: VENDOR_LIBS
  },

  output: {
      path: path.resolve('./src/assets/bundles/'),
      filename: './js/[name].[chunkhash].js'
  },

  module: {
    rules: [
      { 
        loader: 'babel-loader',
        test: /\.js$/, 
        exclude: /node_modules/, 
        query: {
          presets: ["react", "es2015", "stage-1"]
        }
      },
      {
        test: /\.json$/,
        loader: ['json-loader'] 
      },
      {
        test: /\.scss$/,
        loader: ['style-loader', 'css-loader', 'sass-loader'] 
      },
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader']
        })      
      },   
    ],
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'manifest']
    }),
    new BundleTracker({filename: './src/config/webpack-stats.json'}),
    new ExtractTextPlugin({filename: './css/[name].[hash].css', allChunks: true})
  ],
  resolve: {
    extensions: ['*', '.js', '.jsx', '.json', '.gif', '.png'],
  },
}

我在这里想到的是阻止background-image: url(...)显示?

修改

React组件以防万一:

    return (
        <div className='login'>
            <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
                {this.renderAlert()}
                <Field 
                    label='Username'
                    name='username'
                    type='username'
                    component={this.renderField} 
                />
                <Field 
                    label='Password' 
                    name='password'
                    type='password' 
                    component={this.renderField} 
                />
                <div className='row'>
                    <div className='col-sm-2'>
                        <button type='submit' className='btn btn-primary'>Sign in</button>
                    </div>
                    <div className='col-sm-10 pull-right'>
                        <ul className='pull-right signin'>
                            <li><Link to='/auth/username'>Username Recovery</Link></li>                
                            <li><Link to='/auth/password'>Password Reset</Link></li>                                
                            <br />
                            <li><Link to='/auth/new_account'>New Accounts</Link></li>
                        </ul>
                    </div>
                </div>
            </form>
        </div>
    );

编辑2

我将以下内容添加到webpack.config.json

  {
    test: /\.png|jpg$/,
    include: path.join(__dirname, 'assets/img'),
    loader: ['file-loader'] 
  },

并将background-image: url(...)更新为相对于我的入口点index.js

background-image: url('/../assets/img/slide_blue.png');

不会产生错误,也不会加载图像。

尝试以下操作并产生相应的错误:

background-image: url('../assets/img/slide_blue.png');
Module not found: Error: Can't resolve '../assets/img/slide_blue.png' in '/mnt/c/dev/current/app/src/assets/scss'

还给出了一个产生相应错误的镜头:

background-image: url('../../assets/img/slide_blue.png');
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.

所以仍然不确定是什么。我一直在将图像加载到我的组件中,执行以下操作,这让我质疑它是否正确(但这是另一个问题):

import logo from '!!url-loader!../../../assets/img/logo.png';

1 个答案:

答案 0 :(得分:1)

您需要described here

{ 
    test: /\.(png|jpg)$/,
    include: path.join(__dirname, 'assets/img'),
    loader: 'file-loader' 
 }

路径将相对于您的入口点。