Migrating From Vue-CLI & Webpack to Vitejs
Boot.dev’s web app that hosts all of my coding courses is a single-page application written in Vue 2, with plans to migrate to Vue 3 soon™©®. In the meantime, I happened across a cool new tooling app called Vite that promised a few things that caught my attention.
- Nearly instant development server startup time
- Hot module replacement out of the box
- Simple configuration
- Out-of-the-box support for ES modules
This particularly resonated with me because my (fairly) simple app’s development server took over 10 seconds to start up with the Vue-cli and Webpack, and I’ve spent many hours in the past trying to configure Webpack and Babel, when I just needed basic Vue configurations. Let’s look at some quick anecdotal comparisons before I dive into the migration guide, so you can see if the benefits of switching are worth it for you.
| Vite | Vue-cli + Webpack | |
|---|---|---|
| Dev server start time | ~600ms | ~10,000ms |
| HMR time | Unsure, appears instant | ~2,000ms |
| Production build time | ~15s | ~22s |
| Number of bundled JS files | 29 JS modules | 18 JS Modules |
| Average JS bundle size | ~29kb | ~61kb |
| Total JS bundle size | ~840kb | ~1098kb |
Vite vs Vue-cli + Weback
Additionally, to get the 18 modules shown above using the Vue cli and webpack, I had to add comment annotations to my routes.js file. Out of the box the Vue-cli makes one giant bundle, which is much worse for page performance reasons. Vite splits the bundle along module lines out-of-the-box without the need for those annoying annotations.
Migration Guide
Let’s go through the major steps of actually moving a project in Vue 2 from the Vue CLI to Vite.
Step 1 - dependencies
All @vue-cli... dependencies need to go. For me that meant removing the following.
- "@vue/cli-plugin-babel": "^4.5.6",
- "@vue/cli-plugin-eslint": "^4.5.6",
- "@vue/cli-service": "^4.5.6",
These were replaced with Vite and its plugin for Vue.
+ "vite": "^2.2.1",
+ "vite-plugin-vue2": "^1.4.4",
+ "@vue/compiler-sfc": "^3.0.11",
Next, Vite supports sass out of the box, so I could remove my old dependencies.
- "node-sass": "^4.12.0",
- "sass-loader": "^10.0.2",
And I replaced them with the bare-bones sass compiler, because Vite requires it to be available.
+ "sass": "^1.32.11",
Finally, because Vite only supports modern browsers (sorry if you have to support legacy stuff, Vite might not be for you) I removed babel dependencies and my babel.config.js file.
- "babel-eslint": "^10.1.0",
- "babel-runtime": "^6.26.0"
Moving index.html
Vite doesn’t store index.html in the public folder like you’re used to, instead it goes right in the root of your project, so move it there. Vite also needs an additional entry point.
<body>
<noscript>
<strong>
We're sorry but this app doesn't work properly without JavaScript enabled.
Please enable it to continue.
</strong>
</noscript>
<div id="app"></div>
<!-- this new script is for vite -->
<script type="module" src="/src/main.js"></script>
</body>
You’ll also need to move your static asset references to use a simple / rather than <%= BASE_URL %>.
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="manifest" href="/site.webmanifest" />
Vite Config
Here is the vite.config.js I settled on, it goes in the root of the project.
import { defineConfig } from "vite";
import { createVuePlugin } from "vite-plugin-vue2";
import path from "path";
export default defineConfig({
plugins: [createVuePlugin()],
server: {
port: 8080,
},
resolve: {
alias: [
{
find: "@",
replacement: path.resolve(__dirname, "src"),
},
],
},
build: {
chunkSizeWarningLimit: 600,
cssCodeSplit: false,
},
});
The resolve block lets me import components using @ as the root of the src directory. For example, import Tooltip from '@/components/Tooltip.vue';.
The build block does a couple of things, first, it increases the chunk size warning limit from the default of 500kb to 600kb. I did that just because I have a really heavy code editor component and I don’t want to see the warning every time.
Secondly, my site completely broke when I let Vite split my .css files the way it wanted to. This actually makes me sad because I’d rather have my users only load the CSS they need as they need them. If anyone has had a similar problem please let me know how you solved it.
.vue extensions
Vite explicitly requires that all .vue imports include the .vue in the path. This can be a bit tedious if you haven’t been using the extensions. All your imports need to be updated from import Tooltip from '@/components/Tooltip' to import Tooltip from '@/components/Tooltip.vue'.
Webpack chunking for lazy-loaded routes
If previously you used comment annotations in Webpack to break up your bundle, you don’t need to do that anymore! Just remove them.
const Courses = () => import(/* webpackChunkName: "Courses" */ '@/views/Courses.vue');
becomes
const Courses = () => import('@/views/Courses.vue');
Yarn Scripts
I use the following three scripts.
"serve": "vite --open",
"preview": "vite preview --open --port 8080",
"build": "vite build --out-dir dist",
"lint": "eslint src",
"lint:fix": "eslint src --fix"
yarn servestarts a development server and open your preferred browser.yarn buildbuilds the production files and stores them indistyarn previewserves the production files locally for testingyarn lintrunseslintand reports problems. You probably were usingvue-cli-service lintbefore, which just raneslintunder the hood.
Node environment
Vite is strict when it comes to Node.js code being slipped into your front-end bundle. I ran into an issue where a dependency I had required global to be defined. Obviously, the best thing is to not require dependencies like that, but in my case I needed it so I added a little shim in index.html.
<!-- polyfill global because shit dependencies -->
<script>
const global = globalThis;
</script>
<!-- end polyfill -->
Vue 3
My next migration will likely be to Vue 3, hopefully sometime this year. I’ve really just been holding out for a bit more stability, and for a few of my dependencies to support the new major version.
Related Articles
How to Create a Custom Checkbox Form in Vue
Nov 25, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
You have a problem. The browser’s default checkbox form is ugly and outdated, but importing an entire library just to change the styling seems like overkill. Instead, let’s build a custom checkbox form component from scratch. It will take 15 minutes to type up, or just 3 if you just copy and paste my boilerplate code.
How to Make a Custom Slider Component in Vue
Nov 24, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
Creating a custom slider component can be tricky, especially if you want to create a lean standalone Vue component. In this quick article, you’ll learn how to build a fully customizable slider component in Vue. Feel free to swap out the majority of the CSS to get the styling you want, but I’ll give you a good jumping-off point.
How to Make a Simple Vue Custom Select Component
Sep 25, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
Creating a custom select tag with its own styling is notoriously difficult. Sometimes it’s impossible to build from scratch without a combination of styled divs and custom JavaScript. In this article, you’ll learn how to create a custom select component in Vue that can be easily styled with your own CSS. In fact, it’s the same component we use in production on boot.dev, and you can see it in action on our JavaScript playground.
Creating a Custom Tooltip Component in Vue
Aug 28, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time. For that reason, we’re going to build a custom single file tooltip component that you can build yourself and tweak to your heart’s content. It might take 15 minutes instead of 3… sorry about that.