一、安装
1.安装gulp
npm install -g gulp
2.检查gulp 版本
gulp -v
3.在项目文件夹下安装gulp
npm install --save-dev gulp
二、压缩JS
1.安装gulp-uglify模块
npm install gulp-uglify
2.在项目根目录创建gulpfile.js文件
3.在gulpfile.js文件中写入代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var gulp = require('gulp');
var uglify = require('gulp-uglify');
gulp.task('jscompress', function() { return gulp.src('js/1.js') .pipe(uglify()) .pipe(gulp.dest('dist/js')); });
|
4.命令行中切换到gulpfile.js所在的目录,执行如下命令开始压缩
gulp script
5.在gulpfile.js中添加自动监视任务
1 2 3 4 5
| gulp.task('auto', function () { gulp.watch('js/1.js', ['jscompress']); });
|
6.在gulpfile.js中设置默认任务(即命令行中输入gulp执行的任务)
1 2 3
|
gulp.task('default', ['auto']);
|
三、压缩CSS
1.安装gulp-clean-css模块
npm install gulp-clean-css
2.在gulpfile.js文件中添加相应任务
1 2 3 4 5 6 7 8 9 10 11 12 13
| var cleanCSS = require('gulp-clean-css');
gulp.task('csscompress', function() { return gulp.src('css/my.css') .pipe(cleanCSS()) .pipe(gulp.dest('dist/css')); });
|
3.修改添加对应的自动监视任务
1 2 3 4 5 6
| gulp.task('auto', function () { gulp.watch('js/1.js', ['jscompress']); gulp.watch('css/my.css', ['csscompress']); });
|
四、重命名文件
1.安装gulp-rename模块
npm install gulp-rename
2.修改gulpfile.js内代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| var gulp = require('gulp');
var uglify = require('gulp-uglify');
var cleanCSS = require('gulp-clean-css'); var rename = require("gulp-rename"); var gutil = require('gulp-util');
gulp.task('jscompress', function() { return gulp.src('js/1.js') .pipe(rename({suffix: '.min'})) .pipe(uglify()) .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); }) .pipe(gulp.dest('dist/js')); });
gulp.task('csscompress', function() { return gulp.src('css/my.css') .pipe(rename({suffix: '.min'})) .pipe(cleanCSS()) .pipe(gulp.dest('dist/css')); });
gulp.task('auto', function () { gulp.watch('js/1.js', ['jscompress']); gulp.watch('css/my.css', ['csscompress']); });
gulp.task('default', ['auto']);
|
五、增加ES6支持
1.安装gulp-babel模块
npm install --save-dev babel-core gulp-babel babel-preset-es2015
2.修改文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| var gulp = require('gulp');
var uglify = require('gulp-uglify');
var cleanCSS = require('gulp-clean-css'); var rename = require("gulp-rename"); var gutil = require('gulp-util'); var babel = require('gulp-babel');
gulp.task('jscompress', function() { return gulp.src('js/1.js') .pipe(rename({suffix: '.min'})) .pipe(babel({ presets: ['es2015'] })) .pipe(uglify()) .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); }) .pipe(gulp.dest('js')); });
gulp.task('csscompress', function() { return gulp.src('css/my.css') .pipe(rename({suffix: '.min'})) .pipe(cleanCSS()) .pipe(gulp.dest('css')); });
gulp.task('auto', function () { gulp.watch('js/1.js', ['jscompress']); gulp.watch('css/my.css', ['csscompress']); });
gulp.task('default', ['auto']);
|
参考资料:
https://github.com/nimojs/gulp-book
http://www.cnblogs.com/Tzhibin/p/4318457.html
https://stackoverflow.com/questions/34398338/uglification-failed-unexpected-character