Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bundle images using webpack?

I am making an angular2 application with webpack module bundler. Now i have added file-loader for loading image files such as jpg, gif, png,etc. But when i run the build command the image files are not being bundled. Here is my configuration:

webpack.config.js (image loader config only)

{
    test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?v=.+)?$/,
    loader: 'file-loader?name=assets/[name].[hash].[ext]'
},

I have included images from a folder in my html template like this:

<img src="/asset/img/myImg.png"/>

in my css too:

#myBackground {
    background: background: #344556 url(/assets/img/background.jpg) center/cover;
}

But when i build the whole application, the images are not placed in the assets folder like i have specified in the webpack config.

Now the strangest thing is the same configuration is used for the fonts and they are being created inside the assets folder. This is my css for including the fonts in style.css:

@font-face {
    font-family: 'robotolight';
    src: url("../fonts/roboto-light-webfont.eot");
    src: url("../fonts/roboto-light-webfont.eot?#iefix") format("embedded-opentype"), url("../fonts/roboto-light-webfont.woff2") format("woff2"), url("../fonts/roboto-light-webfont.woff") format("woff"), url("../fonts/roboto-light-webfont.ttf") format("truetype"), url("../fonts/roboto-light-webfont.svg#robotolight") format("svg");
    font-weight: normal;
    font-style: normal;
 }

This font's all the files like svgs, ttf, woff, etc can be found in the assets folder after running the build.

Can anyone tell me why the images are not being created in the assets folder but the fonts file are, with the same config/loader.

like image 936
kanra-san Avatar asked Jan 09 '17 09:01

kanra-san


People also ask

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.

How do I bundle files in webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script. But that's just the bare minimum it can do.

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.

How do I add an image to a react in webpack?

Referencing to images Webpack can pick up images from style sheets through @import and url() assuming css-loader has been configured. You can also refer to your images within the code. In this case, you have to import the files explicitly: import src from "./avatar.


1 Answers

Might be a duplicate of this

Missing some loaders (extract-loader & html-loader)

Otherwise, if these static assets are included directly from the HTML, you could try to copy manually these assets via this handy webpack plugin

like image 152
nastyklad Avatar answered Oct 05 '22 23:10

nastyklad