gulpfile.js如下: gulpfile.js如下 : 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031
gulpfile.js如下 :
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113/** npm install gulp gulp-concat gulp-clean-css gulp-fontmin gulp-uglify gulp-clean gulp-rename run-sequence gulp-htmlmin browser-sync --save-dev **/var gulp = require('gulp');var cOncat= require('gulp-concat'); //合并文件var cleanCss = require('gulp-clean-css'); //压缩cssvar fOntmin= require('gulp-fontmin');var uglify = require('gulp-uglify'); // 压缩 代码var clean = require('gulp-clean');var rename = require('gulp-rename');var runSequence = require('run-sequence');var htmlmin = require('gulp-htmlmin');var browserSync = require("browser-sync").create() // 自动刷新var path = { dist:'./dist', fontSrcPath:'./src/css/themes/**/*.*', cssSrcPath:'./src/css/**.css', jsSrcPath:'./src/js/**/*.js', libSrcPath:'./src/lib/*.js', htmlSrcPath:'./src/**/*.html', fontDistPath:'./dist/css/themes/', cssDistPath:'./dist/css/', jsDistPath:'./dist/js/', libDistPath:'./dist/lib/', htmlDistPath:'./dist/',};/*清空文件夹*/gulp.task('clean',function(){ return gulp.src(path.dist,{read: false}) .pipe(clean());});gulp.task('css', function() { return gulp.src(path.cssSrcPath) .pipe(concat('all.min.css')) .pipe(cleanCss({compatibility: 'ie8'})) .pipe(gulp.dest(path.cssDistPath));});gulp.task('font', function () { return gulp.src(path.fontSrcPath) .pipe(fontmin()) .pipe(gulp.dest(path.fontDistPath));});//压缩js代码gulp.task('js', function() { return gulp.src(path.jsSrcPath) .pipe(concat('all.min.js')) // 拼接成 一个js .pipe(uglify()) //压缩 js代码 .pipe(gulp.dest(path.jsDistPath)) //输出到指定目录});gulp.task('jsLib', function() { return gulp.src(path.libSrcPath) .pipe(rename({suffix:'.min'})) .pipe(uglify()) .pipe(gulp.dest(path.libDistPath));});gulp.task('html', function () { var optiOns= { removeComments: true, collapseWhitespace: true, collapseBooleanAttributes: true, removeEmptyAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, minifyJS: true, minifyCSS: true }; gulp.src(path.htmlSrcPath) .pipe(htmlmin(options)) .pipe(gulp.dest(path.htmlDistPath));});//gulp运行的时候gulp.task("default", ['server']);gulp.task('watch',['watch:css','watch:js','watch:html']);gulp.task("init", ['css','font', 'js','jsLib', 'html']);gulp.task('server', ["init","watch"], function() { browserSync.init({ server: { baseDir: ["src"] }, port: 80 });});//监听cssgulp.task("watch:css", function() { gulp.watch(path.cssSrcPath,['css'], browserSync.reload);});//监听jsgulp.task("watch:js", function() { gulp.watch(path.jsSrcPath,['js'], browserSync.reload);});//监听htmlgulp.task("watch:html", function() { gulp.watch(path.htmlSrcPath,['html'], browserSync.reload);});//构建生产gulp.task('build', function () { runSequence('clean',['css','font', 'js','jsLib', 'html']);});