# Vite

# import { defineConfig } from 'vite'

<div class="WaaZC" id="bkmrk-the-line-import-%7B-de"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIBRAB"><span data-huuid="7591640550378436054">The line `import { defineConfig } from 'vite'` is a standard import statement used in JavaScript and TypeScript projects that utilize Vite as their build tool. </span></div><div class="rPeykc" data-hveid="CAUQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIBRAB"></div></div></div><div class="WaaZC" id="bkmrk-purpose%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CAMQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIAxAB" role="heading">**<span data-huuid="7591640550378438448">Purpose: </span>**</div></div></div><div class="WaaZC" id="bkmrk-importing-defineconf"><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="7591640550378437943">Importing `defineConfig`: </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="7591640550378435044">This statement imports the `defineConfig` helper function from the `vite` package. </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="7591640550378437438">**Providing Intellisense and Type Checking:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="7591640550378438635">The primary purpose of `defineConfig` is to provide type hints and better Intellisense support within your IDE when configuring Vite. </span><span data-huuid="7591640550378435736">By wrapping your Vite configuration object with `defineConfig`, you benefit from autocompletion and type checking, which helps prevent errors and ensures your configuration adheres to Vite's expected structure. </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="7591640550378438130">**Defining Vite Configuration:** </span></div><div class="vM0jzc" style="display: inline;"><span data-huuid="7591640550378435231">This function is typically used in the `vite.config.js` (or `vite.config.ts`) file, which is the central place to define your project's build and development settings for Vite. </span></div></div></div></div>

</div></div><div class="WaaZC" id="bkmrk-example-usage%3A"><div class="RJPOee EIJn2" style="animation: none !important;"><div aria-level="3" class="rPeykc pyPiTc" data-hveid="CBcQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIFxAB" role="heading">**<span data-huuid="13781723138190841278">Example Usage: </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 class="x7ndcb">  
</div><div aria-live="polite" class="WDoJJe">  
</div></div></div></div></div></div></div></div>```
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    // your Vite plugins here, e.g., @vitejs/plugin-react
  ],
  server: {
    port: 3000,
  },
  build: {
    outDir: 'dist',
  },
});
```

<div class="WaaZC" id="bkmrk-in-this-example%2C-the"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB"><span data-huuid="17957175148721145227">In this example, the configuration object passed to `defineConfig` specifies various Vite settings, including plugins, server options, and build options. </span><span data-huuid="17957175148721144338">The `defineConfig` wrapper helps ensure that these settings are correctly typed and recognized by your app.</span></div></div><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB"></div></div><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB" id="bkmrk--1"></div><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB" id="bkmrk-key-parameters-in-de"><span data-huuid="17957175148721144338">**Key Parameters in** `<strong>defineConfig</strong>`</span><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" jscontroller="a7qCn" jsuid="V9GUte_r" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`plugins`**: Array of Vite plugins (e.g., React, Vue, TS Paths) to add functionality.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`server`**: Options for the development server (e.g., `port`, `host`, `open`, `proxy`).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`build`**: Settings for the production build (e.g., `outDir`, `minify`, `target`).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`resolve`**: Path aliases (e.g., `@/src`) and module resolution settings.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`define`**: Global constants (e.g., `__APP_VERSION__`) that get replaced during build.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`envDir` / `envPrefix`**: Control where `.env` files are loaded and which variables are exposed.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**`base`**: Public path for assets (e.g., `/my-app/`).</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="V9GUte_21,V9GUte_22,V9GUte_23"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" jsaction="rcuQ6b:&V9GUte_2c|npT2md" jscontroller="KHhJQ" jsuid="V9GUte_2c">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" jscontroller="a7qCn" jsuid="V9GUte_2d" role="heading">**Advanced Usage**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" jscontroller="a7qCn" jsuid="V9GUte_2d" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Conditional Config**: Export a function from `defineConfig` to set options based on `command` (dev/build) or `mode` (development/production).</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Rollup Options**: Use `build.rollupOptions` to deeply customize Rollup (e.g., `output.entryFileNames`, `external` modules)</span>

</div><div class="WaaZC" id="bkmrk-development-environm"><div class="RJPOee EIJn2" style="animation: none !important;"><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB"></div><div class="rPeykc" data-hveid="CBsQAQ" data-ved="2ahUKEwizxJnHoLCRAxViUGcHHQnLFoQQo_EKegQIGxAB"><span data-huuid="17957175148721144338">development environment.</span></div></div></div>

# @vitejs/plugin-react

