cannot load png files with webpack, unexpected character

后端 未结 4 924
梦谈多话
梦谈多话 2021-02-02 07:25

Im trying to reconfigure my webpack, and now i cannot load the css files. i keep my styles in src > styles > main.css

I am getting errors such as

ERROR          


        
4条回答
  •  一整个雨季
    2021-02-02 08:04

    You can use image-webpack-loader with file-loader https://www.davidmeents.com/blog/how-to-set-up-webpack-image-loader/

    1) install them

    $npm install image-webpack-loader file-loader --save-dev

    2) add to webpack.config.js below babel-loader your new set: image-webpack-loaders and file-loader

    module: {
        loaders: [
          {
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel-loader',
            query: {
              presets: ['react', 'es2015', 'stage-0'],
              plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
            }
          }, 
          {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loaders: [
              'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
              'image-webpack-loader?bypassOnDebug&optimizationLevel=7&interlaced=false'
            ]
          } 
        ]
      },
    

    3) Import your .jpg file into variable (for me it is 'imgabout' variable) and add this variable into src

    import React from 'react';
    import imgabout from './img-about.jpg';
    export default class Article extends React.Component {
    render() {
    
        const { title } = this.props; 
    
        return (
          
    Logo
    );}}

提交回复
热议问题