I am using Webpack for my React app.
I installed 'File-loader' & 'Url-loader' in my webpack config.
However, Im not sure how to link images to my components. I am holding the 'src' for the image in a 'Data.js' file from where I pass the data for the image to the react component.
My webpack.config.js:
...
const PATHS = {
app : path.join( __dirname, "app" ),
build : path.join( __dirname, "build" )
};
const common = {
entry : {
app : PATHS.app
},
output : {
path : PATHS.build,
filename : "bundle.js"
},
module : {
preLoaders : [
...
loaders : [
{
test : /\.css$/,
loaders : [ "style", "css" ],
include : PATHS.app
},
{
test : /\.jsx?$/,
loader : "babel",
query : {
cacheDirectory : true,
presets : [ "react", "es2015" ]
},
include : PATHS.app
},
{
test : /\.(jpg|png)$/,
loader : "file-loader?name=[name].[ext]",
include : PATHS.app
}
...
Im my react presentation component, Im simply using require
to retrieve the image:
const PreviewTemImgParent = ({ data }) => {
let img = require( data.get( "src" ));
return(
<div className = "card previewImgParent">
<img
src = { img }
...
The Data.js file has absolute paths (main app folder) for each image (I may be going wrong here):
export const previewTemImgData = Map({
body : List.of(
Map({ // using immutable.js
src : "app/css/asset/template/png/body/GatheredAtEmpireLine.png",
alt : "image",
className : "flipParent",
...
Global directory image for what its worth:
I wonder where I am making the mistakes and how to resolve this?
EDIT : After reading around, I figure that webpack needs reference for the relative position of the image, so I have amended webpack.config.js to following:
...
output : {
path : PATHS.build,
publicPath : "/",
filename : "bundle.js"
}
...
However, that doesnt solve the problem. Guidance would be much appreciated.
First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/. }; It's quite similar to setting up fonts with Webpack.
They allow you to pre-process files as you import or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs.
Developers can customize image bundling using Webpack, which provides asset modules for using files (containing the fonts and icons). This is done without configuring additional loaders like url-loader and file-loader.
File-loader will allow us to import file-based assets into our webpack managed JS and CSS files. Some examples of files we may want to import include images (. jpg, . png, etc.)
Suggestions:
1.Use url-loader
for images.
By specifying the limit
in your webpack configuration, such as: url-loader?limit=8000
, if the file is smaller than 8000 Byte. the url-loader
works seem as file-loader
. Embed the files to the bundle output for the main entry. If not, see point 2.
2.Use url(address) in css/scss for use images
Here is an example which is powered by Webpack and url-loader:background: url('../images/heroBanner.png') no-repeat center center fixed;
And because the file is large enough, webpack transform the file just like other bundles to the output directory. It will send an HTTP request for this image when the web page is loading.
https://github.com/EcutDavid/EcutDavid.github.io/blob/8346f18dccdae18c4d6e98eb9b8cc51d62338cb5/src/styles/Header.scss#L2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With