本文共 2752 字,大约阅读时间需要 9 分钟。
目录
安装
npm install gulp-clinpm install gulp -D
创建 gulpfile.js 文件
var gulp = require('gulp');var pug = require('gulp-pug');var less = require('gulp-less');var minifyCSS = require('gulp-csso');gulp.task('html', function(){ return gulp.src('client/templates/*.pug') .pipe(pug()) .pipe(gulp.dest('build/html'))});gulp.task('css', function(){ return gulp.src('client/templates/*.less') .pipe(less()) .pipe(minifyCSS()) .pipe(gulp.dest('build/css'))});gulp.task('default', [ 'html', 'css' ]);
排除目录
gulp.src(['js/**/*.js', '!js/**/*.min.js'])
// npm install --save-dev gulp gulp-changed gulp-jscs gulp-uglifyvar gulp = require('gulp');var changed = require('gulp-changed');var jscs = require('gulp-jscs');var uglify = require('gulp-uglify');var SRC = 'src/**/*.js';var DEST = 'dist';gulp.task('default', function() { return gulp.src(SRC) .pipe(changed(DEST)) // changed 任务需要提前知道目标目录位置才能找出哪些文件是被修改过的,只有被更改过的文件才会通过这里 .pipe(jscs()) .pipe(uglify()) .pipe(gulp.dest(DEST));});
显示处理中的文件
gulp.task('minify-css', function () { gulp.src([src + '/**/css/**/*.css', "!"+src + '/**/css/**/*.min.css']) .on('data', function(file) { console.log('%s Read %d bytes of data', file.path, file.contents.length); }) .pipe(concat("finally.css")) .pipe(rename({ suffix: '.min' })) .pipe(minifycss()) .pipe(gulp.dest( dist ));});
npm install --save-dev gulp-notify
var notify = require('gulp-notify');
.pipe(notify({ message: 'Styles task complete' }));
var gulp = require('gulp');var del = require('del');gulp.task('clean:mobile', function (cb) { del([ 'dist/**/css/*.min.css', 'dist/mobile/**/*', '!dist/mobile/deploy.json' ], cb);});gulp.task('default', ['clean:mobile']); // Cleangulp.task('clean', function() { return del(['dist/styles', 'dist/scripts', 'dist/images']);});
// Default taskgulp.task('default', ['clean'], function() { gulp.start('styles', 'scripts', 'images');});