Open4

vanilla-extract で width-range media query を使用する

ピン留めされたアイテム
naporitannaporitan

vite の時は postcss の minmax plugin をいれよう
Next.js の時はデフォルトで入ってるので必要ない

naporitannaporitan

これを

@media (max-width: 30em) { ... }

こう書ける

@media (width <= 30em) { ... }

https://developer.mozilla.org/ja/docs/Web/CSS/CSS_media_queries/Using_media_queries#level_4_での構文の拡張

vanilla-extract だとこう

export const test = style({
  "@media": {
    "screen and (width <= 30em)": {
      // 
    },
  },
});

Next.js 使用時だと polyfill が動いていたので、vanilla-extract 側に postcss が入っているものだと思っていたが vite を使っていると polyfill が働いていないのでどこで postcss が刺さっているのか確認する

naporitannaporitan

Next.js

config

const { createVanillaExtractPlugin } = require("@vanilla-extract/next-plugin");

const withVanillaExtract = createVanillaExtractPlugin();

/** @type {import('next').NextConfig} */
const nextConfig = {
 reactStrictMode: true,
 swcMinify: true,
};

module.exports = withVanillaExtract(nextConfig);

Vite

config

import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";
import minmax from 'postcss-media-minmax'
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [vanillaExtractPlugin()],
  css: {
    postcss: {
      plugins: [minmax()]
    }
  }
});
naporitannaporitan

next-plugin では lazyPostCSS でデフォルトの設定を読み込んで webpack の loaders として私ているっぽい。

https://github.com/vanilla-extract-css/vanilla-extract/blob/master/packages/next-plugin/src/index.ts#L55-L60
https://github.com/vanilla-extract-css/vanilla-extract/blob/master/packages/next-plugin/src/index.ts#L85-L92

vite-plugin では config を createCompilerに渡してる。js と css が分離されてから postcss がかかるのかは調べ切れてない
https://github.com/vanilla-extract-css/vanilla-extract/blob/master/packages/vite-plugin/src/index.ts#L135-L150