Setting up a Project with Vite
- #typescript
- #vite
- #tooling
- #setup
In questa lezione
1. What is Vite?
Vite (French for “fast”) is a modern frontend build tool. It provides two things most web projects need:
- an extremely fast development server, with instant startup and near-instant hot module reload (changes show up in the browser almost immediately, without a full page reload);
- an optimized production build command, which bundles and minifies your code for deployment.
Manually configuring a compiler, a bundler, a dev server, and TypeScript support from scratch is a lot of tedious setup. Vite ships with sensible defaults for all of this out of the box, which is why it has become one of the most popular ways to start a new frontend (or general TypeScript) project.
Suggerimento
Vite isn’t tied to one framework. It has official templates for vanilla TypeScript, React, Vue, Svelte, and more — the underlying dev server and build pipeline are the same either way.
2. Scaffolding a project from a template
Vite can generate a ready-to-use project structure from a template, so you don’t start from an empty folder:
npm init vite@latest vite_project -- --template vanilla-ts
Let’s break this command down:
npm init vite@latestfetches and runs the latest version of Vite’s project-creation tool (viacreate-vite) without installing anything globally.vite_projectis the name of the folder that will be created for your new project.-- --template vanilla-tspasses extra arguments throughnpm initto the underlying tool:--template vanilla-tstells it to use the vanilla TypeScript template — plain TypeScript with no UI framework attached, ideal for learning the language itself.
Nota
The double dash -- is npm’s way of saying “everything after this belongs to the program being run, not to npm itself.” Without it, npm might try to interpret --template as its own flag.
Other common templates follow the same pattern, for example react-ts, vue-ts, or svelte-ts — swap vanilla-ts for any of these if you want a specific framework instead.
3. Entering and running the project
cd vite_project
Move into the freshly generated folder. Vite has already created a working project structure for you, roughly:
vite_project/
├── index.html
├── package.json
├── tsconfig.json
├── src/
│ ├── main.ts
│ └── style.css
└── public/
From here, the usual flow is:
npm install # install the project's dependencies (Vite itself, etc.)
npm run dev # start the development server
npm run dev starts Vite’s dev server (typically at http://localhost:5173) and watches your files: every time you save, the browser updates almost instantly.
When you’re ready to ship:
npm run build
This produces an optimized, minified bundle of plain JavaScript/HTML/CSS in a dist/ folder, ready to be deployed to any static hosting.
4. Why Vite over a manual tsc setup
Compare this to the manual workflow from the previous two lessons (npm init, install typescript, tsc --init, then tsc + node by hand):
- Vite gives you an HTML entry point, a dev server, hot reload, and a production bundler for free.
- A manual
tscsetup only compiles TypeScript to JavaScript — it does not bundle assets, serve HTML, or optimize a build for the browser. - For pure Node.js scripts/back-end code, the manual setup (or
tsx) is often simpler and lighter. For anything that runs in a browser, Vite (or a similar bundler) is usually the better starting point.
5. Further reading
6. Quiz
Mettiti alla prova
0/6 risposte
What is Vite primarily used for?
In
npm init vite@latest vite_project -- --template vanilla-ts, what doesvanilla-tsrefer to?What is the purpose of the
--before--templatein the init command?Which command starts Vite's development server?
What does
npm run buildproduce in a Vite project?Why might you choose a manual tsc setup instead of Vite?
7. Exercises
Scenario: You want to build a small interactive TypeScript demo that runs in the browser, with live reload while you develop it.
Task:
- Scaffold a new project with
npm init vite@latest my-demo -- --template vanilla-ts. cdinto the folder, runnpm install, thennpm run dev, and open the printed local URL in your browser.- Edit
src/main.tsto change some text or add aconsole.log, save, and observe how quickly the browser updates. - Run
npm run buildand inspect the generateddist/folder’s contents. - Compare this workflow to the manual
tsc/nodeworkflow from the previous lesson — write down two concrete differences you noticed.
Learning objective: understand what a bundler/dev-server tool like Vite adds on top of the raw TypeScript compiler, and get hands-on with scaffolding a real project from a template.