Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May Webpack process all images in a directory without requiring them?

I am creating a website with ReactJS and using Webpack to bundle it.

One of my components is just a column with images (logos). There are about 15 images to be randomly displayed now, but this number will surely increase with time.

Of course it would be really bad if I hd to hardcode all them, like:

import image01 from './logos/01.png';
import image02 from './logos/02.png';
import image03 from './logos/03.png';
.
.
.

It would imply in changing the code every time I had to add a new image.

I'm doing things this way:

logos.jsx

import React from 'react';
import './logos.css';

export default class Logos extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      logos: null
    }
  }

  componentWillMount() {
    fetch('/random_logos')
    .then(res => res.json())
    .then(json => {
      this.setState({
        logos: json.logos
      });
    });
  }

  render() {
    return (
      <div className="logos">
        // code to display the logos in a column
      </div>
    );
  }
}

This fetches a random list of the logos available from the back-end and makes it avalable to the component.

It happens that my code works when I just do npm start, but when I bundle everything with Webpack my files under my/application/dir/src/logos/logos are not moved by Webpack to the correct directory, which would be my/application/dir/resources/images/logos, because they weren't imported!

Is there a way to tell Webpack to process files under a certain directory without requiring them in our code specifically? Or I would have to create a scritp to copy these files to my production directory?

This is my Webpack configuration:

webpack.config.js

var path = require('path');

module.exports = {
    entry: path.resolve(__dirname, 'fe/src/') + '/index.jsx',
    output: {
        path: path.resolve(__dirname, 'public/js'), 
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: [ "node_modules" ],
                loader: 'babel-loader',
                query: {
                    presets: ['es2015', 'react','stage-0']
                }
            },
            {
                test: /\.css$/, loader: "style-loader!css-loader"
            },
            {
                test: /\.(jpe?g|png|gif|bmp|svg|ico)$/i,
                use: 'file-loader?name=[name].[ext]&outputPath=resources/images/'
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                loader: 'file-loader?name=resources/fonts/[name].[ext]'
            }
        ]
    }
}

If I had five files named 01.png, 02.png, 03.png, 04.png and 05.png, my back-end would return a JSON like this:

{
  "logos": [
    "05.png",
    "03.png",
    "01.png",
    "02.png",
    "04.png"
  ]
}
like image 561
Ed de Almeida Avatar asked Oct 16 '25 17:10

Ed de Almeida


1 Answers

I'll suggest to use https://github.com/kevlened/copy-webpack-plugin. Just add the plugin to your webpack config like:

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  ...,
  plugins: [
    new CopyWebpackPlugin([
        { from: '...', to: '...' }
    ])
  ]
}

Then simply reference your logos from the correct folder.

like image 145
Krasimir Avatar answered Oct 19 '25 07:10

Krasimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!