Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack - Require Images Using File-Loader

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:

enter image description here

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.

like image 205
Kayote Avatar asked Jun 26 '16 19:06

Kayote


People also ask

How do you add a picture to webpack?

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.

What does file-loader do in 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.

Can webpack bundle images?

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.

Which loader enables support for images and fonts in webpack?

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.)


1 Answers

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.

enter image description here

https://github.com/EcutDavid/EcutDavid.github.io/blob/8346f18dccdae18c4d6e98eb9b8cc51d62338cb5/src/styles/Header.scss#L2

like image 127
David Guan Avatar answered Oct 05 '22 23:10

David Guan