Chuyển đến nội dung

Bài 29 · Frameworks and SSR

Framework Templates and Setup

Start a Vite project for React, Vue, Svelte or vanilla JS with create-vite, see what each template adds, and configure TypeScript, JSX and path aliases correctly.

Vite does not ship a framework. It is a build tool that framework authors plug into, which is why almost every modern framework offers a Vite-based starter. This lesson shows how to scaffold a project for the main frameworks, what the templates actually contain, and how to configure the pieces that trip people up: TypeScript, JSX, path aliases and environment types.

The scaffolding commands and configs here are real and read-only. Two stand-ins in plain JavaScript show how templates are described and how path aliases resolve.

Scaffolding with create-vite

Terminal
npm create vite@latest my-app
# or pass the template directly
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm run dev

Available templates include vanilla, vanilla-ts, vue, vue-ts, react, react-ts, react-swc, react-swc-ts, preact, lit, svelte, solid and qwik. The -ts suffix means TypeScript; -swc means the SWC compiler instead of Babel for React.

JS Stand-in: what a template contains

Press Run (or Ctrl + Enter) to see the console output here.

No output.

React

The React template uses @vitejs/plugin-react:

JavaScript
// vite.config.js (real Vite, read-only)
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
});

It gives you the automatic JSX runtime (no import React needed), Fast Refresh and dev-only checks. Entry code:

JSX
// src/main.jsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.jsx';

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>,
);

Notice the .jsx extension on imports. Vite compiles JSX only in files with .jsx or .tsx extensions by default; JSX in a .js file fails with a confusing parse error. Rename the file, or (not recommended) configure esbuild: { loader: 'jsx', include: /src\/.*\.js$/ }.

Vue

JavaScript
// vite.config.js (real Vite)
import vue from '@vitejs/plugin-vue';
export default defineConfig({ plugins: [vue()] });

The plugin compiles single-file components. Remember Vue's templates need the compiler: when importing the full browser build directly (for a CDN-style demo) use the full build, but with the plugin templates are compiled at build time and the smaller runtime-only build is used.

Svelte, Solid, Preact

They follow the same pattern: one plugin, one plugins entry. Preact is often aliased to React-compatible packages with @preact/preset-vite, which sets react to preact/compat for you.

TypeScript setup

Vite transpiles TypeScript with esbuild: it only removes types, it does not check them. A typical tsconfig.json for an app:

JSON
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "lib": ["ES2022", "DOM", "DOM.Iterable"],
    "jsx": "react-jsx",
    "strict": true,
    "isolatedModules": true,
    "noEmit": true,
    "skipLibCheck": true,
    "types": ["vite/client"]
  },
  "include": ["src"]
}

Why these matter:

  • moduleResolution: "bundler" matches how Vite resolves imports (extensionless paths allowed).
  • isolatedModules: true makes TypeScript warn about patterns that a one-file-at-a-time compiler like esbuild cannot handle, such as re-exporting a type without export type.
  • noEmit: true: tsc is only the type checker; Vite produces the output. Add "typecheck": "tsc --noEmit" to your scripts and run it in CI.
  • vite/client types give you import.meta.env, import.meta.hot and imports such as *.svg and *.css.

Type your own environment variables in src/vite-env.d.ts:

TypeScript
/// <reference types="vite/client" />

interface ImportMetaEnv {
  readonly VITE_API_URL: string;
}
interface ImportMeta {
  readonly env: ImportMetaEnv;
}

Path aliases

Long relative imports like ../../../components/Button are brittle. Define an alias in both places: Vite for resolution, TypeScript for the editor.

JavaScript
// vite.config.js (real Vite)
import { fileURLToPath, URL } from 'node:url';

export default defineConfig({
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
});
JSON
// tsconfig.json (fragment)
{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["src/*"] } } }
JS Stand-in: resolving an alias

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that @vitejs/plugin-react is not rewritten, because the alias must match @ exactly or @/. A sloppy prefix check would break scoped packages, which is a real bug people hit with hand-written aliases.

Choosing a template

  • Learning or a small prototype: react-ts, vue-ts or vanilla.
  • Need routing, data loading and SSR from the start: use a full framework built on Vite (React Router framework mode, Nuxt, SvelteKit, SolidStart, Astro). Next lesson: SSR Fundamentals.
  • Building a package: Library Mode.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does the -ts suffix mean in create-vite templates?

    react-ts, vue-ts and the like are the TypeScript variants of the template.
  2. 2. Does Vite type-check your TypeScript?

    esbuild removes types without checking them, so type errors are found by tsc or a checker plugin.
  3. 3. Why does JSX in a .js file fail by default?

    File extensions decide the loader, so rename the file to .jsx or .tsx.
  4. 4. Where must a path alias such as @ be configured?

    Vite needs the alias to resolve imports, and TypeScript needs paths to understand them.

Summary

  • npm create vite@latest scaffolds templates; frameworks plug in through one plugin.
  • React uses plugin-react and needs .jsx/.tsx; Vue uses plugin-vue.
  • Vite only strips TypeScript types: keep tsc --noEmit in your scripts and CI.
  • Set moduleResolution: "bundler", isolatedModules and vite/client types.
  • Configure aliases in both Vite and tsconfig, with an exact-prefix match.

Next: SSR Fundamentals

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.