admin管理员组

文章数量:1427353

I've deployed my react application build in Webpack in netlify. I see the CSS, Fonts and everything else loading fine, but not the images under assets/images folder.

Here's my folder structure in live site source path:

my webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: ['./src/index.js', '@babel/polyfill'],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-react', '@babel/preset-env'],
                    },
                },
            },
            {
                test: /\.css$/i,
                exclude: /node_modules/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                        },
                    },
                ],
            },
            {
                test: /\.(gif|png|jpe?g)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            // name: '[name].[ext]',
                            // name: '[path][name].[hash].[ext]',
                            name: '/src/assets/images/[hash].[ext]',
                            // outputPath: 'src/assets/images/',
                        },
                    },
                ],
            },
            
        ],
        
    },
    devServer: {
        host: '192.168.1.10', //your ip address,
        historyApiFallback: true,
        contentBase: './build',
        disableHostCheck: true,
    },
    resolve: {
        extensions: ['*', '.js', '.jsx'],
    },
    output: {
        path: path.resolve(__dirname, '/dist'),
        filename: 'bundle.js',
        publicPath: '/',
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            inject: true,
            template: path.resolve(__dirname, 'index.html'),
        }),
    ],
    devServer: {
        contentBase: './build',
        hot: true,
    },
};

I've deployed my react application build in Webpack in netlify. I see the CSS, Fonts and everything else loading fine, but not the images under assets/images folder.

Here's my folder structure in live site source path:

my webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: ['./src/index.js', '@babel/polyfill'],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-react', '@babel/preset-env'],
                    },
                },
            },
            {
                test: /\.css$/i,
                exclude: /node_modules/,
                use: [
                    'style-loader',
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                        },
                    },
                ],
            },
            {
                test: /\.(gif|png|jpe?g)$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            // name: '[name].[ext]',
                            // name: '[path][name].[hash].[ext]',
                            name: '/src/assets/images/[hash].[ext]',
                            // outputPath: 'src/assets/images/',
                        },
                    },
                ],
            },
            
        ],
        
    },
    devServer: {
        host: '192.168.1.10', //your ip address,
        historyApiFallback: true,
        contentBase: './build',
        disableHostCheck: true,
    },
    resolve: {
        extensions: ['*', '.js', '.jsx'],
    },
    output: {
        path: path.resolve(__dirname, '/dist'),
        filename: 'bundle.js',
        publicPath: '/',
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            inject: true,
            template: path.resolve(__dirname, 'index.html'),
        }),
    ],
    devServer: {
        contentBase: './build',
        hot: true,
    },
};

And I try to access my images in JSX like this -

import { HeaderLogo } from '../../assets/images/image_name.png';

When I inspect element and change in path from /assets/images/image-name.png to /src/assets/images/image-name.png it is showing image in live site.

Here's my folder structure in local:

The same is working fine in local. Not sure what causing issue in prod. I know something is wrong with my file-loader path. But not sure where to correct.

Any suggestions please?

Share Improve this question asked Aug 6, 2020 at 2:09 beta programmersbeta programmers 6533 gold badges10 silver badges23 bronze badges 8
  • Your configuration looks fine. Are you deploying static files only in your server? – tmhao2005 Commented Aug 6, 2020 at 2:24
  • If I get you correct, I added all image files in the src/assets/images and build it. And I'm accessing images from there, not from any other server. In the ./dist folder generated, I see the assets. But not when deployed over netlify – beta programmers Commented Aug 6, 2020 at 2:37
  • I meant are you serving your website at dist/index.html in your server? Which means the server root is dist directory so as you request to the asset with public path would be okay like yourDomain/assets/image/logo.png. – tmhao2005 Commented Aug 6, 2020 at 2:46
  • Yes, I see the site is served from dist/index.html. But, I don't want to change all the images src path to yourDomain/assets/image/logo.png, which is a pain process. I doubt if something's working with same path in local, would some change in config make this work in prod? Or everytime I deploy, should I change it to public path? – beta programmers Commented Aug 6, 2020 at 2:53
  • 1 Here's the site, where I'm facing this issue currently - frosty-colden-75f287lify.app/# – beta programmers Commented Aug 6, 2020 at 3:01
 |  Show 3 more ments

1 Answer 1

Reset to default 4

You need to copy the image files to the build directory.

To do that, install webpack copy plugin: copy-webpack-plugin

npm install copy-webpack-plugin --save-dev

And webpack config should be:

// import copy plugin
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: ['./src/index.js', '@babel/polyfill'],
    module: {

       ...

    },
    plugins: [

        ...

        new CopyPlugin({
            patterns: [
                {
                    from: path.resolve(__dirname, '/src/assets/images'),
                    to: path.resolve(__dirname, 'dist')
                }
            ]
        }),
     ]
};

For more details https://www.npmjs./package/copy-webpack-plugin

本文标签: javascriptWebpack fileloader not loading assets folder in production buildStack Overflow