gulpにsass-lintを導入する

stylelintとは?
-lint工程を追加することで、コードを綺麗にし、保守性を高める
-styleの重複を減らす

csslintとは?
$ npm install -g csslint
$ csslint main.css

sasslintを導入
$ npm install gulp-sass-lint –save-dev
$ gulp

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

gulp.task('sass', function(done){
	gulp.src('./src/sass/*.scss')
		.pipe(plumber())
		.pipe(sassLint())
		.pipe(sassLint.format())
		.pipe(sassLint.failOnError())
		.pipe(sass({outputStyle: 'expand'}))
		.pipe(gulp.dest('./dest/css'))
	done();
});

src/sass/main.scss
2:2 warning Mixed tabs and spaces indentation
2:20 warning Color ‘red’ should be written in its hexadecimal form #ff0000 no-color-keywords
2:20 warning Color literals such as ‘red’ should only be used in variable declarations no-color-literals
3:2 warning Mixed tabs and spaces indentation
4:2 warning Mixed tabs and spaces indentation
5:1 warning Files must end with a new line final-newline

✖ 6 problems (0 errors, 6 warnings)

ああああああ、これスゲー

gulp + sassの環境を整えよう

## sassをインストール
$ npm i -D gulp-sass

imgを圧縮し、sassとhtmlを編集したら、自動反映にします。

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var webserver = require('gulp-webserver');
var sass = require('gulp-sass');
var plumber = require('gulp-plumber');

