Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  192] [ 3]  / answers: 1 / hits: 19791  / 5 Years ago, wed, october 9, 2019, 12:00:00

I'm trying to get static images fro my public directory but is not being found. I'm not using CRA, so maybe is some configuration with Webpack that I'm missing. Using file-loader module and importing the image works on Dev Mode, but doesn't work in for my production server specification



My Project structure:



public
static
images
image.png
src
component
component.js
...
package.json
webpack.common.js
webpack.dev.js
webpack.prod.js


On component.js, I want to get image.jpg on static/images folder like this:



  <img src='/static/images/image.png'></img>


But I'm getting a 404 not found.



My webpack.commom.js:



const CleanWebPackPlugin = require('clean-webpack-plugin')
const HtmlWebPackPlugin = require('html-webpack-plugin')
const path = require('path')

module.exports = {
entry: {
main: './src/index.js'
},
output: {
filename: '[name].[hash].js',
path: path.resolve('./dist'),
publicPath: /
},
module:{
rules:[
{
test: /.js$/,
exclude: ['/node_modules'],
use: [{ loader: 'babel-loader'}],
},
{
test: /.s?(a|c)ss$/,
use: [{
loader: 'style-loader'
}, {
loader: 'css-loader'
},{
loader: 'sass-loader'
}]
},
{
test: /.(png|jpe?g|gif)$/,
use: [
{
loader: 'file-loader',
options: {},
},
],
},

]
},
plugins: [
new HtmlWebPackPlugin({
template: 'index.html'
}),
new CleanWebPackPlugin(),
],
}


And the Dev version:



module.exports = merge(common, {
mode: 'development',
devServer: {
host: 'localhost',
port: 3000,
open: true,
historyApiFallback: true,
publicPath: /,
}
})


Thank you in advance.


More From » node.js

 Answers
8

You are using file-loader as webpack plugin, which does not work in the way of just mirroring the directory structure of public to dist. You can find the documentation for that plugin here: https://webpack.js.org/loaders/file-loader/



Basically, what the plugin does is, if you import an image file (actually programatically importing it, not just using a string reference to its path), the file is copied to your dist directory, potentially renamed and than in your compiled source code the proper file name is inserted.



So in your case, if you want to solve your problem using file-loader, you would reference the image file like



// Relative path to image file from js file
import imageFile from './assets/image.png';

// ...
const component = props => <img src={imageFile}></img>;


If you want to use the approach of actually just mirroring a public-directory to the dist directory, you need to use an additional webpack-plugin for that (e.g. https://stackoverflow.com/a/33374807/2692307)



By the way, I assume it works in dev-mode because you are setting '/' as public path, so the development server just serves everything in the root directory as well. That is something different than copying the files to dist however as you are trying to achieve.


[#51592] Thursday, September 26, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ross

Total Points: 477
Total Questions: 97
Total Answers: 98

Location: France
Member since Thu, May 6, 2021
3 Years ago
;