Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | About

4.5. JS Minification

		
npm install --save-dev gulp-uglify
		
		

gulpfile.js:

		
// including plugins
var gulp = require('gulp');
var uglify = require("gulp-uglify");
 
// task
gulp.task('minify-js', function () {
    gulp.src('./JavaScript/*.js') // path to your files
    .pipe(uglify())
    .pipe(gulp.dest('path/to/destination'));
});
		
		

Run:

		
gulp minify-js
		
		

4.5.1. JS 校验

			
var gulp = require('gulp');
var header = require('gulp-header');  //给文本文件头部追加内容
var footer = require('gulp-footer');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint'); //js代码校验
var cached = require('gulp-cached');
var remember = require('gulp-remember'); //gulp-remember is a gulp plugin that remembers files that have passed through it. gulp-remember adds all the files it has ever seen back into the stream.

gulp.task('scripts', function() {
  return gulp.src('src/**/*.js')
      .pipe(cached('scripts'))        // 只传递更改过的文件 
      .pipe(jshint())                 // 对这些更改过的文件做一些特殊的处理...
      .pipe(header('(function () {')) // 比如 jshinting ^^^
      .pipe(footer('})();'))          // 增加一些类似模块封装的东西
      .pipe(remember('scripts'))      // 把所有的文件放回 stream
      .pipe(concat('main.js'))         // 合并文件的操作
      .pipe(gulp.dest('public/'));
});