Import image using this webpack config react

Hey, I hope you can help me out with this issue.

How can I import an image using this webpack config?

I get the following error in the console after I build the app yarn build. How can I add the image to the dashboard file from the public folder? (please see image). Thanks in advance.

const webpack = require('webpack');
    const path = require('path');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const CopyWebpackPlugin = require('copy-webpack-plugin');

    const extractCSS = new ExtractTextPlugin('[name].fonts.css');
    const extractSCSS = new ExtractTextPlugin('[name].styles.css');

    const BUILD_DIR = path.resolve(__dirname, 'build');
    const SRC_DIR = path.resolve(__dirname, 'src');

    console.log('BUILD_DIR', BUILD_DIR);
    console.log('SRC_DIR', SRC_DIR);

    module.exports = (env = {}) => {
      return {
        entry: {
          index: [SRC_DIR + '/index.js']
        },
        output: {
          path: BUILD_DIR,
          filename: '[name].bundle.js',
        },
        // watch: true,
        devtool: env.prod ? 'source-map' : 'cheap-module-eval-source-map',
        devServer: {
          contentBase: BUILD_DIR,
          //   port: 9001,
          compress: true,
          hot: true,
          open: true
        },
        module: {
          rules: [
            {
              test: /\.(js|jsx)$/,
              exclude: /node_modules/,
              use: {
                loader: 'babel-loader',
                options: {
                  cacheDirectory: true,
                  presets: ['react', 'env']
                }
              }
            },
            {
              test: /\.html$/,
              loader: 'html-loader'
            },
            {
              test: /\.(scss)$/,
              use: ['css-hot-loader'].concat(extractSCSS.extract({
                fallback: 'style-loader',
                use: [
                  {
                    loader: 'css-loader',
                    options: {alias: {'../img': '../public/img'}}
                  },
                  {
                    loader: 'sass-loader'
                  }
                ]
              }))
            },
            {
              test: /\.css$/,
              use: extractCSS.extract({
                fallback: 'style-loader',
                use: 'css-loader'
              })
            },
            {
              test: /\.(png|jpg|jpeg|gif|ico)$/,
              use: [
                {
                  // loader: 'url-loader'
                  loader: 'file-loader',
                  options: {
                    name: './img/[name].[hash].[ext]'
                  }
                }
              ]
            },
            {
              test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
              loader: 'file-loader',
              options: {
                name: './fonts/[name].[hash].[ext]'
              }
            }]
        },
        plugins: [
          new webpack.HotModuleReplacementPlugin(),
          new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
          new webpack.NamedModulesPlugin(),
          extractCSS,
          extractSCSS,
          new HtmlWebpackPlugin(
            {
              inject: true,
              template: './public/index.html'
            }
          ),
          new CopyWebpackPlugin([
              {from: './public/img', to: 'img'}
            ],
            {copyUnmodified: false}
          )
        ]
      }
    };

Hi!

All I see is some JavaScript code. No errormessage and no elixir code.

Would you mind to elaborate?

1 Like

Hi @NobbZ

You are right. I am receiving this error when I try to import some images.

GET server:/img/logowebpng.a961dfbf36f110092c3f2d35778fa3db.png 404 (Not Found)

My question is, how can we import the images that are in the public folder to the components?

An example would be the .../logowebpng.png seeing in the picture above.

Again, can you please explain, how this iss related to elixir?

1 Like