1、首先全局安装gulp
全局安装就不做介绍了
初学gulp,终于把常用的配置,api,语法弄明白了!
gulp插件地址:http://gulpjs.com/plugins
gulp官方网址:http://gulpjs.com
gulp 官方API:https://github.com/gulpjs/gulp/blob/master/docs/API.md
项目的目录结构应该是
1 D:. 2 └─文件名 3 ├─src 4 │ ├─css 5 │ ├─fonts 6 │ ├─images 7 │ └─js 8 └─dist 9 ├─css 10 ├─fonts 11 ├─images 12 └─js 13 gulpfile.js 14 package.json
src是
2、新建一个package.json
1 npm init
命令行中会出现
1 Press ^C at any time to quit. 2 name: (a1) 3 version: (1.0.0) 4 description: 5 entry point: (index.js) 6 test command: 7 git repository: 8 keywords: 9 author: 10 license: (ISC)
填各种信息,填完出现下面
1 { 2 "name": "a1",//项目名称(必填) 3 "version": "1.0.0",//项目版本(必填) 4 "description": "a test",//项目描述(必填) 5 "main": "gulpfile.js",//入口文件 6 "test command" //测试命令 7 "git repository" //git 地址 8 "keywords" //关键字 9 "scripts": { 10 "test": "echo "Error: no test specified" && exit 1" 11 }, 12 "author": "",//作者 13 "license": "ISC" //许可信息 14 } 15 16 Is this ok? (yes)
为了能正常使用,我们还得本地安装gulp:
1 npm install gulp --save-dev;
全局安装gulp是为了执行gulp任务,本地安装gulp则是为了调用gulp插件的功能。
之后就是需要什么就安装什么插件了
1 npm install gulp-htmlmin gulp-imagemin imagemin-pngcrush gulp-minify-css gulp-uglify gulp-concat gulp-rename gulp-notify --save-dev
安装js校验插件
npm install --save-dev jshint gulp-jshint
–save:将保存配置信息至package.json
-dev:保存至package.json的devDependencies节点,不指定-dev将保存至dependencies节点;
3、创建gulpfile.js文件
1 vim gulpfile.js
输入代码
1 // 引入 gulp及组件 2 //插件的引入方法 和任务建立方法参照gulp的官网插件 3 var gulp = require('gulp'), 4 autoprefixer = require('gulp-autoprefixer'), 5 minifycss = require('gulp-minify-css'), //压缩css 6 jshint = require('gulp-jshint'), //js代码校验 7 uglify = require('gulp-uglify'), //压缩JS 8 imagemin = require('gulp-imagemin'), //压缩图片 9 rename = require('gulp-rename'), //合并js文件 10 concat = require('gulp-concat'), 11 notify = require('gulp-notify'), 12 cache = require('gulp-cache'), 13 livereload = require('gulp-livereload'), 14 del = require('del'); 15 htmlmin = require('gulp-htmlmin');//压缩html代码 16 //建立任务: Styles 17 gulp.task('styles', function() { 18 return gulp.src('src/styles/*.css') 19 .pipe(rename({ 20 suffix: '.min' 21 })) 22 .pipe(minifycss()) 23 .pipe(gulp.dest('dist/styles')) 24 .pipe(notify({ 25 message: 'Styles task complete' 26 })); 27 }); 28 //建立任务: htmlmin 29 gulp.task('htmlmin', function() { 30 // src/**/*.html 这个代表的是src下边的html文件以及他的子文件下的所有的html文件 31 return gulp.src('src/*.html') 32 .pipe(rename({ 33 suffix: 'min' 34 })) 35 .pipe(htmlmin({collapseWhitespace: true})) 36 .pipe(gulp.dest('dist')) 37 .pipe(notify({ 38 message: ' task complete' 39 })); 40 }); 41 // Scripts 42 gulp.task('scripts', function() { 43 return gulp.src('src/scripts/**/*.js') 44 .pipe(jshint()) 45 .pipe(jshint.reporter('default')) 46 .pipe(concat('main.js')) 47 .pipe(rename({ 48 suffix: '.min' 49 })) 50 .pipe(uglify()) 51 .pipe(gulp.dest('dist/scripts')) 52 .pipe(notify({ 53 message: 'Scripts task complete' 54 })); 55 }); 56 // Images 57 gulp.task('images', function() { 58 return gulp.src('src/images/**/*') 59 .pipe(cache(imagemin({ 60 optimizationLevel: 3, 61 progressive: true, 62 interlaced: true 63 }))) 64 .pipe(gulp.dest('dist/images')) 65 .pipe(notify({ 66 message: 'Images task complete' 67 })); 68 }); 69 // Clean 任务执行前,先清除之前生成的文件 70 gulp.task('clean', function(cb) { 71 del(['dist/assets/css', 'dist/assets/js', 'dist/assets/img'], cb) 72 }); 73 // Default task 设置默认任务 74 gulp.task('default', ['clean'], function() { 75 gulp.start('styles', 'scripts', 'images'); 76 }); 77 // 监听文件: Watch 78 gulp.task('watch', function() { 79 // Watch .scss files 80 gulp.watch('src/styles/**/*.css', ['styles']); 81 // Watch .js files 82 gulp.watch('src/scripts/**/*.js', ['scripts']); 83 // Watch image files 84 gulp.watch('src/images/**/*', ['images']); 85 // Create LiveReload server 86 livereload.listen(); 87 // Watch any files in dist/, reload on change 88 gulp.watch(['dist/**']).on('change', livereload.changed); 89 });
4、查看js 和 json 文件
json 文件现在应该是
1 { 2 "name": "gulp-learn", 3 "version": "1.0.0", 4 "description": "", 5 "main": "gulpfile.js", 6 "scripts": { 7 "test": "echo "Error: no test specified" && exit 1" 8 }, 9 "author": "haonan", 10 "license": "ISC", 11 "devDependencies": { 12 "gulp": "^3.9.1", 13 "gulp-clean-css": "^3.0.2", 14 "gulp-concat": "^2.6.1", 15 "gulp-htmlmin": "^3.0.0", 16 "gulp-imagemin": "^3.1.1", 17 "gulp-jshint": "^2.0.4", 18 "gulp-minify-css": "^1.2.4", 19 "gulp-notify": "^3.0.0", 20 "gulp-rename": "^1.2.2", 21 "gulp-uglify": "^2.0.1", 22 "imagemin-pngcrush": "^5.0.0", 23 "jshint": "^2.9.4" 24 "package.json" 25L, 594C
5、开始运行压缩
js文件中的
gulp.task('images', function() {
task也就是任务后边的 '' 里面是什么就代表是什么方法,在终端运行
gulp 方法 就行