gulp学习
2016-03-14 本文已影响65人
北漂老莫
Gulp API主要包括4个顶级函数。如下
-
gulp.task(name, deps, fn)
定义任务 -
gulp.src(globs, [opt])
指向我们想要操作的源文件 -
gulp.dest
指向输出文件 -
gulp.watch
用于监视文件的变化
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
less = require('gulp-less');
// use 'default' so `gulp` to run task
gulp.task('default', ['watch']);
// Error: Cannot find module 'jshint/src/cli'
// at Function.Module._resolveFilename (module.js:336:15)
// at Function.Module._load (module.js:278:25)
// at Module.require (module.js:365:17)
// need to install jshint as well
// use `gulp jshint` to run task
gulp.task('jshint', function() {
return gulp.src('source/javascript/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(gulp.dest('build'));
})
gulp.task('less', function() {
return gulp.src('source/less/*.less')
.pipe(less())
.pipe(gulp.dest('source/css'))
})
// need to write task before watch task
gulp.task('watch', function() {
gulp.watch('source/javascript/**/*.js', ['jshint']);
gulp.watch('source/less/**/*.less', ['less']);
})