--- title: Next.js parent: integrations --- # Next.js A plugin for integrating vanilla-extract with [Next.js](https://nextjs.org). ## Installation ```bash npm install --save-dev @vanilla-extract/next-plugin ``` ## Setup If you don't have a `next.config.js` or `next.config.ts` file in the root of your project, create one. Then add the plugin to your Next.js config file: ```ts // next.config.ts import type { NextConfig } from 'next'; import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; const withVanillaExtract = createVanillaExtractPlugin(); const nextConfig: NextConfig = {}; export default withVanillaExtract(nextConfig); ``` If required, this plugin can be composed with other plugins. ```ts // next.config.ts import type { NextConfig } from 'next'; import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; import createMDX from '@next/mdx'; const withVanillaExtract = createVanillaExtractPlugin(); const withMDX = createMDX({ extension: /\.mdx$/ }); const nextConfig: NextConfig = {}; export default withVanillaExtract(withMDX(nextConfig)); ``` ## Version Support **Only Next.js >= 16 is currently supported**. Both Webpack and Turbopack are supported, but Turbopack support is currently experimental. See [the Turbopack section](#unstable_turbopack) for more details. ## Configuration The plugin accepts the following as optional configuration, passed to `createVanillaExtractPlugin`. ### identifiers Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc) can be configured by selecting from the following options: - `short` identifiers are a 7+ character hash. e.g. `hnw5tz3` - `debug` identifiers contain human readable prefixes representing the owning filename and a potential rule level debug name. e.g. `myfile_mystyle_hnw5tz3` - A custom identifier function takes an object parameter with properties `hash`, `filePath`, `debugId`, and `packageName`, and returns a customized identifier. e.g. ```ts // next.config.ts import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; const withVanillaExtract = createVanillaExtractPlugin({ identifiers: ({ hash }) => `prefix_${hash}` }); ``` Each integration will set a default value based on the configuration options passed to the bundler. ### externals Effectively [`ExternalItem[]`](https://github.com/webpack/webpack/blob/9211be0f7a04feb45e1074e6cf848a657dd82ebc/declarations/WebpackOptions.d.ts#L207-L211) from webpack. Currently typed as `any` as this type was previously not exposed. The `any` type will be fixed in the next major version. ### unstable_turbopack > ⚠️  Turbopack support is experimental. Its API is unstable and may undergo breaking changes in non-major versions. Additionally, it may not handle all features supported by Next.js. You can control Turbopack autoconfiguration using `unstable_turbopack.mode`: - `off`: (default) never configure Turbopack (i.e. use Webpack) - `auto`: enable Turbopack config (equivalent to `on` in v3) - `on`: force-enable Turbopack config For example: ```ts // next.config.ts import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; const withVanillaExtract = createVanillaExtractPlugin({ unstable_turbopack: { mode: 'auto' } }); export default withVanillaExtract({}); ``` If you already manage `turbopack.rules` yourself for the same file globs, the plugin may throw to avoid rule conflicts. In that case, set `mode: 'off'` and apply your Turbopack config manually. By default Turbopack integration processes `**/*.css.{js,cjs,mjs,jsx,ts,tsx}` files. You can override this with `unstable_turbopack.glob`: ```ts // next.config.ts import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; const withVanillaExtract = createVanillaExtractPlugin({ unstable_turbopack: { mode: 'auto', glob: ['**/*.css.ts', '**/*.css.tsx'] } }); export default withVanillaExtract({}); ``` ## Transpiling Vanilla Extract-dependent Libraries By default, Next.js does not allow importing of TypeScript files outside of the app root. If your application depends on a TypeScript library, whether that be a local package within your app's monorepo, or a dependency inside `node_modules`, and that library styles its components with Vanilla Extract, but does _not_ compile its styles, then that library needs to be added to [`transpilePackages`] in your Next.js config: ```tsx // App.tsx import { Button } from '@company/design-system'; export default function App() { // This is unstyled and/or throws errors about Vanilla Extract being used in runtime return ; } ``` ```ts // next.config.ts import type { NextConfig } from 'next'; import { createVanillaExtractPlugin } from '@vanilla-extract/next-plugin'; const withVanillaExtract = createVanillaExtractPlugin(); const nextConfig: NextConfig = { transpilePackages: ['@company/design-system'] }; // Next.js Vanilla Extract integration will now compile @company/design-system styles export default withVanillaExtract(nextConfig); ``` [`transpilePackages`]: https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages