skip to content

In Vite 8 library mode, what does `build.lib` emit by default for a single-entry widget, and how do you keep the framework out of its bundle?

level: seniorimportance: should knowfreq 7%

answer

  1. no HTML entry this time
  2. two formats by default
  3. a global name the UMD needs
  4. externals sit under the bundler's options
  5. assets inlined, one stylesheet

basics

~20 s

With one entry, build.lib emits an ES module and a UMD file named after package.json's name, plus one CSS file if it imports CSS, with every asset inlined. The framework is bundled unless listed in build.rolldownOptions.external, with a UMD global in output.globals.

solid answer

~40 s

`build.lib` switches `vite build` from an HTML-entry app to a library. With a single `entry` and no `formats`, it emits `['es', 'umd']`: with `"type": "module"` in package.json and `fileName: 'quote-widget'` that is `dist/quote-widget.js` and `dist/quote-widget.umd.cjs`. Several entries default to `['es', 'cjs']`, and `umd` or `iife` require `name` (the global variable) and a single entry, or the build throws. Imported CSS becomes one file (`build.cssCodeSplit` defaults to `false`, and `cssFileName` names it), and assets are always inlined because `build.assetsInlineLimit` is ignored. Library mode does not externalize anything for you: in Vite 8 you list the framework in `build.rolldownOptions.external` and map it to a global in `output.globals` for the UMD build, and the package declares it as a peer dependency.

code

typescript · 18 lines
typescript
import { resolve } from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    lib: {
      entry: resolve(import.meta.dirname, 'src/quote-widget.ts'),
      name: 'QuoteWidget',
      fileName: 'quote-widget',
    },
    rolldownOptions: {
      external: ['vue'],
      output: {
        globals: { vue: 'Vue' },
      },
    },
  },
})

go deeper

for a junior

Remember that build.lib turns a JavaScript entry into distributable files, ES and UMD by default, and that the framework must be marked external.

for a middle

Explain the format defaults for one and several entries, why UMD needs name, how fileName shapes the file names, and where Vite 8 puts external and globals.

for a senior

Spot library-mode differences that bite consumers: inlined assets, one CSS file, unminified ES whitespace kept for tree-shaking, and process.env left unreplaced.

for a principal

Decide when library mode's opinionated preset is enough for a design system and when the team should move to a dedicated library build with Rolldown.

## What library mode changes A normal `vite build` starts from `index.html` and produces an app. **Library mode**, switched on with `build.lib`, starts from a JavaScript or TypeScript **entry** and produces files another project imports: here, a quote widget that partner sites embed. You keep using `index.html` as a demo page during development; only the build changes. ## The options | Option | Default | Notes | |---|---|---| | `entry` | the top-level `input` option (added in Vite 8.2) | required one way or the other, because a library cannot use HTML as its entry; a string, an array or an object of aliases | | `name` | none | the exposed global variable; required when `formats` includes `'umd'` or `'iife'` | | `formats` | `['es', 'umd']` for one entry, `['es', 'cjs']` for several | `'umd'` and `'iife'` do not support several entries | | `fileName` | the `name` in `package.json` | a string, or a function of `(format, entryName)` | | `cssFileName` | `fileName` if it is a string, else the package name | the single CSS file's name | ## What a single-entry build writes With `"type": "module"` in `package.json` and `fileName: 'quote-widget'`: - `dist/quote-widget.js`, the **ES module** build, for bundlers and `<script type="module">`. - `dist/quote-widget.umd.cjs`, the **UMD** build, which also works as a plain `<script>` that sets `window.QuoteWidget`. - `dist/quote-widget.css`, if the widget imports CSS. Without `"type": "module"`, Vite picks extensions for Node.js compatibility: `.js` becomes `.mjs` and `.cjs` becomes `.js`. ## How a library build differs from an app build - **Assets are always inlined** as base64 URLs; `build.assetsInlineLimit` is ignored, and `build.assetsDir` is not used. - **CSS is one file**, because `build.cssCodeSplit` defaults to `false` in library mode. - **No module preload polyfill**; `build.modulePreload` does not apply. - **No code splitting in UMD or IIFE output**, since those formats are single files. - **Whitespace in the ES output is not minified**, because that would remove the pure annotations consumers' bundlers rely on for tree-shaking. - **Entry signatures are kept**: unless you set it, Vite uses `'strict'` for Rolldown's `preserveEntrySignatures` in library builds. - **Environment values**: `import.meta.env.*` is replaced statically, but `process.env.*` is left for the consuming app, unless you replace it with `define`. ## Keeping the framework out Library mode bundles every import it can resolve, the UI framework included. If the host page also loads that framework, a widget that bundles its own copy runs a second instance beside it. To prevent that: 1. List the framework in **`build.rolldownOptions.external`** (Vite 8's name; `build.rollupOptions` is now a deprecated alias), so imports of it stay as imports. 2. For the UMD build, map it in **`build.rolldownOptions.output.globals`**, for example `{ vue: 'Vue' }`, so the script reads the host page's global. 3. Declare it as a **peer dependency**, so the consuming app supplies a single copy. ## Checking the package before publishing 1. **Read the file list** that `vite build` prints: you should see the ES file, the UMD file and one CSS file, with no hashed chunk for the framework. 2. **Search the ES output** for an `import` of the framework. If it is still an import, `external` worked; if the framework's own code is inlined, it did not. 3. **Load the UMD file in a plain HTML page** after the framework's own script, and check that the widget's global appears and finds the framework's global. 4. **Point the package's entry fields at these files**, so bundlers get the ES build and script tags get the UMD build. How to lay out those fields is a package-design question of its own. ## Errors you will meet - `Option "build.lib.name" is required when output formats include "umd" or "iife".` - `Multiple entry points are not supported when output formats include "umd" or "iife".` - `Name in package.json is required if option "build.lib.fileName" is not provided.` Library mode is deliberately a small, opinionated preset for browser-oriented libraries. For non-browser libraries or more complex flows, the Vite docs point to using Rolldown directly.

  • Partner sites want a separate calculator entry, delivered as a plain script tag like the main widget. Can you add it as a second `entry`?
    Not in the same UMD build: with `'umd'` or `'iife'` in `formats`, Vite throws `Multiple entry points are not supported`. Several entries default to `['es', 'cjs']` instead. For script-tag delivery of two entries, run a separate library build per entry, each with its own `name`.
  • Why does the minified ES build still contain whitespace and comments?
    Vite deliberately does not minify whitespace for the `'es'` format in library mode, because doing so would also strip pure annotations, the comments that tell a consuming app's bundler a call has no side effects and can be dropped. Keeping them lets the consumer tree-shake the library; the consumer's own build minifies the final output.
  • The widget reads `process.env.NODE_ENV`. What does library mode do with it?
    It leaves it alone. Library mode statically replaces `import.meta.env.*`, but not `process.env.*`, so the consuming app's build can decide the value. If you want it fixed in the library, add `define: { 'process.env.NODE_ENV': '"production"' }` to the library's config.

  • Library mode emits es, cjs and umd by default
  • build.lib externalizes the UI framework automatically
  • Library mode honours build.assetsInlineLimit like an app build
  • build.lib.name is only cosmetic and can be skipped for UMD
  • Library builds split CSS per async chunk by default
  • build.rollupOptions is still the right place for library externals in Vite 8

Assumes Vite 8: externals and output globals are set under build.rolldownOptions (build.rollupOptions is a deprecated alias), and the top-level input option that can supply the entry arrived in 8.2.