# vite package different components

<div class="WaaZC" id="bkmrk-vite%27s-%22library-mode"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwjRpZXaoLCRAxUDcGwGHQXbIVMQo_EKegQIBRAB"><span data-huuid="3562406143257101670">Vite's "library mode" is the primary method for packaging different components or a component library for distribution as an NPM package. </span><span data-huuid="3562406143257101485">This allows you to build your components into optimized bundles that can be easily consumed by other projects. </span></div></div></div><div class="WaaZC" id="bkmrk-here%27s-a-breakdown-o"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc uP58nb" data-hveid="CAIQAQ" data-ved="2ahUKEwjRpZXaoLCRAxUDcGwGHQXbIVMQo_EKegQIAhAB"><span data-huuid="3562406143257101115"><span aria-level="2" role="heading">Here's a breakdown of the key steps and concepts involved:</span> </span></div></div><div class="rPeykc uP58nb" data-hveid="CAIQAQ" data-ved="2ahUKEwjRpZXaoLCRAxUDcGwGHQXbIVMQo_EKegQIAhAB"></div></div><div class="WaaZC" id="bkmrk-project-setup%3A-initi"><div class="RJPOee EIJn2" style="animation: none !important;">- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="3562406143257100560">**Project Setup:** </span></div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257104101">Initialize a new Vite project or use an existing one. </span>
    - <span data-huuid="3562406143257103731">Ensure your project structure clearly separates your library components (e.g., in a `lib` folder) from any demo or development-only code (e.g., in a `src` folder). </span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;">**<span data-huuid="3562406143257103361">Vite Configuration (`vite.config.js`): </span>**</div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257102806">`build.lib`: </span><span data-huuid="3562406143257102621">This is the core of library mode. </span><span data-huuid="3562406143257102436">You define the entry point of your library and the output formats. </span>
        - <span data-huuid="3562406143257101881">`entry`: Path to your main library file (e.g., `lib/main.js` or `lib/index.ts`). </span>
        - <span data-huuid="3562406143257101511">`name`: The global variable name for your library when used in UMD format. </span>
        - <span data-huuid="3562406143257101141">`fileName`: The name of the output bundle file(s). </span><span data-huuid="3562406143257100956">You can use a function to customize file names based on format and entry name. </span>
    - <span data-huuid="3562406143257100586">`build.rollupOptions`: </span><span data-huuid="3562406143257100401">Customize Rollup's behavior (the underlying bundler Vite uses). </span>
        - <span data-huuid="3562406143257103942">`external`: Define dependencies that should not be bundled into your library (e.g., `react`, `vue`). </span><span data-huuid="3562406143257103757">These will be treated as external dependencies that the consuming project needs to provide. </span>
        - <span data-huuid="3562406143257103387">`output.globals`: Map external dependencies to their global variable names for UMD builds. </span>
    - 
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="3562406143257103017">**Component Implementation:** </span></div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257102462">Create your components within your designated library folder. </span>
    - <span data-huuid="3562406143257102092">Use standard ES module `export` statements to make your components available for import. </span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="3562406143257101722">**Type Definitions (Optional but Recommended):** </span></div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257101167">If you're using TypeScript, generate type definitions (`.d.ts` files) for your library. </span><span data-huuid="3562406143257100982">Plugins like `vite-plugin-dts` can automate this process. </span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="3562406143257100612">**Building the Library:** </span></div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257104153">Run the Vite build command, which will generate the bundled output in your `dist` folder according to your `vite.config.js` settings. </span>
    
    </div></div></div></div>
- <div class="zMgcWd dSKvsb" data-il=""><div data-crb-p=""><div class="xFTqob"><div class="Gur8Ad" style="display: inline;"><span data-huuid="3562406143257103783">**Publishing (for NPM Packages):** </span></div><div class="vM0jzc" style="display: inline;">
    - <span data-huuid="3562406143257103228">Ensure your `package.json` correctly defines your package name, version, main entry points (e.g., `main`, `module`, `types`), and any necessary dependencies. </span>
    - <span data-huuid="3562406143257102858">Publish your package to NPM. </span>
    
    </div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-example-vite.config."><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CEYQAQ" data-ved="2ahUKEwjRpZXaoLCRAxUDcGwGHQXbIVMQo_EKegQIRhAB" role="heading">**<span data-huuid="4061063841968288043">Example `vite.config.js` snippet for a component library: </span>**</div></div></div><div class="WaaZC" id="bkmrk-"><div class="RJPOee EIJn2" style="animation: none !important;"><div jsaction="rcuQ6b:npT2md" jscontroller="JegcYe"><div class="ecCNFc"><div class="zYSUYd"><div class="FS7GEb"><div class="dDrxod">  
<div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  build: {
    lib: {
      entry: 'lib/main.js', // Your library's entry point
      name: 'MyComponentLibrary', // Global variable name for UMD
      fileName: (format) => `my-component-library.${format}.js`,
    },
    rollupOptions: {
      external: ['react', 'react-dom'], // Exclude React from the bundle
      output: {
        globals: {
          react: 'React',
          'react-dom': 'ReactDOM',
        },
      },
    },
  },
});
```