可能不少人会说如今提gulp也太落后了吧,但我想说写点东西并非为了讨论它是否过期,而是来帮助咱们本身来记忆、整理和学习。任何工具,我须要,我才去使用它,正如此时我须要gulp同样。css
为了效率而使用工具html
$ npm install --global gulp-cli
$ npm install --save-dev gulp
在项目根目录下建立一个名为 gulpfile.js 的文件:node
touch gulpfile.js
gulp.src(globs[, options])git
读取目标源文件github
gulp.dest(path[, options])web
向目标路径输出结果npm
gulp.pipe()gulp
将目标文件经过插件处理api
gulp.watch(glob [, opts], tasks) 或 gulp.watch(glob [, opts, cb])浏览器
监视文件系统,而且能够在文件发生改动时候作一些事情
gulp.task(name[, deps], fn): 任务
定义一个gulp任务
当配置完gulp.file后运行 gulp:
$ gulp
sass/scss编译
js代码校对
生成sprite雪碧图
本地起一个websocket服务,实时刷新浏览器
gulp-changed
会首先把文件进行比对,若是文件没有改动,则跳事后续任务,。gulp-changed
的对比功能更增强大,好比能够知道导入/依赖的文件是否更改。http-proxy-middleware
路由代理中间件
如下是个人gulp文件,仅供交流。
'use strict'; const gulp = require("gulp"); /** * [sass sass/scss编译] */ const sass = require("gulp-sass"); /** * [eslint js代码检测] */ const eslint = require('gulp-eslint'); /** * [connect 本地起一个websocket服务,实时刷新浏览器] */ const connect = require('gulp-connect'); /** * [changed 比较文件变更] * 默认状况下,gulp只能检测流中的文件是否更改。 * 若是您须要更高级的东西,好比知道导入/依赖的文件是否更改,则可使用该插件。 */ const changed = require('gulp-changed'); /** * [spritesmith 合并成雪碧图] */ const spritesmith= ("gulp.spritesmith"); /** * [proxy 中间代理件] */ const proxy = require('http-proxy-middleware'); let Pathconfig = { sassCompilePath: __dirname + "/scss/**/*.scss", //须要编译的scss文件路径 sassDestPath: __dirname + "/css/", //编译后的scss文件存放处 htmlSrcPath: __dirname + "/html/*.html", //监控的html路径 jsSrcPath: __dirname + "/js/*.js", //监控的js文件路径 } // html任务 gulp.task("html",function(){ gulp.src(Pathconfig.htmlSrcPath) .pipe(connect.reload()); }) // 样式任务 gulp.task("stylus",function(){ gulp.src(Pathconfig.sassCompilePath) .pipe(changed(Pathconfig.sassDestPath)) .pipe(sass()) .pipe(gulp.dest(Pathconfig.sassDestPath)) .pipe(connect.reload()); }) // js任务 gulp.task("js",function(){ gulp.src([Pathconfig.jsSrcPath,'!node_modules/**']) .pipe(eslint()) .pipe(eslint.formatEach('compact', process.stderr)) .pipe(connect.reload()); }) // 监控变更 gulp.task("watch",function(){ gulp.watch([Pathconfig.htmlSrcPath], ['html']); gulp.watch([Pathconfig.sassCompilePath], ['stylus']); gulp.watch([Pathconfig.jsSrcPath], ['js']); }) //定义livereload任务,起一个本地服务 gulp.task('connect',function () { connect.server({ root: __dirname, port: 8000, livereload: true }); }); gulp.task("default",['connect','watch'])