gulp.task('html', function(done){
	gulp.src('./src/*.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('img', function(done){
	gulp.src('./src/img/*.png')
		.pipe(imagemin())
		.pipe(gulp.dest('./dest/img'))
	done();
});

gulp.task('sass', function(done){
	gulp.src('./src/sass/*.scss')
		.pipe(plumber())
		.pipe(sass({outputStyle: 'expand'}))
		.pipe(gulp.dest('./dest/css'))
	done();
});

gulp.task('watch', function(done){
	gulp.watch('./src/*.html', gulp.task('html'))
	gulp.watch('./src/sass/*.scss', gulp.task('sass'))
	done();
});

gulp.task('webserver', function(done){
	gulp.src('./dest')
		.pipe(webserver({
			host:'192.168.34.10',
			port: 8000,
			livereload: true,
		}));
	done();
});

gulp.task('default', gulp.series('html','img','sass','watch','webserver'));

あ、これでいいじゃん。あとは、JSとCSSのLint周りか

gulp 4

gulp-webserver
$ npm i -D gulp-webserver

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var plumber = require('gulp-plumber');
var header = require('gulp-header');
var webserver = require('gulp-webserver');

gulp.task('html', function(done){
	gulp.src('./src/index.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('img', function(done){
	gulp.src('./src/img/*.png')
		.pipe(imagemin())
		.pipe(gulp.dest('./dest/img'))
	done();
});

gulp.task('js', function(done){
	gulp.src('./src/coffee/*.coffee')
		.pipe(plumber())
		.pipe(coffee())
		.pipe(concat('all.min.js'))
		.pipe(uglify())
		.pipe(header('/* copry righ @hpscript */'))
		.pipe(gulp.dest('./dest/js'))
	done();
});

gulp.task('watch', function(done){
	gulp.watch('./src/coffee/*.coffee', gulp.task('js'))
	gulp.watch('./src/*.html', gulp.task('html'))
	done();
});

gulp.task('webserver', function(done){
	gulp.src('./dist')
		.pipe(webserver({
			host:'192.168.34.10',
			livereload: true
		}));
	done();
});

gulp.task('default', gulp.series('html','img','js','watch','webserver'));

きた。livereloadが動かないと思ったら、htmlタグでないとダメのようね。
OK。では、sass + gulpの環境を考えましょう

gulp 4

watchの使い方

gulp.task('watch', function(done){
	gulp.watch('./src/coffee/*.coffee', gulp.task('js'))
	done();
});

gulp.task('default', gulp.series('html','img','js','watch'));

素晴らしい!
しかしsassの–watchとどうやって一緒に使うんだろうか、これ。

gulp-plumber

gulp.task('js', function(done){
	gulp.src('./src/coffee/*.coffee')
		.pip(pulumber())
		.pipe(coffee())
		.pipe(concat('all.min.js'))
		.pipe(uglify())
		.pipe(gulp.dest('./dest/js'))
	done();
});
gulp.task('js', function(done){
	gulp.src('./src/coffee/*.coffee')
		.pipe(plumber())
		.pipe(coffee())
		.pipe(concat('all.min.js'))
		.pipe(uglify())
		.pipe(header('/* copry righ @hpscript */'))
		.pipe(gulp.dest('./dest/js'))
	done();
});
/* copry righ @hpscript */(function(){}).call(this),function(){}.call(this);

アジャイル・チーム開発では属人性をなくす為、出来るだけcopy rightは付けたくないんだよなー

gulp 3

gulp-imageminで圧縮する

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

gulp.task('html', function(done){
	gulp.src('./src/index.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('img', function(done){
	gulp.src('./src/img/*.png')
		.pipe(imagemin())
		.pipe(gulp.dest('./dest/img'))
	done();
});

gulp.task('default', gulp.series('html','img'));

e.g. 150.2kb → 80.1kb

$ npm i -D gulp-coffee gulp-concat gulp-uglify

var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');

gulp.task('html', function(done){
	gulp.src('./src/index.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('img', function(done){
	gulp.src('./src/img/*.png')
		.pipe(imagemin())
		.pipe(gulp.dest('./dest/img'))
	done();
});

gulp.task('js', function(done){
	gulp.src('./src/coffee/*.coffee')
		.pipe(coffee())
		.pipe(concat('all.min.js'))
		.pipe(uglify())
		.pipe(gulp.dest('./dest/js'))
	done();
});

gulp.task('default', gulp.series('html','img','js'));

[vagrant@localhost gulp]$ gulp
[10:49:46] Using gulpfile ~/gulp/gulpfile.js
[10:49:46] Starting ‘default’…
[10:49:46] Starting ‘html’…
[10:49:46] Finished ‘html’ after 8.49 ms
[10:49:46] Starting ‘img’…
[10:49:46] Finished ‘img’ after 2.97 ms
[10:49:46] Starting ‘js’…
[10:49:46] Finished ‘js’ after 1.92 ms
[10:49:46] Finished ‘default’ after 17 ms
[10:49:48] gulp-imagemin: Minified 1 image (saved 70.1 kB – 46.7%)

マジかよ、これ。

gulp 2

コピーして転送

var gulp = require('gulp');

gulp.task('html', function(done){
	gulp.src('./src/index.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('default', gulp.task('html'));

複数のタスクを実行する際には、gulp.seriesと書く

gulp.task('default', gulp.series('html','msg'));
var gulp = require('gulp');

gulp.task('html', function(done){
	gulp.src('./src/index.html')
		.pipe(gulp.dest('./dest'))
	done();
});

gulp.task('img', function(done){
	gulp.src('./src/img/*.png')
		.pipe(imagemin())
		.pipe(gulp.dest('./dest/img'))
	done();
});

gulp.task('default', gulp.series('html','img'));

変換して圧縮はwebpackのJS minifyと一緒か。

gulp 1

gulpとは?
-> Node.jsをベースとしたビルトシステムヘルパー、タスクランナー
-> ブラウザの自動更新もタスクとして処理できる
https://gulpjs.com/

ん?webpackと同じ?ビルトインがある分、webpackの方が高機能か。

gulp install
$ sudo npm i -g gulp
$ gulp -v
CLI version: 2.2.0
$ npm init

gulpfile.js

var gulp = require('gulp');

gulp.task('hello', function(done){
	console.log("hello world");
	done();
});

gulpの3系と4系だと引数の渡し方が異なるようです。

var gulp = require('gulp');

gulp.task('hello', function(done){
	console.log("hello world");
	done();
});

gulp.task('default', gulp.task('hello'));

Sass 4

$totalWidth: 940px;
$columnCount: 5;
@function getColumnWidth($width, $count){
	$padding: 10px;
	$columnWidth: floor($width - ($padding * ($count - 1) / $count));
	@debug $columnWidth;
	@return $columnWidth;
}

.grid {
	float: left;
	width: getColumnWidth($totalWidth, $columnCount);
}
@mixin round {
	border-radius: 4px;
}

.label {
	font-size: 12px;
	@include round;
}
.msg {
	font-size: 12px;
	font-weight: bold;
	padding: 2px 4px;
	color: white;
}

.errorMsg {
	@extend .msg;
	background: red;
}

.warningMsg {
	@extend .msg;
	background: orange;
}
.msg, .errorMsg, .warningMsg {
  font-size: 12px;
  font-weight: bold;
  padding: 2px 4px;
  color: white;
}

.errorMsg {
  background: red;
}

.warningMsg {
  background: orange;
}

OK! 比較的いいペース

Sass 3

lighten, darkenなどが使える

$brandColor: red;

#main {
	width: 90%;
	p {
		color: lighten($brandColor, 30%);
		font-size: 16px;
	}
}
#main {
  width: 90%;
}
#main p {
  color: #ff9999;
  font-size: 16px;
}

sass module
https://sass-lang.com/documentation/modules

if文

$debugMode: true;

#main {
	@if $debugMode == true {
		color: red;
	} else {
		color: green;
	}
}

for

@for $i from 10 through 14 {
.fs#{$i} {font-size: #{$i}px;}
}
$i: 10;
@while $i <= 14 { .fs#{$i} {font-size: #{$i}px;} $i: $i +1; } [/css] [css] $animals: cat, dog, tiger; @each $animal in $animals { .#{$animal}-icon { background: url("#{$animal}.png"); } } [/css]

Sass 2

sassの自動変換
変更があると、上書きされる
[vagrant@localhost sass]$ sass –style expanded –watch scss:css
>>> Sass is watching for changes. Press Ctrl-C to stop.
write css/main.css
write css/main.css.map
>>> Change detected to: scss/main.scss
write css/main.css
write css/main.css.map

あれ、gulpって必要なかったっけ?

/* comment */

#main {
	width: 90%;
	p {
		font-size: 16px;
		a {
			text-decoration: none;
		}
	}
}

&の使い方

a {
			text-decoration: none;
			&:hover {
				font-weight: bold;
			}
		}

変数

$baseFontSize: 14px;

#main {
	width: 90%;
	p {
		font-size: $baseFontSize;
	}
}
$imgDir: "../img/";

#main {
	width: 90%;
	background: url($imgDir + "bg.png");
	p {
		font-size: 16px;
	}
}