Skip to main content
Introducing packages.sweber.dev
Documentation menuVite plugin

Vite plugin

Build the index as part of every Vite build, with a dev server and an embedding cache.

@weber-development/cosine-vite replaces the cosine build step. The index is always in sync with the docs, also in vite dev.

vite.config.ts

import cosine from "@weber-development/cosine-vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [cosine({ docs: "docs", baseUrl: "/docs" })],
});
import "@sweberdev/cosine/element";
import { indexUrl } from "virtual:cosine";

document.querySelector("cosine-search")?.setAttribute("index", indexUrl);

For TypeScript, add "types": ["@weber-development/cosine-vite/client"] to tsconfig.json.

What it does

  • Build: writes cosine/cosine-index.json and cosine/cosine-vectors.bin into the output folder. In tools that run a server and a client build (Astro, SvelteKit) the files go to the client build.
  • Dev: serves the index from memory and rebuilds it when a file in docs changes. The browser receives the Vite event cosine:update.
  • Cache: vectors are stored in node_modules/.cache/cosine/embeddings.json by section text. A rebuild after editing one page embeds only that page's changed sections. Cache that folder in CI to make builds fast.

Frameworks

astro.config.mjs

import cosine from "@weber-development/cosine-vite";
export default defineConfig({ vite: { plugins: [cosine({ docs: "src/content/docs", baseUrl: "/" })] } });

.vitepress/config.ts

import cosine from "@weber-development/cosine-vite";
export default defineConfig({ vite: { plugins: [cosine({ docs: ".", baseUrl: "/", exclude: ["node_modules", ".vitepress"] })] } });

Options

OptionDefault
docsFolder with the pages, relative to the Vite root
baseUrl/URL prefix of the pages
modelenglishenglish, multilingual, a model id, or false for keyword search only
embedderCustom embedder instead of model
outDircosineFolder of the index below the site base
cacheFilenode_modules/.cache/cosine/embeddings.jsonEmbedding cache, or false
excludePaths below docs to skip
maxChars, code, minLevel, maxLevelChunking, as in Building the index

cachedEmbedder(embedder, file) is exported too, for your own build scripts.