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
- Create a new Vite project
bash
npm create vite@latest my-vue-app --template vue-ts- Change directory to the new project
bash
cd my-vue-app- Install dependencies
bash
npm install- Start the development server
bash
npm run dev- Open your browser and navigate to
http://localhost:5173/to see your new Vite app running. - Build the project for production
bash
npm run build- Preview the production build
bash
npm run previewOpen your browser and navigate to
http://localhost:4173/to see the production build.To deploy the app, you can copy the contents of the
distfolder to your web server.To add a new component, create a new file in the
src/componentsdirectory and import it into your main app file (e.g.,src/main.ts).To add a new route, you can use a routing library like
vue-routerand configure it in your main app file.To add state management, you can use a state management library like
PiniaorVuexand configure it in your main app file.To add CSS, you can create a new CSS file in the
src/assetsdirectory and import it into your main app file.
Add Tailwind CSS to Vite
- Install Tailwind CSS and its dependencies
bash
npm install tailwindcss @tailwindcss/vite- 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()
]
})- Import Tailwind CSS by Add an @import to your CSS file that imports Tailwind CSS.
css
/* src/stylee.css */
@import "tailwindcss";- Install Vitest for testing
bash
npm install -D vitest
npm install -D vitest @testing-library/vue @testing-library/jest-dom- 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"
}
}- 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()
})
})- Run the tests
bash
npm run test