5

I'm trying to import (move) my image folder from my src folder to my dist folder with Webpack 4 but unlike Gulp, this task looks SO complicated in Webpack even when I'm not even trying to mess around with the images at the moment, I just want Webpack to load them and put them on my dist/image folder.

I've seen that people import image by image on their index.js which I refuse to believe is the only solution, is there a way to just move the whole folder to the dist / production folder without having to import file by file on the index.js ?

This is my webpack config at the moment:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackMd5Hash = require('webpack-md5-hash');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = {
    entry: path.resolve(__dirname, 'src/js/scripts.js'),
    output: {
        path: path.resolve(__dirname,'dist'),
        filename: '[name].[chunkhash].js'
    },
    module: {
        rules: [
            //JS
            {
                //Tipo de Archivo quiero reconocer
                test: /\.js$/,
                exclude: /node_modules/,
                //Que Loader se encarga del tipo de extension de archivo
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets:['babel-preset-env']
                    }
                },
            },           
            //IMAGES
            {
                test: /\.(jpg|png|gif)$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        outputPath: 'images/',
                        name:'[name][hash].[ext]', 
                    }
                }
            },                       
            //SASS
            {
                test: /\.scss$/,
                use: ['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
            }           
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename:'css/style.min.[contenthash].css',
        }),
        new HtmlWebpackPlugin ({
            inject: false,
            hash: true,
            template: './src/index.html',
            filename: 'index.html'        
        }),
        new WebpackMd5Hash()   
    ]
}

I'm not pasting my index.js file because I just don't know what to do there since I won't be importing image by image. Looking forward for your thoughts.

Lowtrux
  • 156
  • 2
  • 12
  • 41

1 Answers1

3

The following statement should output all images in folder images to dist.

require.context("../images/", true, /\.(png|svg|jpg|gif)$/);
Legends
  • 21,202
  • 16
  • 97
  • 123
  • Many thanks @Legends it worked. Just one thing, whos doing the work of moving the files here? the "require.context" or the "file-loader" ?..or maybe both ?. The fact that the loader and the "require.context" have a path kinda confuses me. Thanks a lot ! – Lowtrux Jul 05 '18 at 07:26
  • It's webpack. the `file-loader`'s job is just to load this type of file. Take a look at the source code to see what file-loader actually does. – Legends Jul 05 '18 at 10:21
  • 1
    I have the same issue. But where in webpack.config.js file should I put that line of code? – user1941537 Jul 31 '18 at 22:08
  • The last time I used this was with webpack version 3, back then I have put it in an entry file, but you can try to do it in the `webpack.config.js`, just put it before or after the `export` statement. – Legends Aug 01 '18 at 08:24
  • Hey i am using webpack 4 and i need to move fonts and icons files, where should i put this line ? require.context is not a function Following error shown when i put this line before and after (both cases) export. – zeeshan zahoor May 09 '22 at 07:29
  • @Lowtrux Hello brother where do u put this line of code in webpack.config.js file ? – zeeshan zahoor May 09 '22 at 07:32
  • You put it in your entry script. Look at the webconfig.js of the op, in his case it's `entry:scripts.js` – Legends May 09 '22 at 08:00