Vite library mode: build.lib requirements, trust boundaries, and release checks
Use Vite build.lib to publish reusable components without bundling peer dependencies or leaking internal modules.
16 Jan 2026, 00:20 UTC

The problem and the takeaway
Vite’s default build assumes you are shipping a web application. It reads index.html, bundles the application graph, and produces assets meant to be served by a static host. If you run that same command on a component library or SDK, the output bundles your dependencies and exposes every module reachable from the HTML entry, not just your public API.
Consumers who install that package receive a second copy of React or Vue, and your internal helpers become part of the public bundle surface. The takeaway is to use build.lib with a single entry point, externalize peer dependencies so consumers supply them, and ship TypeScript declarations through a separate step because Vite does not emit .d.ts files.
Requirements
- A single entry file that exports only the public API.
- Distributable formats, typically ES modules and UMD, so bundlers and script tags can consume the package.
- Peer dependencies such as React or Vue listed as external and declared in
peerDependencies, so the consumer’s copy is used. - CSS handled without global leakage into the host application.
- Type declarations available to consumers, produced outside Vite’s build step.
Smallest suitable design
This Vite config builds a React component library from src/index.ts into ES and UMD formats. It externalizes react and react-dom, meaning those modules are not bundled into the library output.
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';
export default defineConfig({
plugins: [react()],
build: {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
name: 'ComponentLibrary',
formats: ['es', 'umd'],
},
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
},
},
});
Replace src/index.ts with your library entry point. The name value becomes the global variable for the UMD build. The external array tells Rollup to leave those imports unresolved; consumers must install them.
Run the build from the library project root:
vite build
No special permissions are required. The command overwrites the dist directory. Expected output includes dist/index.es.js, dist/index.umd.js, and any generated CSS assets. Confirm that the bundle does not contain React source code before publishing.
Point package.json at the generated formats:
{
"main": "dist/index.umd.js",
"module": "dist/index.es.js",
"exports": {
".": {
"import": "./dist/index.es.js",
"require": "./dist/index.umd.js"
}
},
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
Trust and data boundaries
The library entry point is the trust boundary for bundle contents. Vite includes only modules reachable from that entry, so unexported internal helpers remain private. Do not assume privacy for modules imported by the entry; anything in the import graph ships to consumers.
Externalizing peer dependencies transfers version resolution to the consumer. If your library is built against React 18 but the consumer installs React 17, the host application may fail at runtime because the library expects APIs or behavior from a different major version. Declare peer dependency ranges carefully and test against the oldest supported consumer version.
CSS is another boundary. Global or unscoped styles imported by library components can leak into the consuming page and override host styles. Prefer CSS modules or scoped styles, and inspect the generated CSS for generic class names before release.
Finally, dist is not a development server. It lacks Vite HMR, proxying, and the @vite/client bridge. Serving dist directly as a runtime will not reproduce the development experience.
Operational checks
- Run
vite buildin the library project root. - List
distand confirm ES and UMD files exist. - Open the ES bundle and search for your peer dependency names. They should appear as import statements, not bundled source.
- Run
npm packornpm publish --dry-runand inspect the tarball contents. - Install the package in a consumer project, import a component, and verify the peer dependency resolves from the consumer’s
node_modulesrather than from inside the library package. - Run the consumer’s TypeScript check and confirm module resolution finds your declarations.
Failure modes
- Duplicate dependency copies: Forgetting
externalbundles React into the library. Two React instances in one page break hooks and context. - Missing declarations: Vite emits JavaScript only. Consumers using TypeScript see "Could not find a declaration file" errors unless you add a declaration step.
- Style leakage: Unscoped library CSS overrides host component styles.
- Wrong runtime expectations: Serving
distwith a static file server and expecting HMR or proxy behavior. - Peer version mismatch: Library built against one major version of a framework, consumer using another.
Conditions that change the design
- Multiple public entry points: use
build.rollupOptions.inputinstead ofbuild.lib.entry, or switch to a custom Rollup configuration. - Type declarations required: add
vite-plugin-dtsor runtsc -das a separate step. Vite does not generate.d.tsfiles. - Consumers without a bundler: use the
iifeformat and load the UMD/IIFE file through a script tag. - The project is an application, not a library: do not use
build.lib; use Vite’s default application build withindex.html. - CSS output preference: set
build.lib.cssCodeSplitto split styles into separate assets or inline them into JavaScript.
Rollback and state
vite build changes filesystem state by writing to dist. Because dist is normally build output and not source, rollback is to delete the directory and restore the previously published package version:
rm -rf dist
Do not delete dist if it contains the only copy of artifacts you intend to publish; verify the previous release is available in your registry or git history first.
Limitation to verify before release
Vite does not emit TypeScript declaration files. After building, run:
find dist -name '*.d.ts'
Expect no results unless you have added a declaration plugin. If the command returns nothing, add vite-plugin-dts or a tsc step before publishing, then verify the consumer project can resolve types.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.