How to build an application using Vite and Bun

Bun and Vite

Bun and Vite are relatively new tools, but they are already gaining popularity in the JavaScript community. One reason for this is that they work well together.

Bun can be used to run Vite applications, and Vite can be used to build Bun applications. This makes it easy to get the benefits of both tools in our project.

Building an application using Vite and Bun

To build an application using Vite and Bun, we can follow these steps:

  1. Run the following command to download and execute a script that installs Bun:

curl -fsSL https://bun.sh/install | bash
  1. Add the following lines to our shell configuration file:

export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"

These lines ensure that the Bun binaries are available in our system’s PATH, making it easier to use the Bun commands.

  1. Run the command to ensure that we have the latest version of Bun installed. This step is optional but can help us stay up-to-date with Bun’s features and bug fixes.

bun upgrade
  1. Create a new Vite project and execute the following command:

bun create vite my-app

Note: Replace my-app with our preferred project name. This command sets up a new Vite project with the specified name.

Vite supports multiple templates in its application development. The supported preset templates are as follows:

Vite Templates

JavaScript

TypeScript

vanilla

vanilla-ts

vue

vue-ts

react

react-ts

preact

preact-ts

lit

lit-ts

svelte

svelte-ts

solid

solid-ts

qwik

qwik-ts

Selecting a framework
1 of 2

For our demo app, we have selected, React and Typescript.

  1. Now, move into the project directory we’ve just created:

cd my-app
  1. Run the following command to install the project dependencies:

bun install
  1. Start the development server using the following command:

bunx --bun vite

Try it

The instructions mentioned above have been followed, and the resultant project is as follows:

{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}
A demo Vite project using Bun

Conclusion

To conclude, the provided sequence of commands initiates the setup of a Vite project using the Bun JavaScript development framework. It begins by installing Bun and its dependencies, including Vite, configuring the environment, and creating a new Vite project named my-app. With the subsequent installation of project dependencies and the launch of a development server, this setup empowers developers to rapidly start building web applications. Bun simplifies the process of working with Vite, enhancing the efficiency of web development while making it accessible for a broad range of projects and developers. The resulting product is a Vite-powered web application ready for customization and further development.

Copyright ©2024 Educative, Inc. All rights reserved