TExcelTAD
Home › Vite › Vite index.html as Entry

Vite index.html as Entry

In many older toolchains index.html was an afterthought hidden in a public/ folder, and a JavaScript file was the "entry". Vite flips this around: index.html is the entry point, it lives in the project root, and it is part of your source code.

A minimal entry

html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Vite App</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.ts"></script>
  </body>
</html>

Three things to notice:

  1. The script uses type="module" — Vite only processes module scripts.
  2. The src points straight at a TypeScript file. Browsers can't run TypeScript, but Vite transforms it when the browser asks for it.
  3. Paths starting with / are resolved from the project root (the folder with index.html), not from your file system root.

What Vite does with the HTML

Phase What happens
Dev (vite) Serves the HTML, injects the HMR client script (/@vite/client), and rewrites URLs so imports resolve
Build (vite build) Parses the HTML, bundles every <script type="module">, processes <link rel="stylesheet"> and asset URLs (<img src>, <link rel="icon">…), then writes dist/index.html with hashed file names

After a build, the same file looks like this:

html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My Vite App</title>
    <script type="module" crossorigin src="/assets/index-C3k9pQ2x.js"></script>
    <link rel="stylesheet" crossorigin href="/assets/index-DiwrgTda.css">
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

The script moved into <head> (module scripts are deferred, so that is safe) and the CSS that main.ts imported became a real <link>.

Module scripts in the browser

type="module" changes how a script behaves. This live page shows the differences: module scripts run after the document is parsed, have their own scope, and run in strict mode.

html · try it yourself
<!doctype html>
<html>
  <body style="font-family: system-ui">
    <ul id="log"></ul>
    <script type="module">
      const log = (t) => document.querySelector('#log').insertAdjacentHTML('beforeend', `<li>${t}</li>`);
      log('module: #after exists? ' + (document.querySelector('#after') !== null));
      log('module: strict mode? ' + (function () { return this === undefined; })());
      var moduleVar = 1;
      log('module: var leaked to window? ' + ('moduleVar' in window));
    </script>
    <p id="after">I am below the script tag.</p>
  </body>
</html>

All three answers are the reason Vite insists on modules: predictable order, isolation, and import support.

Inline modules and multiple scripts

You can also write an inline module, and Vite will process its imports too:

html
<script type="module">
  import { setupCounter } from '/src/counter.ts';
  setupCounter(document.querySelector('#counter'));
</script>

A classic script without type="module" is left alone: Vite will not bundle it, and its src should then point to a file in public/.

Multiple HTML pages

Because HTML files are entries, a multi-page site is just several HTML files. In dev, http://localhost:5173/about/ serves about/index.html automatically. For the build you list them in the config:

js
// vite.config.js
import { defineConfig } from 'vite';
import { resolve } from 'node:path';

export default defineConfig({
  build: {
    rolldownOptions: {
      input: {
        main: resolve(import.meta.dirname, 'index.html'),
        about: resolve(import.meta.dirname, 'about/index.html'),
      },
    },
  },
});

Note: In Vite 8 the bundler options live under build.rolldownOptions. The older name build.rollupOptions still works as a deprecated alias, so you will see both in tutorials.

Changing the root

By default the project root is the current working directory. If your HTML lives elsewhere — for example web/index.html — pass the folder as an argument or set root in the config:

bash · try it yourself
npx vite web

  VITE v8.3.4  ready in 221 ms

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

Now / maps to web/index.html, and absolute paths like /src/main.ts resolve to web/src/main.ts.

URL resolution, simulated

How does a root-relative URL map to a file? Conceptually it is just joining the project root with the request path, which this small function imitates:

js · try it yourself
function resolveRequest(root, url) {
  const path = url.split('?')[0];
  const file = path.endsWith('/') ? path + 'index.html' : path;
  return root.replace(/\/$/, '') + file;
}

console.log(resolveRequest('/home/me/my-app', '/'));
console.log(resolveRequest('/home/me/my-app', '/about/'));
console.log(resolveRequest('/home/me/my-app', '/src/main.ts?t=1'));
console.log(resolveRequest('/home/me/my-app/web/', '/src/main.ts'));
/home/me/my-app/index.html
/home/me/my-app/about/index.html
/home/me/my-app/src/main.ts
/home/me/my-app/web/src/main.ts

Test yourself

Which script tags does Vite bundle?

  1. All script tags
  2. Only <script type="module">
  3. Only scripts in public/
  4. Only inline scripts

In <script type="module" src="/src/main.ts">, what is / relative to?

  1. The disk root
  2. The public/ folder
  3. The project root (where index.html is)
  4. node_modules

How do you build a multi-page app with Vite?

  1. It is not possible
  2. List each HTML file as an input in the bundler options
  3. Use one HTML file per git branch
  4. Rename the pages to .vite