Vite or Webpack
Tags: webdev, javascript, webpack, react
Published: 2022-12-30 09:08:29

## Javascript bundler
It combines different modules into a single optimized bulndle tailored for your web applications. Modern web browsers support ES modules and technologies like HTTP/2, which address request overhead concerns, JavaScript bundlers remain indispensable for an array of code enhancements. They perform essential code transformations, including minification, transpilation, optimization and ensure compatibility across various browsers.

##Webpack 
Is a popular build tool for modern JavaScript applications that is designed to bundle and optimize static assets such as JavaScript, CSS, and images. It has a wide range of features and is highly configurable, which makes it a good choice for a variety of projects.

##Vite 
As an alternative, on the other hand, is a newer build tool that is designed to be fast and lightweight. It uses native **ES module** imports to speed up development builds, and it has a focus on simplicity and ease of use.

Vite also leverages esbuild for transpiling TypeScript into JavaScript. This combination allows for faster transpilation and development server start times compared to traditional bundlers.

Since vite takes advantage of the availability of native ES modules in the browser and serves source code over native ESM It needs to transform and serve source code on demand as the browser requests it, reducing the need for upfront bundling.

It also uses Rollup, for efficient production builds. As a result, Vite offers lightning-fast development and solid production performance.

## Vite or Webpack? 

- **Vite** is often a faster and more lightweight alternative
- It has a smaller feature set and is not as configurable as Webpack.
- **Webpack** is a powerful and configurable build tool that is suitable for a wide range of projects

## Faster in what?
Vite is primarily designed to be fast in development builds, (DX: developer experience) and can be a significant advantage during development, as it allows you to make changes to your code and see the results almost instantly.

In production (UX), the speed of the build tool is usually less of a concern, as the build process is typically <u>only run once </u>before deploying the application. 
In this case, the main advantage of Vite is its simplicity and ease of use, as it can be easier to set up and maintain than more complex build tools like Webpack.

### Plus Vite + esbuild might encounter certain challenges [1]:
1. Limited support for export in embedded-in-app browsers on Android.
2. Slower loading time due to multiple network requests for individual modules (when not bundling code) consequently Webpack performs better in browser navigation speed due to pre-bundled site data.

## Under the hood
Are you curios how they work differently under the hood, let's watch these two videos
 [https://www.youtube.com/watch?v=nBYo2B2XN5s](https://www.youtube.com/watch?v=nBYo2B2XN5s)
[https://www.youtube.com/watch?v=5IG4UmULyoA](https://www.youtube.com/watch?v=5IG4UmULyoA)

## Learn more: 
1 - [ecmascript 6 - Webpack vs ES6 modules - Stack Overflow](https://stackoverflow.com/questions/57448588/webpack-vs-es6-modules)
2 - [How we use ES modules to speed up frontend development in a large application | Shortcut Blog](https://www.shortcut.com/blog/how-clubhouse-uses-es-modules-to-speed-up-frontend-development-in-a-large-application)
3 - [Vite vs. Webpack: A Head-to-Head Comparison - Kinsta®](https://kinsta.com/blog/vite-vs-webpack/)

## Summary
Overall, Vite is designed to be fast in <u>development builds</u> and easy to use in both development and production. However, it may not be the best choice for all projects, as it has a smaller feature set and is less configurable than some other build tools.

Any thoughts? 

All the best
Hash