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
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/')); });
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。