`@vitejs/plugin-react` is the official Vite plugin for React and React Server Components, providing fast development with HMR (Hot Module Replacement), asset handling (images, CSS), JSX/TSX support, and optimizing builds with options like using [SWC](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=SWC&mstk=AUtExfB2qGCwQD7X2OGQzjRfSPtf1MVkgqlyQERK2bIlAQrdNs1EG_R8iksQMEPuvYQIzB7NFQzAUxEp6qHSEH_ChHR3qCftRqo-hfOUrlcdwQZwJiM5j14s-UCDfN1kzylLeF-hN9kbeLwBRq_7IdCeQeH_iEDOd4Kq2xn3nu6Pg1v5S2F6bkOgwPezGZ7kESq9cHJDI1N6TuvC7c3YNQRQhvLUicDPkAsZ3qKIv6hjH46Dcvucf7Tf8MyzF0gMlSfdrh23w0tbSjczNYxKe9ZFBtqg&csui=3&ved=2ahUKEwje5ar3n7CRAxWTRWcHHX8mCeYQgK4QegQIARAD) for speed, enabling features like React Refresh and TypeScript decorators, making React development in Vite super efficient.<span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ph8Ky_g,ph8Ky_h,ph8Ky_i"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk-" jsaction="rcuQ6b:&ph8Ky_8|npT2md" jscontroller="zcfIf" jsuid="ph8Ky_8">  
</div><div class="Y3BBE" data-hveid="CAEQAA" data-processed="true" data-sfc-cp="" id="bkmrk--1" jsaction="rcuQ6b:&ph8Ky_8|npT2md" jscontroller="zcfIf" jsuid="ph8Ky_8"></div><div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--2" jsaction="rcuQ6b:&ph8Ky_o|npT2md" jscontroller="KHhJQ" jsuid="ph8Ky_o">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-key-features-%26-benef" jscontroller="a7qCn" jsuid="ph8Ky_p" role="heading">**Key Features &amp; Benefits:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--3" jscontroller="a7qCn" jsuid="ph8Ky_p" role="heading">  
</div>- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Fast Dev Server:** Blazing fast starts and instant HMR with React components and MDX.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**[JSX/TSX](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=JSX%2FTSX&mstk=AUtExfB2qGCwQD7X2OGQzjRfSPtf1MVkgqlyQERK2bIlAQrdNs1EG_R8iksQMEPuvYQIzB7NFQzAUxEp6qHSEH_ChHR3qCftRqo-hfOUrlcdwQZwJiM5j14s-UCDfN1kzylLeF-hN9kbeLwBRq_7IdCeQeH_iEDOd4Kq2xn3nu6Pg1v5S2F6bkOgwPezGZ7kESq9cHJDI1N6TuvC7c3YNQRQhvLUicDPkAsZ3qKIv6hjH46Dcvucf7Tf8MyzF0gMlSfdrh23w0tbSjczNYxKe9ZFBtqg&csui=3&ved=2ahUKEwje5ar3n7CRAxWTRWcHHX8mCeYQgK4QegQIAxAC) Support:** Handles React syntax (JSX/TSX) for seamless development.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Asset Handling:** Imports images, CSS, etc., directly into components.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**React Refresh (HMR):** See changes instantly without full page reloads.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**SWC [ Option](https://www.google.com/search?client=ubuntu-sn&channel=fs&q=Option&mstk=AUtExfB2qGCwQD7X2OGQzjRfSPtf1MVkgqlyQERK2bIlAQrdNs1EG_R8iksQMEPuvYQIzB7NFQzAUxEp6qHSEH_ChHR3qCftRqo-hfOUrlcdwQZwJiM5j14s-UCDfN1kzylLeF-hN9kbeLwBRq_7IdCeQeH_iEDOd4Kq2xn3nu6Pg1v5S2F6bkOgwPezGZ7kESq9cHJDI1N6TuvC7c3YNQRQhvLUicDPkAsZ3qKIv6hjH46Dcvucf7Tf8MyzF0gMlSfdrh23w0tbSjczNYxKe9ZFBtqg&csui=3&ved=2ahUKEwje5ar3n7CRAxWTRWcHHX8mCeYQgK4QegQIAxAG)**: Use the faster SWC compiler via `@vitejs/plugin-react-swc` for quicker builds.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**React Server Components (RSC):** Supports building apps with RSCs via `@vitejs/plugin-rsc`.</span>
- <span class="T286Pc" data-processed="true" data-sfc-cp="">**Build Optimization:** Code splitting and efficient bundling for production.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ph8Ky_1j,ph8Ky_1k,ph8Ky_1l"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Fsg96" data-processed="true" data-sfc-cp="" id="bkmrk--4" jsaction="rcuQ6b:&ph8Ky_1r|npT2md" jscontroller="KHhJQ" jsuid="ph8Ky_1r">  
</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk-how-it-works-with-vi" jscontroller="a7qCn" jsuid="ph8Ky_1s" role="heading">**How it works with Vite:**</div><div aria-level="3" class="otQkpb" data-animation-nesting="" data-processed="true" data-sfc-cp="" id="bkmrk--5" jscontroller="a7qCn" jsuid="ph8Ky_1s" role="heading">  
</div>1. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Installation:** You install it (often automatically when you scaffold a React project with Vite).</span>
2. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Configuration:** It's configured in `vite.config.js` (or `.ts`).</span>
3. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Development:** Vite's dev server uses the plugin to process React code quickly.</span>
4. <span class="T286Pc" data-processed="true" data-sfc-cp="">**Build:** The plugin prepares your React app for production.</span><span class="uJ19be notranslate" data-processed="true" data-wiz-uids="ph8Ky_2b,ph8Ky_2c,ph8Ky_2d"><span class="vKEkVd" data-animation-atomic="" data-processed="true"> </span></span>

<div class="Y3BBE" data-hveid="CAYQAA" data-processed="true" data-sfc-cp="" id="bkmrk-in-short%2C-it%27s-the-e" jsaction="rcuQ6b:&ph8Ky_2h|npT2md" jscontroller="zcfIf" jsuid="ph8Ky_2h">In short, it's the essential bridge that brings the power and speed of Vite to your React projects.</div>

# 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',
        },
      },
    },
  },
});
```