TExcelTAD
Home › Vite › Vite Config Basics

Vite Config Basics

Vite works without configuration, but sooner or later you will want to add a plugin, change the port, or set a base path. All of that goes into a config file in the project root.

The config file

Vite looks for one of these, in order: vite.config.js, vite.config.mjs, vite.config.ts, vite.config.cjs, vite.config.mts, vite.config.cts.

js
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  server: { port: 3000 },
  build: { outDir: 'build' },
});

defineConfig does nothing at runtime — it just returns its argument — but it gives your editor full type information, so you get autocompletion and inline docs even in a .js file.

This repository's own config is a good real-world example:

ts
// vite.config.ts (from the ExcelTAD project)
import { defineConfig } from 'vite';

// base './' keeps the build relocatable: dist/ works from any sub-path or even file:// hosting.
export default defineConfig({
  base: './',
  build: {
    target: 'es2022',
    chunkSizeWarningLimit: 1500,
  },
});

The most used options

Option Default Purpose
root process.cwd() Folder containing index.html
base '/' Public base path when deployed
publicDir 'public' Static files copied as-is
plugins [] Array of plugins
resolve.alias — Path shortcuts like @ → src
define — Global constant replacements
css — CSS Modules, pre-processor and PostCSS options
server — Dev server: port, host, open, proxy
preview — Same for vite preview
build — outDir, target, sourcemap, minify, rolldownOptions
envPrefix 'VITE_' Which env vars are exposed to the client

Conditional config

Export a function instead of an object to adapt to the command or mode. Vite calls it with { command, mode, isSsrBuild, isPreview }.

js
import { defineConfig } from 'vite';

export default defineConfig(({ command, mode }) => {
  if (command === 'serve') {
    return { server: { open: true } };
  }
  return {
    build: { sourcemap: mode === 'staging' },
  };
});

Here is the same logic as a plain function you can run, showing which config each command gets:

js · try it yourself
const defineConfig = (c) => c; // the real one is just as simple at runtime

const config = defineConfig(({ command, mode }) => ({
  base: command === 'build' ? '/app/' : '/',
  server: { port: 5173, open: command === 'serve' },
  build: { sourcemap: mode !== 'production', minify: mode === 'production' },
}));

const runs = [
  { command: 'serve', mode: 'development' },
  { command: 'build', mode: 'production' },
  { command: 'build', mode: 'staging' },
];
for (const env of runs) {
  const c = config(env);
  console.log(`${env.command}/${env.mode}:`, c.base, c.server.open, c.build.sourcemap, c.build.minify);
}
serve/development: / true true false
build/production: /app/ false false true
build/staging: /app/ false true false

The function can also be async, for example to read a file or fetch data before returning the config.

How options are merged

Plugins and framework tools often provide part of the config and let you add the rest. Vite exports mergeConfig for that — objects are merged deeply, arrays are concatenated. A simplified version:

js · try it yourself
function mergeConfig(base, extra) {
  const out = { ...base };
  for (const [key, value] of Object.entries(extra)) {
    if (Array.isArray(value) && Array.isArray(base[key])) out[key] = [...base[key], ...value];
    else if (value && typeof value === 'object' && base[key] && typeof base[key] === 'object')
      out[key] = mergeConfig(base[key], value);
    else out[key] = value;
  }
  return out;
}

const shared = { plugins: ['react'], server: { port: 5173 }, build: { outDir: 'dist' } };
const local = { plugins: ['inspect'], server: { open: true } };
console.log(mergeConfig(shared, local));
{"plugins":["react","inspect"],"server":{"port":5173,"open":true},"build":{"outDir":"dist"}}

define: compile-time constants

define replaces identifiers with values in your code at build time. Values must be valid JavaScript expressions, so wrap strings in JSON.stringify:

js
export default defineConfig({
  define: {
    __APP_VERSION__: JSON.stringify('2.1.0'),
    __BUILD_DATE__: JSON.stringify('2026-10-16'),
  },
});
js
// anywhere in src/
footer.textContent = `v${__APP_VERSION__}`;

For TypeScript, declare them: declare const __APP_VERSION__: string;.

Using a different config file

bash · try it yourself
npx vite --config vite.staging.config.js

  VITE v8.3.4  ready in 301 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help

Vite restarts the dev server automatically when the config file (or a file it imports) changes.

Tip: Most CLI flags mirror config options: --port, --host, --open, --base, --outDir, --mode. Flags override the config file, which is handy for one-off runs.

Debugging the resolved config

Run with --debug config to see the final config, or add a tiny plugin with a configResolved(config) hook and log what you need. You'll learn about plugin hooks in the Plugins lesson.

Test yourself

What does defineConfig do at runtime?

  1. Validates and throws on bad options
  2. Returns its argument unchanged; it exists for type hints
  3. Starts the dev server
  4. Merges with defaults

What arguments does a function-style config receive?

  1. (req, res)
  2. { command, mode, ... }
  3. The package.json
  4. Nothing

Why wrap strings in JSON.stringify inside define?

  1. For performance
  2. Because define values are inserted as raw code, so strings need quotes
  3. To encrypt them
  4. Vite requires JSON files