Vite Plugin Hooks

A Vite plugin is an object of hooks that fire as Vite resolves, loads, and transforms each module. Run a module through the pipeline and watch the hooks light up in order.

idle

A real plugin: virtual module

The classic pattern — expose build-time data to your app through an importable "virtual" module that doesn't exist on disk:

function buildInfoPlugin() {
  const id = "virtual:build-info";
  const resolvedId = "\0" + id; // \0 marks it virtual (convention)

  return {
    name: "build-info",
    enforce: "pre", // run before core plugins

    resolveId(source) {
      if (source === id) return resolvedId; // "yes, I own this id"
    },

    load(thisId) {
      if (thisId === resolvedId) {
        // generate the module source on the fly
        return \`export const builtAt = "\${new Date().toISOString()}"\`;
      }
    },

    transform(code, fileId) {
      // optionally rewrite real files here
      return null; // null = leave unchanged
    },
  };
}

// app code — imports something that never existed on disk:
import { builtAt } from "virtual:build-info";