admin管理员组文章数量:1431435
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 isdist
directory so as you request to the asset with public path would be okay likeyourDomain/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
1 Answer
Reset to default 4You 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
版权声明:本文标题:javascript - Webpack file-loader not loading assets folder in production build - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745488925a2660514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论