Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

Vite Practice: Exercises and Interview Questions

Test your Vite knowledge with hands-on exercises you can run, a debugging checklist and common interview questions about HMR, builds, plugins, SSR and performance.

You have read about the dev server, plugins, builds and SSR. This final lesson turns that knowledge into practice: four exercises with runnable solutions that rebuild small pieces of Vite's machinery, a debugging drill, and a set of interview questions with short answers. Try each exercise before opening the solution.

Vite is not available in our runners, so exercises use plain JavaScript that models the logic. Config examples are read-only.

Exercise 1: write a plugin's transform logic

Task. A plugin should replace the token __APP_VERSION__ in .js and .ts files (not in .css, not in node_modules) and ignore query strings in ids. Write transform(code, id) and test it.

JS Exercise 1: version-replacing transform

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

No output.

In real Vite you would usually use the define option for this: define: { __APP_VERSION__: JSON.stringify(pkg.version) }. Knowing when a config option is enough is part of the skill.

Exercise 2: a manifest-based preload list

Task. Given a manifest, return the files to load for an entry, with each shared chunk listed once, dependencies before dependents. Handle shared imports without looping forever.

JS Exercise 2: ordered preload list

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

No output.

Post-order traversal gives dependencies first. The seen set prevents duplicates and infinite loops on cycles.

Exercise 3: classify a build warning

Task. Write a function that reads a list of chunk sizes (KB) and the limit, and returns advice per chunk: ok, consider splitting (over limit) or urgent (over twice the limit).

JS Exercise 3: chunk size advice

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

No output.

Exercise 4: HMR boundary idea

Task. In HMR, when module c.js changes, an update propagates up through importers until it reaches a module that accepts the update (import.meta.hot.accept). If it reaches the entry without a boundary, the page reloads. Implement findBoundary(changed).

JS Exercise 4: finding the HMR boundary

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

No output.

theme.js is imported only by the entry and no module accepts it, so editing it triggers a full reload. This is the reason "why does my page reload instead of hot updating?" has an answer in your module graph.

Debugging drill

For each symptom, write down the first thing you would check, then compare with the list.

  1. Blank page after deploying to /app/. (Check base.)
  2. Uncaught ReferenceError: process is not defined. (Replace with import.meta.env; check the library.)
  3. Works in npm run dev but breaks in the production build. (Run vite build && vite preview; look for CommonJS interop, dynamic imports with variables, and code that depends on dev-only behavior.)
  4. A new dependency causes a reload loop. (Add it to optimizeDeps.include; clear node_modules/.vite.)
  5. Plugin transform runs on files you did not intend. (Filter on the id with createFilter, check include/exclude.)
  6. Type errors appear only in CI. (Local editor did not run tsc; add the typecheck script to your local workflow.)
  7. Changing an env var has no effect. (Restart the dev server; check the VITE_ prefix and the mode's .env.[mode] file.)

Interview questions

What makes Vite's dev server fast? It does no bundling of your source: files are served as native ES modules on demand, and each module is transformed (with esbuild for TS/JSX) only when the browser requests it. Dependencies are pre-bundled once with esbuild and cached. HMR updates only the changed module and the chain to its boundary.

What is dependency pre-bundling and why is it needed? It converts CommonJS or UMD dependencies to ESM and merges packages with many internal modules into one file, so the browser does not make hundreds of requests for something like lodash-es.

Dev uses esbuild, build uses Rollup. Why, and what is the downside? Esbuild is extremely fast for transforms; Rollup gives better chunking and a mature plugin ecosystem for production. The downside was two pipelines that could behave differently, so some bugs appear only in one mode. Newer Vite versions move toward a single Rolldown-based pipeline to reduce this gap.

What are the main plugin hooks? resolveId, load and transform for the module pipeline; config, configResolved and configureServer for setup; transformIndexHtml; handleHotUpdate for HMR; generateBundle for output.

How do environment variables work? Files .env, .env.[mode] and their .local variants are loaded; only variables starting with VITE_ are exposed on import.meta.env. They are replaced statically at build time, so they are not secret.

How do you split a bundle? Dynamic import() (route-level lazy loading), manualChunks for vendor groups, and removing heavy dependencies found with a visualizer.

What is the difference between public/ and src/assets? Files in public/ are copied as-is and referenced by absolute URL, with no hashing. Assets imported from src are processed, may be inlined, and get hashed names.

How do you support old browsers? Lower build.target, polyfill the APIs you use, and add @vitejs/plugin-legacy only when module-less browsers must be supported.

Describe an SSR setup with Vite. Two entries (client and server), dev server in middleware mode with ssrLoadModule, production with vite build plus vite build --ssr, a server that imports the server bundle, state serialized safely into the HTML, and hydration on the client. Frameworks package all of this.

Vite vs webpack? Vite favors native ESM in dev for instant start and simple config; webpack has a larger legacy plugin ecosystem and flexibility for unusual setups. For new projects Vite is usually the default; for huge legacy webpack apps migration cost matters.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A module is changed and no importer up the chain accepts the update. What happens?

    Updates bubble up the import chain; without an accept boundary the client falls back to a full reload.
  2. 2. What is the quick way to expose an app version to client code without writing a plugin?

    define statically replaces identifiers at build time.
  3. 3. Which command checks the production build locally?

    Build first, then preview the output of dist/.
  4. 4. Why is a dependency pre-bundled in dev?

    Esbuild turns dependencies into a few ESM files cached in node_modules/.vite.
  5. 5. Where do files that must keep their exact names and URLs belong?

    public/ is copied without processing and served from the root.

Summary

  • You modeled a transform, a manifest traversal, chunk advice and HMR boundaries in plain JavaScript.
  • Debugging starts with the question: dev only, build only, or both?
  • Be ready to explain pre-bundling, esbuild vs Rollup, plugin hooks, env variables and SSR.
  • Keep building: add a project of your own and publish or deploy it.

Back to the course: Vite