LoginSignup
9
11

More than 5 years have passed since last update.

gulp シリーズ2 〜gulp-sassでのコンパイル時の整形〜

Posted at

gulp-sassコンパイル時の整形

前回のgulpシリーズ1では、プラグインはgulp-sassのみをインストールしてみました。gulp-sassでのコンパイル後、cssファイルを覗いてみると、デフォルトでは以下のようになっていた。

コンパイル前


h1{
  color: red;
  &:hover{
    color: blue;
  }
}

コンパイル後


h1 {
  color: red; }
  h1:hover {
    color: blue; }

これでは見にくいCSSファイルになってしまう。。整形するにはどうしようかと調べてみたところ、cssbeautifyというプラグインでも整形できるのだが、sassにはオプションでoutputStyleを指定出来るらしい。outputStyleオプションを指定することでコンパイル後のcssのスタイルを指定することが出来る。sassをあまり勉強せずに、gulpをキッカケにsassを本意気でやろうと思い立った自分には目から鱗の情報だった。ありがとう同僚。

さて、outputStyleの指定は下記の通り。

gulpfile.js

var gulp = require('gulp');
var sass = require('gulp-sass');

gulp.task('sass', function(){
    gulp.src('develop/sass/**/*.scss')
        .pipe(sass({
            outputStyle: 'expanded'
        }))
        .pipe(gulp.dest('httpdocs/css/'));
});

これをコンパイルすると、こうなる。


h1 {
  color: red;
}

h1:hover {
  color: blue;
}

その他の出力スタイルはこちらを参考に。

めでたしめでたし。
今日はここまで。

9
11
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
9
11