$ npm install gulp -g
gulp安装到全域环境下,可以存取gulp的CLI
$ npm install gulp --save-dev
将gulp安装到本地端的项目内,并在package.json内的devDependencies注册。
使用npm install 安装下列模块
- 编译Sass (gulp-ruby-sass)
- Autoprefixer (gulp-autoprefixer)
- 缩小化(minify)CSS (gulp-minify-css)
- JSHint (gulp-jshint)
- 拼接 (gulp-concat)
- 丑化(Uglify) (gulp-uglify)
- 图片压缩 (gulp-imagemin)
- 即时重整(LiveReload) (gulp-livereload)
- 清理档案 (gulp-clean)
- 图片快取,只有更改过得图片会进行压缩 (gulp-cache)
- 更动通知 (gulp-notify)
使用npm install 安装下列模块
var gulp = require('gulp'), sass = require('gulp-ruby-sass'), autoprefixer = require('gulp-autoprefixer'), minifycss = require('gulp-minify-css'), jshint = require('gulp-jshint'), uglify = require('gulp-uglify'), imagemin = require('gulp-imagemin'), rename = require('gulp-rename'), clean = require('gulp-clean'), concat = require('gulp-concat'), notify = require('gulp-notify'), cache = require('gulp-cache'), livereload = require('gulp-livereload');
示例文件
//建立一个gulpfile.js档案,载入外挂: var gulp = require('gulp'), sass = require('gulp-sass'), autoprefixer = require('gulp-autoprefixer'), minifycss = require('gulp-minify-css'), jshint = require('gulp-jshint'), uglify = require('gulp-uglify'), imagemin = require('gulp-imagemin'), rename = require('gulp-rename'), clean = require('gulp-clean'), concat = require('gulp-concat'), notify = require('gulp-notify'), cache = require('gulp-cache'), livereload = require('gulp-livereload'); //gulp.taskAPI用来建立任务。可以透过终端机输入$ gulp styles指令来执行任务。 gulp.task('styles', function() { return gulp.src('css/sass/pub.scss') .pipe(sass({ style: 'expanded' ,})) .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4')) .pipe(gulp.dest('dist/css')) .pipe(rename({suffix: '.min'})) .pipe(minifycss()) .pipe(gulp.dest('dist/css')) .pipe(notify({ message: 'Styles task complete' })); }); //JSHint,拼接及缩小化JavaScript gulp.task('scripts', function() { return gulp.src('src/scripts/**/*.js') .pipe(jshint('.jshintrc')) .pipe(jshint.reporter('default')) .pipe(concat('main.js')) .pipe(gulp.dest('dist/js')) .pipe(rename({suffix: '.min'})) .pipe(uglify()) .pipe(gulp.dest('dist/js')) .pipe(notify({ message: 'Scripts task complete' })); }); //对所有来源图片进行imagemin处理。 gulp.task('images', function() { return gulp.src('css/img/**/*') .pipe(imagemin({ optimizationLevel: 3, progressive: true, interlaced: true })) .pipe(gulp.dest('dist/assets/img')) .pipe(notify({ message: 'Images task complete' })); }); //佈署之前,清除目的地目录并重建档案–以防任何已经被删除的来源档案遗留在目的地目录之中 gulp.task('clean', function() { return gulp.src(['dist/assets/css', 'dist/assets/js', 'dist/assets/img'], {read: false}) .pipe(clean()); }); //预设任务,当只输入$ gulp指令时执行的任务 gulp.task('default', ['clean'], function() { gulp.start('styles', 'scripts', 'images'); }); //看守档案,并在更动发生后执行相关任务 gulp.task('watch', function() { // 看守所有.scss档 gulp.watch('css/sass/*.scss', ['styles']); // 看守所有.js档 // gulp.watch('src/scripts/**/*.js', ['scripts']); // 看守所有图片档 gulp.watch('css/img/**/*', ['images']); //即时重整(LiveReload),Gulp处理档案更动后,自动重新整理页面。 // 建立即时重整伺服器 var server = livereload(); // 看守所有位在 dist/ 目录下的档案,一旦有更动,便进行重整 gulp.watch(['dist/**']).on('change', function(file) { server.changed(file.path); }); });
gulp 命令
用 gulp +任务名 运行相应配置 用 gulp watch 监察目录变化并运行相应配置
如果是用gulp-sass ,它是基于node-sass的 ,并保持对应模块名一致,gulp-sass不基于ruby而是c#性能更好。