Compiled

@compiled/vite-plugin

Vite 4Vite 5

Vite plugin used to transform your code. See installation for setup instructions.

npm install @compiled/vite-plugin --save-dev

Options

Configure in your Vite config.

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import compiled from '@compiled/vite-plugin';

export default defineConfig({
  plugins: [
    compiled({
      importReact: true,
    }),
    react(),
  ],
});

Important: The Compiled plugin must be placed before the React plugin in the plugins array.

Options used by @compiled/vite-plugin

@compiled/vite-plugin accepts the following options:

bake

Turns on @compiled/babel-plugin.

extensions

Extensions that we should consider to be code. We use these to identify if a file should be parsed.

If set, the value of extensions will be used in two places:

  1. If resolver is not set, then extensions will be passed to the default resolver, enhanced-resolver.
  2. This is also passed to @compiled/babel-plugin under the hood.

extract

Enables extracting all styles to an atomic style sheet. This enables @compiled/babel-plugin-strip-runtime under the hood.

When enabled in production builds, all styles will be extracted to a compiled.css file and automatically injected into your HTML. The CSS link will be added to the <head> of your HTML.

Extraction is automatically disabled in development mode to ensure optimal Hot Module Replacement (HMR) performance.

extractStylesToDirectory

When set, extracts styles to an external CSS file. Read babel-plugin-strip-runtime for more information.

parserBabelPlugins

Babel parser plugins to be used when parsing the source code. Define these to enable extra babel parsers (for example, typescript). See the babel docs for more context.

Example usage:

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import compiled from '@compiled/vite-plugin';

export default defineConfig({
  plugins: [
    compiled({
      parserBabelPlugins: ['typescript'],
    }),
    react(),
  ],
});

This is also passed to @compiled/babel-plugin under the hood.

sortAtRules

Whether to sort at-rules, including media queries, when extraction is enabled.

See here for more information.

sortShorthand

Whether to sort shorthand properties so that they always come before longhand properties when extraction is enabled.

For example, border will always come before borderColor, which will always come before borderTopColor.

See Shorthand properties for more information.

ssr

To be used in conjunction with extract: true when having a different configuration for SSR.

When set to true, ssr will prevent additional require (one import per rule) in the bundle during style sheet extraction.

This is equivalent to setting compiledRequireExclude in @compiled/babel-plugin-strip-runtime.

transformerBabelPlugins

Babel transformer plugins to be applied to transformed files before the Compiled evaluation.

Use this if you have some sort of babel plugin that should run before Compiled, likely because you have some sort of non-standard syntax in your code. See the babel docs for more context.

Example usage:

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import compiled from '@compiled/vite-plugin';

export default defineConfig({
  plugins: [
    compiled({
      transformerBabelPlugins: [
        [
          '@babel/plugin-proposal-decorators',
          {
            legacy: true,
          },
        ],
      ],
    }),
    react(),
  ],
});

Options passed to @compiled/babel-plugin

@compiled/vite-plugin also accepts the following options. These are not used in the plugin itself, but instead they are passed directly to the underlying @compiled/babel-plugin.

To avoid repetition, we have not included the descriptions here. Please refer to the @compiled/babel-plugin documentation for more information.

CSS Extraction

When extract is set to true, the plugin will generate a compiled.css file containing all extracted styles. This file will be automatically linked in your HTML during production builds.

// vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import compiled from '@compiled/vite-plugin';

export default defineConfig({
  plugins: [
    compiled({
      extract: true,
    }),
    react(),
  ],
});

The generated CSS file will include:

Styles are automatically sorted and deduplicated for optimal performance. In development mode, extraction is disabled to maintain fast HMR updates.

Suggest changes to this page ➚