Incorporate the Vuetify 2 library into your Vite project

Our development team successfully created an application using Vue 2 with class based components and TypeScript. Now, we are eager to transition to Vite for its numerous benefits.

After following a helpful guide on how to migrate from Vue CLI to Vite (which I highly recommend), we encountered some issues. The browser was unable to fetch "/src/main.js" from index.html. When attempting to use "/src/main.ts", it fetched the file but displayed errors indicating that Vuetify was not installed. We understand that .ts files are not readable by browsers, but after hours of debugging, we still faced challenges.

We also learned from Vuetify's documentation that "First party Vite support" is yet to be released.

My main question remains - is it feasible to integrate Vuetify into a Vite application?

Here are the relevant dependencies from our package.json:

  "dependencies": {
    "vue": "^2.6.12",
    "vue-class-component": "^7.2.6",
    "vue-property-decorator": "^9.1.2",
    "vuetify": "^2.4.0",
  },
  "devDependencies": {
    // List of dev dependencies
  }

In our tsconfig.json file, we specified certain compiler options, including the necessary types:

{
  "compilerOptions": {
    // Compiler options here
    "types": [
      "webpack-env",
      "vite/client"
    ],

For our vite.config.js, we configured the plugins and resolved aliases as required:

import { defineConfig } from "vite";
import { createVuePlugin as vue } from "vite-plugin-vue2";
const path = require("path");

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

When attempting to import ".ts" in index.html, only an empty HTML page rendered with a "no vuetify" error indicator:

<script type="module" src="/src/main.ts"></script>

https://i.sstatic.net/FnoPT.png

Answer №1

Need help with Vuetify 2? Check out this link

To get started, first install the plugin by running:

npm i unplugin-vue-components -D

Next, make changes to your vite.config.ts file as follows:

import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/vite';

// Customize your configuration below:
export default defineConfig({
  plugins: [
    createVuePlugin(/* options */),
    Components({
      resolvers: [
        // Include support for Vuetify components
        VuetifyResolver(),
      ],
    }),
  ]
})

Answer №2

Get started by downloading unplugin-vue-components.

npm install -D unplugin-vue-components

Next, integrate the plugin into your vite.config.ts file:

import { defineConfig } from 'vite'
import vue2 from '@vitejs/plugin-vue2'
import Components from 'unplugin-vue-components/vite'
import { VuetifyResolver } from 'unplugin-vue-components/resolvers'

defineConfig({
  plugins: [
     vue2(),
     Components({
        resolvers: [VuetifyResolver()],
     })
  ]
})

For those using vuetify and wanting to customize sass variables, check out this link for details: custom sass variable

defineConfig({
  plugins: [
    vue2(),
    Components({
      resolvers: [VuetifyResolver()],
    }),
  ],
  css: {
    preprocessorOptions: {
      sass: {
        additionalData: [
          // Add your custom vuetify variables here
          '@import "@/styles/variables.scss"',
          '',
        ].join('\n'),
      },
    },
  },
})

If you're interested in a Vite 3 + Vuetify 2 + Vue 2.7 starter template with TypeScript support and IntelliSense in Vuetify components, check out my repository:

https://github.com/kingyue737/vitify-admin

Answer №3

I recently finished putting together a new starter template.

Check it out here on GitHub!

Initially designed for projects utilizing vue-property-decorator, it also now supports the composition API.

To access the Vuetify function using the composition API, simply use useVuetify().

Answer №4

I have successfully transitioned the default vue2/vuetify2 official template from vue-cli to vite 5 in a repository that I created.

This migration primarily makes use of:

  • @vitejs/plugin-vue2 and vue:^2.7.0 (the minimum required version) for vue 2
  • unplugin-vue-components for vuetify 2

Please refer to: https://github.com/sbernard31/vuetify2-vite-template

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Angular http.post function seems to be returning null responses consistently, without any actual data being received

When making a post call in Angular using Http.post, I am sending jsonData as a parameter with the following formatted data. However, every time I receive a response as null. Could you please review my code and let me know if there are any mistakes? Here ...

NGXS: Issue with passing objects to "setState" occurs when any of the patched inner fields is nullable or undefined

In my Angular components, I have created a class that is responsible for storing the state: export class MyStateModel { subState1: SubStateModel1; substate2: SubStateModel2; } This class has simple subclasses: export class SubStateModel1 { someField ...

Retrieve a collection of nested dictionaries containing flask and angular data

In my app development journey with flask and ionic(angular), I'm working on returning a JSON list. Here's the python code snippet: def get-stocks(): # Select all stocks cursor.execute("""SELECT * FROM `tbl_symbol_index`"" ...

What is the correct way to interact with an Object's functions in TypeScript?

I'm facing a challenge with one of the libraries I'm using as it is throwing a lot of TypeScript errors related to Object functions. The list of TS errors that I encountered include: error TS2339: Property 'random' does not exist on t ...

What is the solution for fixing the "type object does not contain property" error in Angular?

Within my application, there is a parent component that houses a list of objects representing various fruits. When a user clicks on a fruit, its data is passed to the child component for display as detailed information. <app-details ...

Customizing Tabs in Material UI v5 - Give your Tabs a unique look

I am attempting to customize the MuiTabs style by targeting the flexContainer element (.MuiTabs-flexContainer). Could someone please clarify the significance of these ".css-heg063" prefixes in front of the selector? I never noticed them before upgrading my ...

Having trouble with Jest imports not functioning as expected when using ts-jest

Currently, I am utilizing Jest in conjunction with ts-jest to conduct tests on my TypeScript code. Everything is functioning well except for the issue that arises when importing external libraries. Strangely enough, while import dotenv from 'dotenv&ap ...

What approach should be taken to effectively utilize the Vuetify skeleton in a Nuxt project?

I'm relatively new to Vue, Nuxt, and Vuetify and all their components. Currently, I'm developing a Nuxt project with Vuetify and I want to implement the skeleton loader, but I'm facing some challenges. Right now, I'm using the following ...

Issue with Angular 6 where data is not binding to the UI on initialization

I am struggling with binding dynamic data to the UI and Data tables on my website. Despite trying various methods, I have not been able to achieve success. Currently, I am using the smart admin latest theme for development. When I make a call to the API, ...

Adding additional properties to JSX components during typescript compilationIs there a way to include additional properties

Currently, I am employing JSX syntax in my TypeScript node.js project (without relying on React). Within my tsconfig, I have specified my custom jsxFactory { "compilerOptions": { ... "jsxFactory": "myJSXFactory", ...

Is there a way to inform TypeScript that the process is defined rather than undefined?

When I execute the code line below: internalWhiteList = process.env.INTERNAL_IP_WHITELIST.split( ',' ) An error pops up indicating, Object is possibly undefined. The env variables are injected into process.env through the utilization of the mod ...

Circular structure error occurred when attempting to convert an object to JSON, starting at an object constructed with the constructor 'Object'

I am facing an issue where I need to update a Medico from the collection, and I have successfully destructured the data of the Medico's name and email. Additionally, I have obtained the ID of the assigned hospital. However, I am having trouble sendin ...

Guide: Displaying components within a Vue2 string

Within my Vue2 component, I am working with a string that looks something like this: <template> <div><h1>Hello World</h1> <div v-html="testString"></div> </div> </template> <script> exp ...

Converting a numeric value to a string in JavaScript involves using

How can I display the message below in the snippet? The minimum value is 0.00000001. Instead of The minimum value is 1e-8 var minValue = 0.00000001; var message = "The minimum value is " + minValue.toString(); ...

What is the best way to integrate a Vue component into the HTML of another Vue component?

Currently, I am dealing with a large HTML string that is being loaded into a div within my Vue component. This HTML string comes from a WYSIWYG editor and was previously handled using v-html, which worked well. However, I now have scenarios where I need t ...

The concept of RxJS's catchError function involves the return of a versatile

It's interesting that catchError is returning an Observable union type as Observable<{} | Page} instead of just Observable<Page>. The error message from the compiler reads: Type 'Observable<{} | Page>' is not assignable to t ...

The `pubnub removeListener` function does not get triggered when the `useEffect` hook

Operating a single chat works perfectly; however, upon entering and exiting a chat screen before re-entering it, double messaging occurs, and the listener remains active despite being placed in the return of useEffect. I attempted the solution provided in ...

Is there a syntax issue in your Angular/Typescript code?

I am currently developing a login system using Angular and Firestore, and here is the code I have written: import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import * as firebase from 'firebase/ ...

Different ways to combine the use of backgroundColor and backgroundImage for layering effects

Looking for assistance with using a background image and color together in a React/Typescript project with Material UI. My goal is to have a transparent color over an image. Below is the code snippet: return ( //Todo need to do one more level of refactor ...

Typescript: How to Ensure Tuple Type is Explicit and Not Combined

I have a code snippet that defines a person object and a function to get its values: const person = { name: "abc", age: 123, isHere: true }; const getPersonValues = () => { return [person.name, person.age, person.isHere]; }; const [n ...