Handle Image Asset Bundling using Vite in React

Amarachi Iheanacho

Vite is a frontend toolkit providing robust features to build scalable web applications.

This article will discuss what Vite is, why it is becoming an essential tool in the front-end universe, and how to utilize it to handle image bundling via building an image gallery.

The completed project is on Codesandbox. Check out the Codesandbox to get started quickly.

GitHub URL

https://github.com/Iheanacho-ai/handle-assets-bundling-react

Prerequisities

To get the most out of this article, we require the following:

  • A basic understanding of CSS, JavaScript and React.js
  • Node and its package manager, npm. Run the command node -v && npm -v to verify we have them installed, or install them from here. Alternatively, we can use another package manager, Yarn.

What is Vite

Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects.

It is an excellent frontend tooling that provides developers with NPM Dependency Resolving, Pre-Bundling, and Hot Module Replacement. It is advantageous in local development as it handles refreshing file changes swiftly.

Vite leverages the availability of native ES modules in the browser and the rise of JavaScript tools written in compile-to-native languages to solve the slow feedback loop that arises in developing larger applications.

Setting up our Vite app

To create a vite app, we go to our terminal and run the command below.

NOTE: If you are on windows and using Git Bash, you might have issues with the arrows, so you should use the Powershell terminal instead.

1npm create vite@latest
2
3 #or
4
5 yarn create vite
6
7 #or
8
9 pnpm create vite

Running this command will trigger a set of question prompts. Answer these questions as follow.

1Project name: <name of our project>
1Select a framework: React.js
1Select a variant: react

Next, we run these commands.

1cd <name of our project>
2 npm install
3 npm run dev

The browser will change to the directory containing our project with this command. To see our app, go to http://localhost:3000/.

Building our Image Gallery

Vite handles images as assets automatically, so we do not need to specify configurations for the images. Learn more on static asset handling here.

To get started on building our image gallery, we create a new folder src/images, that will contain all nine images in our gallery.

To download the images we will use in our gallery from here.

After adding the images, we import them in our App.jsx file.

1import React from 'react'
2 import Img1 from './images/photo-1.jpg'
3 import Img2 from './images/photo-2.jpg'
4 import Img3 from './images/photo-3.jpg'
5 import Img4 from './images/photo-4.jpg'
6 import Img5 from './images/photo-5.jpg'
7 import Img6 from './images/photo-6.jpg'
8 import Img7 from './images/photo-7.jpg'
9 import Img8 from './images/photo-8.jpg'
10 import Img9 from './images/photo-9.jpg'
11 import './App.css'

Next, we create an array of these images in our App component body.

1const images = [Img1, Img2, Img3, Img4, Img5, Img6, Img7, Img8, Img9]

We use the JavaScript map() function to loop over the images array and render a native html img tag for each. The source of the image is attributed to the current array value in the loop.

1{
2 images.map((image) =>
3 <div className='image'>
4 <img src={image} alt="" />
5 </div>
6 )
7 }

Our App.jsx file should look like this.

1import { useState } from 'react'
2 import Img1 from './images/photo-1.jpg'
3 import Img2 from './images/photo-2.jpg'
4 import Img3 from './images/photo-3.jpg'
5 import Img4 from './images/photo-4.jpg'
6 import Img5 from './images/photo-5.jpg'
7 import Img6 from './images/photo-6.jpg'
8 import Img7 from './images/photo-7.jpg'
9 import Img8 from './images/photo-8.jpg'
10 import Img9 from './images/photo-9.jpg'
11 import './App.css'
12
13 function App() {
14 const images = [Img1, Img2, Img3, Img4, Img5, Img6, Img7, Img8, Img9]
15 return (
16 <div className="App">
17 <div className="image-container">
18 {
19 images.map((image) =>
20 <div className='image'>
21 <img src={image} alt="" />
22 </div>
23 )
24 }
25 </div>
26 </div>
27 )
28 }
29
30 export default App

We need to add the image gallery styles in our App.css file to top our gallery.

1.App {
2 width: 100%;
3 height: 100%;
4 display: flex;
5 justify-content: center;
6 align-items: center;
7 }
8 h2{
9 text-align: center;
10 }
11 .image-container{
12 width: 80%;
13 margin-left: 10%;
14 display: grid;
15 grid-template-columns: repeat(4, 1fr);
16 gap: 5px;
17 }
18 .image-container div:first-child {
19 grid-column-start: 1;
20 grid-column-end: 3;
21 }
22 .image-container div:last-child {
23 grid-row-start: 2;
24 grid-row-end: 5;
25 }
26 .image{
27 width: 100%;
28 height: 300px;
29 }
30 .image img {
31 width: 100%;
32 height: 100%;
33 }

In the code block above, we do the following:

  • Center our gallery using CSS native justify-content: center and align-items.
  • Define how many columns our gallery should have using grid-template-columns: repeat(4, 1fr).
  • We target the first and last div in the image-container and specify how many columns or rows each should take up using CSS selectors.

With that, we have successfully created our image gallery.

Conclusion

This article discussed what Vite is, the advantages of building our applications with Vite and, more importantly, how to integrate Vite in our project to handle image assets bundling. Using this knowledge, we created an image gallery.

Resources

Amarachi Iheanacho

Frontend Engineer and Technical Writer