LoginSignup
7
3

More than 5 years have passed since last update.

postcss-preset-envでpostcss-nestingを使う

Posted at

postcss-preset-envを使っていて、CSSをネストさせたいなと思いました。

しかし、普通にオプション付け加えずに書いても、もちろん反映されない。

ググっても、あまりpostcss-preset-envとの組み合わせの情報はなく、postcss-nesting単体のものが多かったため、書き残しておく。

'nesting-rules': true を features に追記すればよいだけ

postcss.config.jsfeatures欄に'nesting-rules': trueを追記すればよいだけでした。

postcss.config.js
  postcssPresetEnv({
    features: {
      'nesting-rules': true,
    },
  }),

他のpostcss-custom-propertiesとかは'custom-properties'のようにしてfeatureを設定するので、同じ流れで'nesting'を使おうとしたら、上手くいかず少し困った経緯があります。

postcss-preset-envのissueにいたこの人のコードで、設定方法を知りました。一応、公式にも記載がありました。ちゃんちゃん

postcss-nestedよりpostcss-nesting

蛇足ですが、2つの違いについて。

Sassっぽいほうはpostcss-nestedで、なんならnestingのほうよりいい感じの書き方な気がするのですが、公式のCSSの草案?に準拠しているほうはpostcss-nestingなので、そっちを採用しました。

やっぱり、Sassではなく未来のCSSに準拠したいという思いからです。

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3