Back to Tamagui

Vite Guide

code/tamagui.dev/data/docs/guides/vite.mdx

1.144.43.0 KB
Original Source

Tamagui now has two plugins for Vite: one that sets up everything you need to get going, and a second that adds CSS compilation. Both are included in the @tamagui/vite-plugin package.

Install

<Notice theme="yellow"> `@tamagui/vite-plugin` is ESM-only. Your project must have `"type": "module"` in its `package.json` (or use `.mjs`/`.mts` config files). CJS (`require()`) is not supported. </Notice> <Notice theme="green"> For a full-featured example, you can create a new app using `npm create tamagui@latest` and select the 'Simple Web' option which includes a Vite setup. </Notice>

Create a new Vite project:

bash
npm create vite@latest

Add @tamagui/vite-plugin:

bash
yarn add @tamagui/vite-plugin

Configuration

Update your vite.config.ts. If you have a tamagui.build.ts (recommended — see compiler install docs), no options are needed:

tsx
import { tamaguiPlugin } from '@tamagui/vite-plugin'

export default {
  plugins: [
    // reads from tamagui.build.ts automatically
    tamaguiPlugin(),
  ].filter(Boolean),
}

Or pass options inline:

tsx
import { tamaguiPlugin } from '@tamagui/vite-plugin'

export default {
  plugins: [
    tamaguiPlugin({
      config: 'src/tamagui.config.ts',
      components: ['tamagui'],
      disableExtraction: true,
    }),
  ].filter(Boolean),
}

Or use a minimal manual setup for Vite that just adds compatibility for react-native-web and React Native extensions:

tsx
config.define = {
  __DEV__: `${process.env.NODE_ENV === 'development' ? true : false}`,
  'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}

config.resolve.alias['react-native'] = 'react-native-web'

// set up web extensions
config.optimizeDeps.esbuildOptions = {
  ...config.optimizeDeps.esbuildOptions,
  resolveExtensions: [
    '.web.js',
    '.web.jsx',
    '.web.ts',
    '.web.tsx',
    '.mjs',
    '.js',
    '.mts',
    '.ts',
    '.jsx',
    '.tsx',
    '.json',
  ],
  loader: {
    '.js': 'jsx',
  },
}

Custom Aliases with tamaguiAliases

For advanced use cases where you need more control over alias ordering in your Vite config, you can use the tamaguiAliases helper function:

tsx
import { tamaguiAliases } from '@tamagui/vite-plugin'

export default {
  resolve: {
    alias: [
      // your custom aliases first
      { find: '@app', replacement: '/src' },
      // then tamagui aliases
      ...tamaguiAliases({
        // use @tamagui/react-native-web-lite for smaller bundle
        rnwLite: true,
        // or 'without-animated' for even smaller bundle (no Animated API)
        // rnwLite: 'without-animated',
        // alias react-native-svg to @tamagui/react-native-svg
        svg: true,
      }),
    ],
  },
}

This is useful when you need to ensure specific alias resolution order or when using a custom Vite setup without the full tamaguiPlugin.