LoginSignup
2
1

More than 5 years have passed since last update.

cssnext の custom-media を使っているファイルを import して、別ファイルで使う方法

Last updated at Posted at 2017-08-19

ハマったので備忘録です。

エラー内容

breakpoint.css
@custom-media --iPad-viewpoint (min-width: 768px);
foo.css
@import 'breakpoint.css';

.foo {
  @media (--iPad-viewpoint) {
    display: flex;
  }
}

タイトル通り、別ファイルで media query を呼んであげたいんだけど、以下のエラーが出る。

Missing @custom-media definition for '--iPad-viewpoint'. The entire rule has been removed from the output

解決方法

ハマりポイント1

postcss-loader で呼んでいる、 config ファイルに以下を追加してあげる。 postcss-cssnext だけではダメだった…

postcss.config.js
module.exports = {
  plugins: {
    'postcss-cssnext': {},
    'postcss-import': {}, // add
    'postcss-custom-media': {}, // add
  },
};

ハマりポイント2

postcss-import@8.1.0 以上のバージョンである必要があるみたいです。
https://github.com/postcss/postcss-loader/issues/8#issuecomment-243294973

postcss-cssnext の devDependencies に postcss-import が入ってるからそのまま使っていたのが原因…。そりゃそうだよねっていう当たり前のところでつまづいていました。

なお、 postcss-custom-mediapostcss-next の dependencies に入っているみたいなので、 install 不要でいけました。

備考

動作確認済み

2
1
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
2
1