Anjolaoluwa Adebayo-Oyetoro Maker. Writes sometimes. playful most times. loves beautiful UIs

Getting started with Vite

The no-bundling JavaScript environment

4 min read 1173

JavaScript build tools have changed and shaped the way we build things that run on the web. Build tools are an integral part of any development workflow, they include (but are not limited to) task runners, transpilers, module bundlers, linters, package managers, and development servers.

These tools help developers build efficiently and make development processes much easier(although, configuring them can get complicated).

In this article, we will take a look at a build tool called Vite, what it is, why we need it, how it might improve our development workflow, and how to get started with it.

Prerequisites

This tutorial assumes the reader has the following:

What exactly is Vite?

Vite was originally intended as a development server for just Vue single file components(SFC) but it has evolved and is now a no-bundle JavaScript development server.

Vite doesn’t bundle our projects in development environments, rather it uses native ES module imports.

According to its official documentation:

Vite is an opinionated web dev build tool that serves your code via native ES Module imports during development and bundles it with Rollup for production.

The main difference between Vite and the other development servers currently available is the fact that it does not bundle your files during development.

It is important to note that Vite is still very much experimental and is undergoing work to make it suitable for production. It is best to not use it on critical projects until it becomes stable.

We made a custom demo for .
No really. Click here to check it out.

How does it work?

One of the reasons module bundlers are popular today is the poor support for ES6 modules by the browser when ES modules were first introduced in ES2016. Many modern browsers now support native ES modules and you can use the import and export statements natively, we can include our imports in our HTML using the type="module" attribute in our script tag to specify we’re importing a module:

<script type="module" src="filename.js"></script>

According to the documentation, the ES import syntax in our source code is served directly to the browser and any browser that supports the native <script module> automatically parses them, then makes HTTP requests for each import. The dev server intercepts the HTTP requests from the browser and performs code transformations where necessary.

This makes the Vite server insanely fast, Vite Cold start clocks at around 140ms compared to Vue-CLI 1900ms.

What does Vite offer?

Some of the perks of using Vite include:

Bare module resolving —  Browsers do not yet support bare module imports where you import from a package name like import { createApp } from 'vue' because it’s not a relative path to our node_modules. Vite checks your JavaScript files for such bare import specifiers, it rewrites them and performs module resolution to locate the correct files from your project dependencies and resolves them as valid module specifiers.

Hot module replacement(HMR) — Hot module replacement is a feature available in JavaScript bundlers where your JavaScript file changes are updated in the browser without needing a browser refresh, with Vite all your file changes are reflected in the browser almost immediately and you do not need to reload the browser. According to the documentation, “the hot module replacement (HMR) performance is decoupled from the total number of modules. This makes HMR in your project consistently fast no matter how big your app is”.

HMR speed is a huge pain point for developers who use Webpack.

On-demand compilation— Vite compiles source files as they are requested by the browser so the only code that is imported and required on the current screen is compiled, and unchanged files return a 304 (Not Modified) error code. This is different from what current bundlers do because they compile all the files in your project and bundle them before you can begin making changes to them. This makes Vite suitable for larger projects.

Configuration option — If you like to have more controls over your project, You can extend the default configuration of your project with the vite.config.js or vite.config.ts file in the base root directory of your project or the current working directory. You can also explicitly specify a config file via vite --config my-config.js.

You can add support for custom file transforms by adding a Koa middleware in your configuration file in development mode and a Rollup plugin for build.

Other features of Vite include:

  • Support for .tsx and .jsx files using esbuild for transpilation
  • Out of the box support for TypeScript also using esbuild for transpilation
  • Asset URL handling
  • Support for CSS Preprocessors, PostCSS, and CSS modules
  • Support for mode options and environment variables

Basic usage

To get started using Vite, we will be making use of create-vite-app, a boilerplate to bootstrap new Vite projects, we will not have to worry about configurations to get started with using our app as it comes with Vue as the default starter and we can configure what template we want to use with the --template flag as it also supports React and Preact.

Run this command to create a new Vite app with the boilerplate:

#Using NPX
npx create-vite-app testing-vite

#or, Using Yarn
yarn create vite-app testing-vite

create a vite app with boiler plate

We’re using the name “testing-vite” as the project name for this tutorial, it can be replaced with whatever name you deem fit.

Now, change to the created project directory using the command:

cd testing-vite

Then proceed to install the necessary packages required for our project to work:

#Using NPM
npm install

#or, Using Yarn
yarn

We can then spin up our development server in the browser by running the command:

#Using NPM
npm run dev

#or, Using Yarn
yarn dev

You should get something similar to this running on http://localhost:3000 after running the dev command:

running vite on local host

You can run the following command to bundle your app for production:

vite build

vite bundling

Vite uses Rollup for production builds, the production build output is in the dist directory located in the root of your project. It contains static assets that can be deployed anywhere (and can be polyfilled to support older browsers).

The documentation says, “the build step is configurable by passing on most options to Rollup”.

Conclusion

With Vite, you have a very fast development server that would improve your development workflow and increase productivity.

The result of your file changes are instantaneous in the browser and you can bundle your app for production using Rollup. The code repository to this article can be accessed on GitHub.

: Full visibility into your web apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

.
Anjolaoluwa Adebayo-Oyetoro Maker. Writes sometimes. playful most times. loves beautiful UIs

Leave a Reply