Skip to content

Build a Vue-Typescript application using Vite

This guide will help you set up a Vue-Typescript application using Vite, a modern frontend build tool.

Prerequisites

  • Node.js (v14 or later)
  • npm (v6 or later)
  • Basic knowledge of Vue.js and TypeScript
  • Basic knowledge of Vite
  • Basic knowledge of Tailwind CSS
  • Basic knowledge of Vitest
  • Basic knowledge of Testing Library
  1. Create a new Vite project
bash
npm create vite@latest my-vue-app --template vue-ts
  1. Change directory to the new project
bash
cd my-vue-app
  1. Install dependencies
bash
npm install
  1. Start the development server
bash
npm run dev
  1. Open your browser and navigate to http://localhost:5173/ to see your new Vite app running.
  2. Build the project for production
bash
npm run build
  1. Preview the production build
bash
npm run preview
  1. Open your browser and navigate to http://localhost:4173/ to see the production build.

  2. To deploy the app, you can copy the contents of the dist folder to your web server.

  3. To add a new component, create a new file in the src/components directory and import it into your main app file (e.g., src/main.ts).

  4. To add a new route, you can use a routing library like vue-router and configure it in your main app file.

  5. To add state management, you can use a state management library like Pinia or Vuex and configure it in your main app file.

  6. To add CSS, you can create a new CSS file in the src/assets directory and import it into your main app file.

Add Tailwind CSS to Vite

  1. Install Tailwind CSS and its dependencies
bash

npm install tailwindcss @tailwindcss/vite
  1. Configure the Vite plugin, add the @tailwindcss/vite plugin to your Vite configuration.
bash
// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    tailwindcss()
  ]
})
  1. Import Tailwind CSS by Add an @import to your CSS file that imports Tailwind CSS.
css
/* src/stylee.css */
@import "tailwindcss";
  1. Install Vitest for testing
bash
npm install -D vitest

npm install -D vitest @testing-library/vue @testing-library/jest-dom
  1. Add vitest in package.json
json
{
  "scripts": {
    "test": "vitest",
    "test:watch": "vitest --watch",
    "test:coverage": "vitest --coverage",
    "test:ci": "vitest --ci",
    "test:debug": "vitest --debug"
  }
}
json
{
  "devDependencies": {
    "vitest": "^0.1.0",
    "@testing-library/vue": "^6.0.0",
    "@testing-library/jest-dom": "^5.16.4"
  }
}
  1. Create a test file
bash
// src/components/HelloWorld.spec.ts
import { render } from '@testing-library/vue'
import HelloWorld from './HelloWorld.vue'
import '@testing-library/jest-dom'
import { describe, it, expect } from 'vitest'
describe('HelloWorld.vue', () => {
  it('renders props.msg when passed', () => {
    const { getByText } = render(HelloWorld, {
      props: { msg: 'Hello Vite + Vue 3 + TypeScript!' }
    })
    expect(getByText('Hello Vite + Vue 3 + TypeScript!')).toBeInTheDocument()
  })
})
  1. Run the tests
bash
npm run test