ã¿ã¹ã¯ã©ã³ãã¼gulp.jsæéå ¥é
ç¸å¤ãããä»äºã§ã¯ãã¶ã¤ã³ããã¤ã¤JavaScriptæ¸ãã¦ããã
ã¿ã¹ã¯ã©ã³ãã¼ã¨ãã¦Grunt.jsã使ã£ã¦ããããã©ãã使ããã¡ã«æ®µã ä¸æºãã§ã¦ãããé ãã£ãããè¨è¿°ãåé·ã«ãªããã¡ã§ã¤ããã£ãã®ã§æè¿ã«ãªã£ã¦gulpã«ä¹ãæããã
gulpã¯è¯ããã¿ã¹ã¯ã¯èªåçã«ä¸¦åã«å®è¡ããããã¤ã¹ããªã¼ã ã§å¦çãããã®ã§éãããã¿ã¹ã¯ã®è¨è¿°ãã¹ããªã¼ã ãã¼ã¹ã®æ¸ãæ¹ã®ãããã§Grunt.jsã«æ¯ã¹ãã¨ã ãã¶çããªãã
ãã ããããã«ããgulpãã¡ãã£ã¨è©¦ããã ãã®æ¥æ¬èªã®è¨äºãããã¥ã¡ã³ããã¿ã¦ã¦ãå®éã®ããã¸ã§ã¯ãã§ä½¿ããã¬ãã«ã¾ã§ã®ç¥èãå¾ãããå¦ç¿ã«ä¸æ¥ããã£ãã
ãã®è¨äºã§ã¯ãgulpãã¾ã¨ãã«ä½¿ããããã«ãªãã¾ã§ã«å¿ è¦ãªç¥èãæ¸ãã
å°å ¥ã¨HelloWorld
ã¾ãã¯å°å ¥ãnpmããgulpãã¤ã³ã¹ãã¼ã«ããã
$ npm install gulp -g $ gulp -v [gulp] CLI version 3.5.6 [gulp] Local version 3.5.6
ã¤ã³ã¹ãã¼ã«ã§ãããã以ä¸ã®ãããªgulpfile.jsãç½®ããgulpfile.jsã¯ãgulpã³ãã³ããå®è¡ããã¨èªåçã«èªã¿è¾¼ã¾ãããã¡ã¤ã«ã§ãGrunt.jsã§ããGruntfile.jsã§ããã
var gulp = require('gulp'); gulp.task('hoge', function() { console.log('HelloWorld!'); });
以ä¸ã¿ããã«gulpã³ãã³ãå©ãã¨ã¿ã¹ã¯ãå®è¡ãããã
$ gulp hoge HelloWorld!
ã¿ã¹ã¯ãå®ç¾©ãã
gulpã§ã¯ãã¿ã¹ã¯ãæ±ããã¼ã¿ã¯ã¹ããªã¼ã ã«ãã£ã¦å¦çããããä¾ãã°ãlessãã¡ã¤ã«ãã³ã³ãã¤ã«ããå¾autoprefixerã«ããããããªã³ã¼ãã¯ã以ä¸ã®ããã«ãªãã
var gulp = require('gulp'); var less = require('gulp-less'); var autoprefix = require('gulp-autoprefixer'); gulp.task('css-compile', function() { return gulp.src('less/*.less') .pipe(less()) .pipe(autoprefix('last 1 version')) .pipe(gulp.dest('css/')); });
ã¿ã¹ã¯å®ç¾©ã®ä¸ã§ãgulp.src()ã§å¦çãããã¡ã¤ã«ãæå®ãã¦ãgulp.dest('css/')ã§å¦çããããã¡ã¤ã«ãæ¸ãè¾¼ã¾ããå ãæå®ãããgulp.src()ãgulp.dest()ã®è©³ç´°ã¯ãgulp API docsãåç §ããã
åºæ¬çã«gulpãã®ãã®ã¯ä½ãç¹å®ã®æ©è½ãæã£ã¦ããããã§ã¯ãªããã¦ã¼ã¶ã¯ãããããã¨ã«åããã¦gulp-*ããã±ã¼ã¸ãnpmã§ã¤ã³ã¹ãã¼ã«ãã¦ãã使ããã¨ã«ãªããããã§ã¯ãgulp-lessã¨gulp-autoprefixerã使ã£ã¦ããã
ããã©ã«ãã§å®è¡ãããã¿ã¹ã¯ãæå®ããå ´åã«ã¯Grunt.jsã¨åæ§ã«defaultã¨ããååã®ã¿ã¹ã¯ãä½ãã¨ããã
ã¿ã¹ã¯ã®å®ç¾©ã«ã¯ã次ã®ããã«åã«ä¾åããã¿ã¹ã¯ãè¤æ°æå®ãããã¨ãã§ããã
gulp.task('default', ['foo', 'bar']);
ããã§ãdefaultã¿ã¹ã¯ãå®è¡ããã¨fooã¿ã¹ã¯ã¨barã¿ã¹ã¯ãå®è¡ãããã
ã¿ã¹ã¯ã®å®è¡é ãä¿è¨¼ãã
gulpã§ã¯ãã¿ã¹ã¯ã®å®è¡ã¯èªåçã«ä¸¦ååããããããã¯ã©ããããã¨ãã¨ããã°ã以ä¸ã®ãããªã¿ã¹ã¯ãå®ç¾©ããã¨ããã
gulp.task('default', ['foo', 'bar']);
ãã®æãdefaultã¿ã¹ã¯ãå®è¡ãããã¨fooã¿ã¹ã¯ã¨barã¿ã¹ã¯ã並åã§å®è¡ãããããã®çµæã¿ã¹ã¯ã®å®è¡æéãçããã¦ãããã
ãã ãç¶æ³ã«ãã£ã¦ã¯å¿ ããã並åã§å®è¡ãã¦ã»ãããªãå ´åãããã
ä¾ãã°ãçæãããã¡ã¤ã«ãæ¶ãcleanã¿ã¹ã¯ã¨ãã¡ã¤ã«ãçæããbuildã¿ã¹ã¯ãããã¨ãã¦ããã®ãµãã¤ã®ã¿ã¹ã¯ãå®è¡ããæã«cleanã¿ã¹ã¯ãçµãã£ã¦ããbuildã¿ã¹ã¯ãéå§ãããªãã¨æå³ãç¡ãã
gulpã§ã¿ã¹ã¯ã®é åºãæå®ããå ´åã«ã¯ãã¿ã¹ã¯ãéåæåãã¦ããã¤ã¿ã¹ã¯ã®ä¾åé¢ä¿ãè¨å®ããã
ã¿ã¹ã¯ãéåæåããã«ã¯ï¼ã¤ã®æ¹æ³ãããã
- ã¹ããªã¼ã ãè¿ã
- ã³ã¼ã«ããã¯ãå¼ã³åºã
- ãããã¹ãè¿ã
1çªç°¡åãªã®ã¯ãåã«ã¹ããªã¼ã ãè¿ãããã«ããããæ¹ã§ãããã¯åã«gulp.src()ããçæããã¹ããªã¼ã ãè¿ãã°ããã§è¯ãã
gulp.task('foo', function() { return gulp.src('src/*.js') .pipe(minify()) .pipe(gulp.dest('/build')); });
2çªç®ã®ããæ¹ã¯ãã¿ã¹ã¯ãçµäºããããã®çµäºã¿ã¤ãã³ã°ãä¼ããã³ã¼ã«ããã¯ãå¼ã³åºãã¦ãããã¨ãã¿ã¹ã¯ãå®ç¾©ããä»®å¼æ°ãæå®ããã¨ããã«ã³ã¼ã«ããã¯ã渡ãããããã«ãªãã
gulp.task('foo', function(done) { setTimeout(function() { // ã¿ã¹ã¯çµäº done(); }, 1000); });
3çªç®ã¯ãã¿ã¹ã¯å ã§ãããã¹ãè¿ãããæ¹ãå人çã«ã¯ä½¿ãæ©ä¼ããªãã®ã§èª¬æã¯çç¥ãgulp API docsãåç §ããã
ã¿ã¹ã¯ãéåæåãã¦ã次ã«ã¿ã¹ã¯ã®ä¾åé¢ä¿ã追å ããã°ãã¿ã¹ã¯ã¯é çªéãå®è¡ãããããã«ãªããã¿ã¹ã¯ã®ä¾åé¢ä¿ãè¨å®ããã«ã¯ãgulp.task()ã¡ã½ããã®ï¼çªç®ã®å¼æ°ã«ä¾åããã¿ã¹ã¯åã®é åãæå®ããã
以ä¸ã®ä¾ã§ã¯ãfooã¿ã¹ã¯ãå®è¡ãããã¨ããã¨èªåçã«barã¿ã¹ã¯ãå®è¡ãããã¿ã¹ã¯ãå®äºãã¦åãã¦fooã¿ã¹ã¯ãå®è¡ãããã
gulp.task('bar', function() { return gulp.src('src/*.js') .pipe(minify()) .pipe(gulp.dest('/build')); }); // ãã®ã¿ã¹ã¯ãå®è¡ããåã«ããªããbarã¿ã¹ã¯ãå®è¡ããã gulp.task('foo', ['bar'], function() { return gulp.src('./build/*.js') .pipe(doanything()) .pipe(gulp.dest('./build')); });
注æã¨ãã¦ã¯ãåã«ã¿ã¹ã¯ãã¾ã¨ããã ãã§ã¯ä¾åé¢ä¿ãè¨å®ãããã¨ã«ã¯ãªããã並è¡ã§å®è¡ãããã以ä¸ã®ãããªã³ã¼ãã®å ´åãfoobarã¿ã¹ã¯ãå®è¡ãã¦ããfooã¿ã¹ã¯ã¨barã¿ã¹ã¯ã¯ä¸¦åã§å®è¡ãããã
gulp.task('foobar', ['foo', 'bar']);
ä¾åé¢ä¿ãè¨å®ããã«é åºãæå®ãããå ´åã«ã¯ã次ã«è¨è¿°ããrun-sequenceã使ãã
ä¾åé¢ä¿ãè¨å®ãã代ããã«run-sequenceã使ã
ã¿ã¹ã¯ã®å®è¡é åºãæå®ãããããä¾åé¢ä¿ã¯è¨å®ããããªãå ´åãããã
ä¾ãã°ããã£ãã·ã¥ãã¡ã¤ã«ãæ¶ãclear-cacheã¿ã¹ã¯ã¨ãã¡ã¤ã«ãçæããbuildã¿ã¹ã¯ããããå¿ ãããæ¯åãã£ãã·ã¥ãæ¶ãããã¯ãªãå ´åãããã¨ä»®å®ããããã®æbuildã¿ã¹ã¯ã«clear-cacheã¿ã¹ã¯ãä¾åå ã«è¨å®ããã¨ãbuildã¿ã¹ã¯ãå®è¡ããããã³ã«æ¯åãã£ãã·ã¥ãæ¶ããã¦ãã¾ãããããä¾åé¢ä¿ãè¨å®ããªããã°é åºãä¿è¨¼ã§ããªãã
ããããæã«ã¯ãrun-sequenceパッケージã使ãã¨ããã
var runSequence = require('run-sequence'); gulp.task('foobar', function() { runSequence('foo', 'bar'); });
foobarã¿ã¹ã¯ãå®è¡ããã¨ãfooã¿ã¹ã¯ã¨barã¿ã¹ã¯ãé ã«å®è¡ããããrun-sequneceããã±ã¼ã¸ã使ãã¨ä¾åé¢ä¿ãè¨å®ããªãã¦ãã¿ã¹ã¯ãé ã«å®è¡ã§ããã
ã¿ã¹ã¯å ã®ã¹ããªã¼ã ããã¼ã¸ãã
ã¿ã¹ã¯ã®å®ç¾©ã®ä¸ã«è¤æ°ã®ã¹ããªã¼ã ãããå ´åãããã
gulp.task('foo', function() { // ã©ããã£ã¦è¤æ°ã®ã¹ããªã¼ã ãè¿ãã°è¯ã? gulp.src('src/*.coffee') .pipe(coffee()) .pipe(gulp.dest('js/')); gulp.src('src/*.less') .pipe(less()) .pipe(gulp.dest('css/')); });
ã¿ã¹ã¯ãéåæåããããã«ã¯ã¹ããªã¼ã ãè¿ããªããã°ãªããªãããã¹ããªã¼ã ãè¤æ°ããå ´åã«ã¯ã©ãããã°ããã®ã ããã
event-streamパッケージã«ã¯ãã¹ããªã¼ã ãä¸ã¤ã«ã¾ã¨ããmergeã¡ã½ãããããã®ã§ããã使ãã°ããã
var merge = require('event-stream').merge; gulp.task('foo', function() { return merge( gulp.src('src/*.coffee') .pipe(coffee()) .pipe(gulp.dest('js/')), gulp.src('src/*.less') .pipe(less()) .pipe(gulp.dest('css/')) ); });
è¤æ°ã®ã¹ããªã¼ã ãããå ´åã«ã¯event-streamããã±ã¼ã¸ã®mergeã¡ã½ããã使ã£ã¦ã¹ããªã¼ã ãä¸ã¤ã«ã¾ã¨ããã¨ããã
ã¿ã¹ã¯å ã®ã¹ããªã¼ã ã®å®è¡é ãæå®ãã
event-streamã®mergeã¡ã½ããã使ã£ã¦ã¹ããªã¼ã ãä¸ã¤ã«ã¾ã¨ããå ´åã«ãããããã®ã¹ããªã¼ã ã¯ä¸¦åã§å®è¡ãããã
ã¿ã¹ã¯å®ç¾©å ã§ãè¤æ°ã®ã¹ããªã¼ã ã«é åºãä»ãã¦å®è¡ãããå ´åã«ã¯ãã¹ããªã¼ã ã®ã¤ãã³ããªã¹ãã使ãã
ã¿ã¹ã¯ãéåæåããå ´åã«ã¯ãã¿ã¹ã¯ã®å®äºã³ã¼ã«ããã¯ãä½µãã¦ä½¿ãã
// jsã®ã³ã³ãã¤ã«ãçµäºãã¦ãããlessã®ã³ã³ãã¤ã«ãè¡ããã gulp.task('foo', function(done) { gulp.src('src/*.coffee') .pipe(coffee()) .pipe(gulp.dest('js/')) .on('end', function() { gulp.src('src/*.less') .pipe(less()) .pipe(gulp.dest('css/')) .on('end', done); // ã¿ã¹ã¯å®äº }); });
watchã¿ã¹ã¯ãå®ç¾©ãã
gulpã«ã¯ããã¡ã¤ã«ã®å¤æ´ãæ¤ç¥ãã¦ã¿ã¹ã¯ã®å®è¡ãè¡ãªãgulp.watch()ãäºãç¨æããã¦ããã
gulp.task('serve', function() { // src/js/*.jsãã¡ã¤ã«ãå¤æ´ãããããbuild-jsã¿ã¹ã¯ãå®è¡ãã gulp.watch('src/js/*.js', ['build-js']); });
watch()ã¡ã½ããã®ç¬¬äºå¼æ°ã«ã¯ãã¿ã¹ã¯ã®é åã ãã§ã¯ãªãã³ã¼ã«ããã¯ãè¨è¿°ã§ããã
gulp.task('serve', function() { // src/js/*.jsãã¡ã¤ã«ãå¤æ´ãããããbuild-jsã¿ã¹ã¯ãå®è¡ãã gulp.watch('src/js/*.js', function() { gulp.src('src/*.coffee') .pipe(coffee()) .pipe(gulp.dest('js/')); }); });
ã¨ã©ã¼ãåºã¦ãã³ã±ãªãããã«plumber使ã
gulpã§ã¯ã¿ã¹ã¯ã®å®è¡ä¸ã«ä½ãã¨ã©ã¼ãèµ·ããã¨ãã®ã¾ã¾ã¿ã¹ã¯ã®å®è¡ãçµäºãããæ®éã®ã¿ã¹ã¯ã§ããã°ããã¯åé¡ãªãããwatchããã¿ã¹ã¯ã®ä¸ã§ã¨ã©ã¼ãèµ·ããã¨watchèªä½ãçµäºããã¦ãã¾ãã
ãããã£ãæã«ã¯ãã¨ã©ã¼ãããã¦ãä¸æãããªãgulp-plumberã使ããã
var plumber = require('gulp-plumber'); gulp.task('css', function() { gulp.src('src/*.less') .pipe(plumber()) // lessã®ã³ã³ãã¤ã«ã§ã³ã±ã¦ãçµäºããªã .pipe(less()) .pipe(gulp.dest('css/')) }); gulp.task('watch', function() { gulp.watch('src/js/*.less', ['css']); });
gulp-connectã使ã£ã¦livereloadãã
ãã¡ã¤ã«ãå¤æ´ãããå ´åã«livereloadãããå ´åã«ã¯ãwatchã¨gulp-connectã以ä¸ã®ããã«çµåãã¦ä½¿ãã
var gulp = require('gulp'); var connect = require('gulp-connect'); gulp.task('serve', ['connect'], function() { gulp.watch([ 'docroot/*.*' ]).on('change', function(changedFile) { // å¤æ´ãããã£ããã¡ã¤ã«ãconnect.reload()ã§ã©ã¤ããªãã¼ããã gulp.src(changedFile.path).pipe(connect.reload()); }); }); gulp.task('connect', function() { connect.server({ root: [__dirname + '/docroot/'], port: 9001, livereload: true }); });
ãã使ãgulp-*ããã±ã¼ã¸
ãã®ä»ãgulpã使ãä¸ã§é »ç¹ã«ä½¿ãããã±ã¼ã¸ãç´¹ä»ãã¦ããã
- gulp-header, gulp-footer - ã¹ããªã¼ã ã«ä½ãæååãä»ãå ãã
- gulp-concat - ã¹ããªã¼ã ã§æ±ããã¡ã¤ã«ãä¸ã¤ã®ãã¡ã¤ã«ã«é£çµãã
- gulp-rename - ã¹ããªã¼ã ã§æ±ããã¡ã¤ã«åãå¤æ´ãã
- gulp-clean - ãã¡ã¤ã«ããã£ã¬ã¯ããªæ¶ãã¦ããã
- gulp-util - Grunt.jsã§è¨ãã¨ããã®grunt-util
çµããã«
Grunt.jsã«æ¯ã¹ãã¨gulpã®æ å ±ããªã½ã¼ã¹ã¯å å®ãã¦ããªãããä¸åº¦æ £ããã¨ããããGrunt.jsã使ãæ°ãããªããªããGrunt.jsã«ä¸æºãæãå§ããæã«ã¯gulpã試ãã¦ã¿ãã¨è¯ãã¨æããã¾ãã