Vite
Up until now, we were doing fine with the VS Code Live Server. But since our applications will become more and more complex (depending on npm packages to give an example), we need a more advanced development server. This is where Vite comes in. There are a lot of advantages, the following is just a summary:
Features
Dev server
Vite is a development server that has some special tricks up its sleeve to start it up fast, even when a lot of libraries need to be loaded first.
HMR
Vite has Hot Module Replacement (HMR) built in. This means that when you change a file, the browser will automatically reload the page. And again… does this fast.
Production build
Vite can also build your application for production. It will bundle all your files into one or more files and minimizes it. It strips out all the comments and unused code. This makes your application smaller and faster.
Plugins
Vite has a lot of plugins that can be used to extend its functionality. For example, you can use a plugin that makes it possible to use brand-new CSS features, but provides an automatic fallback for older browsers.
In the end, Vite really speeds up our development process and provides us with a lot of convenient features. Be aware that this isn’t the only option out there, things like Webpack or Parcel do about the same thing.
Installation
To create a new vite-project, you can run the following command:
This is an alias for npm init vite@latest
and will create a new project configured with Vite. A folder will be created with the name of the project. When asked, you can choose for a basic ‘Vanilla’ project.
You can write this in one line to skip the steps as follows:
Be aware that we still have to navigate to the project and do a npm install
to install all the necessary dependencies.
Exploration
Have a look at the newly created project, there is a lot!
node_modules
- this is where all the dependencies are stored, dependencies needed by Vitepublic
- A directory containing an SVG file. Files in this directory aren’t parsed by Vite, this has certain consequences.gitignore
- A file that tells git which files to ignore, notice that the project itself isn’t a repository yet.counter.js
- An example file that contains a JavaScript function to increase a counterindex.html
- The main HTML file, notice that it has a script tag that loads a/main.js
filejavascript.svg
- An other SVG file, this one will be parsed by Vitemain.js
- The main JavaScript file, it imports thecounter.js
file, thejavascript.svg
file and thestyle.css
(!) filepackage-lock.json
- obviouslypackage.json
- Notice that Vite is a devDependency and there are 3 scripts availablestyle.css
- The main CSS file.
Spin up the dev server:
When you open the URL in your browser, you will see a basic demo page.
- Some HTML is generated by the
main.js
file - When you click on the button, the counter goes up. So JavaScript seems to be working OK
- The two SVG’s are there
- The page is definitely styled, but where is the CSS file loaded?
If you want to stop the dev server, you can enter Ctrl + C
in the terminal.
HMR
Organize your desktop so that a browser and VS Code are side by side. Make sure that the Vite dev server is running. Now edit the style.css to give the page a red background. You will see that the background of the page turns red immediately —without doing a full page refresh.- When you open up your Developer Tools and inspect the head element, you will experience that only the style tag changes when you change the background color.
Build it
When you are done with your project, you can build it for production.
The build process is done, and you can find the files in the dist
folder.
Notice the following:
- The
vite.svg
in the public directory isn’t “transformed” by Vite, but it is present in thedist
folder. (it is just copied over) - The other files have a hash in their name, this is to prevent caching issues.
- The .js and .css files are minified.
Preview
You can preview the production build by running the following command:
This opens up a new browser window with the production build. Instead of the inline CSS in the head, the minified CSS is loaded from a separate file. The same goes for the JavaScript.
Deployment on a subdirectory
It is quite common for us to deploy something on a “nested public path” (aka subdirectory). By default, Vite assumes we are deploying on the root of a domain, so we can run into some issues when requiring assets.
To let Vite know the name of te subdirectory (structure), we can add a base
property to the build argument in our package.json. See the documentation for more details.