LoginSignup
1
0

More than 3 years have passed since last update.

gulp-autoprefixerでベンダープレフィックスを自動で付ける

Posted at

gulp-autoprefixerを使えば、CSSのベンダープレフィックスを自動で付けられます。

インストール

$ npm install -D gulp gulp-stylus gulp-autoprefixer

タスクを書く

gulpfile.js
const { src, dest } = require('gulp')
const stylus = require('gulp-stylus')
const autoprefixer = require('gulp-autoprefixer')

const stylusCompile = () =>
  src('./src/**/!(_)*.styl')
  .pipe(stylus())
  .pipe(autoprefixer({
    cascade: false
  }))
  .pipe(dest('./dest/'))

exports.default = stylusCompile

npm run buildのコマンドで実行できるようにpackage.jsonにスクリプトを登録しておきます。

package.json
{
  ...
  "scripts": {
    "build": "gulp"
  }
  ...
}

実際に動かす

下記のようなStylusを書いて

style.styl
.foo
  display flex
  justify-content center
  align-items center

コンパイルすると

$ npm run build

下記のようなCSSが出力されます。

style.css
.foo {
  display: -webkit-box;
  display: flex;
  -webkit-box-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  align-items: center;
}

対応ブラウザを指定する

.browserslistrcgulpfile.jsと同じ階層に作成すれば、.browserslistrcの内容に応じたベンダープレフィックスを付けられます。

.browserslistrc
last 2 versions
not ie <= 10
> 1% in JP

この状態で、下記のようなStylusを書いて

style.styl
.foo
  display flex
  justify-content center
  align-items center

コンパイルすると

$ npm run build

下記のようなCSSが出力されます。

style.css
.foo {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}

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