When I'm trying to load an image
<img class="brand" alt="Brand" width="50%" height="50%" src="/logo.jpg">
It produces the following error:
logo.jpg:1 GET http://localhost:8080/logo.jpg 404 (Not Found)
webpack.config.js:
module: {
loaders: [
{
test: /\.html$/,
exclude: /index\.html$/,
loader: 'html-loader?root=./assets/images&interpolate&name=./views/[name].[ext]'
},
{
test: /\.(png|jpg|jpeg|gif)$/,
loader: 'url-loader?limit=10000&name=./assets/images/[name].[ext]'
}
]
}
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.
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.
Using Webpack Similar to CSS, we can use Webpack for image bundling. First, we need to install two loaders as we did with CSS bundling. Here the loaders we use are the file loader and the HTML loader.
You have to import/require
your image in your entry js
file and that image will be processed and added to your output
directory and the Logo
variable will contain the url of that image after processing
import Logo from './logo.jpg';
Another way is to use html-loader
and import it in your entry js
file . Then you can use the usual src
attribute as in html.
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