👾

NextJS v15 + vanilla-extract で公式に従ってもエラーになった

に公開1

環境

  • next: 15.2.4
  • react: ^19.0.0
  • typescript: ^5
  • @vanilla-extract/css: ^1.17.1

公式通り vanilla-extract を導入したがエラーが発生した

下記 公式のドキュメントには書かれていないようだったのでメモとして、
https://vanilla-extract.style/documentation/integrations/next/

vanilla-extract を Next.js に導入する

vanilla-extract を導入し npm run dev で Next を起動すると下記のようなエラーが発生した

vanilla-extract の .css.ts が意図した通り認識さていない...

結論: Next.js の Turbopack に Vanilla-extract が対応していない

Thanks for raising an issue. As documented in the turbopack documentation, using webpack-based Next.js plugins as-is from next.config.js is not yet supported. Until we can actually write plugins for turbopack (I don't think this is possible just yet), the VE next plugin will not work with next --turbo.
https://github.com/vanilla-extract-css/vanilla-extract/issues/1367#issuecomment-2016352850

https://github.com/kuma-ui/kuma-ui/issues/408
https://github.com/vanilla-extract-css/vanilla-extract/issues/1367

現状 turbopack を使わない選択肢しかなさそう

package.json
  "scripts": {
-   "dev": "next dev --turbopack",
+   "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
  }

--turbopack を取り npm run dev し直せば問題なく vanilla-extract が使えるようになった

おわり

Discussion

KiKiKi KiKiKiKiKi KiKi

Next.js v16 はデフォルトで Tubopack が使用されるので明示的に --webpack を指定する必要がある

  • next.js 16.1.1
  • @vanilla-extract/css ^1.18
  • @vanilla-extract/next-plugin ^2.4.17

上記の環境で同様のエラーが発生した

⨯ ERROR: This build is using Turbopack, with a `webpack` config and no `turbopack` config.
   This may be a mistake.

   As of Next.js 16 Turbopack is enabled by default and
   custom webpack configurations may need to be migrated to Turbopack.

   NOTE: your `webpack` config may have been added by a configuration plugin.

   To configure Turbopack, see https://nextjs.org/docs/app/api-reference/next-config-js/turbopack

   TIP: Many applications work fine under Turbopack with no configuration,
   if that is the case for you, you can silence this error by passing the
   `--turbopack` or `--webpack` flag explicitly or simply setting an 
   empty turbopack config in your Next config file (e.g. `turbopack: {}`).

原因

Next.js ではデフォルトで turbopack が使用されるようになっており、vanilla-extract がまだ Turbopack に対応できてないためにエラーが発生している

https://github.com/vanilla-extract-css/vanilla-extract/issues/1367
https://github.com/vanilla-extract-css/vanilla-extract/pull/1639

対応

明示的に --webpack オプションで実行させる

package.json
{
  "scripts": {
    "dev": "next dev",
+   "dev:webpack": "next dev --webpack",
   }
}
npm run dev:webpack

vanilla-extract が対応するまでは Turbopack の恩恵は受けられなそう