使用中,还会继续完善
异步多任务
npm install gulp-cli -g
npm install gulp -D
npx -p touch nodetouch gulpfile.js
gulp --help
src(filePath/pathArr)
指向指定路径的所有文件,返回文件流对象;用于读取文件
dest(dirPath/pathArr)
指向指定的所有文件夹
cnpm install gulp@3.9.1 --save-dev 如果是gulp低版本,node最新版本不支持(我们现在都直接使用gulp4版本)
创建一个gulpfile.js的文件,专门让gulp去编写任务的。
gulpfile.js中,使用commIS规范
在gulpfile.js中编写任务,需要在控制台通过 gulp任务名,运行你编写好的任务
第一个参数:任务的名字 自定义
第二个参数:回调函数,任务执行的功能
gulp.src() 找到源文件路径
gulp.dest() 找到目的文件路径 【注】如果设置的这个目的文件路径不存在,会自动创建
pipe() 理解程序运行管道
gulp.task("copy-html", function(){
return gulp.src("index.html")
.pipe(gulp.dest("dist/"))
})
// 注册压缩html的任务
gulp.task(‘html‘, function(){
return gulp.src(‘index.html‘)
.pipe(htmlmin({collapseWhitespace: true}))
})
gulp.task("images", function(){
// return gulp.src("img/*.jpg")
// return gulp.src("img/*.{jpg,png}")
return gulp.src("img/**/*")
.pipe(gulp.dest("dist/images"))
})
gulp.task("data", function(){
return gulp.src(["json/*.json","xml/*.xml","!xml/4.xml"]) // 将 !xml/04.xml 剔除掉
.pipe(gulp.dest("dist/data"))
})
gulp.task("build", ["copy-html", "images", "data"], function(){
console.log("任务执行完毕")
})
gulp.watch("index.html", ["copy-html"]); // 第二个参数是数组
gulp.watch("img/**/*", ["images"]);
gulp.watch(["json/*.json","xml/*.xml","!xml/4.xml"], ["data"]);
使用第三方插件步骤:commonJS规范
<1>先去下载插件到本地
cnpm install 插件名字 --save-dev
cnpm i 插件名字 -D
<2>通过require()引入文件
<3>查阅插件的用法
```
// gulp-sass
const sass = require("gulp-sass");
// 压缩css gulp-minify-css
const minifyCSS = require("gulp-minify-css");
// 重命名插件 gulp-rename
const rename = require("gulp-rename");
// cssClean({compatibility: ‘ie8‘}) gulp-clean-css
gulp.task("sass", function(){
return gulp.src("stylesheet/index.scss")
.pipe(sass())
.pipe(minifyCSS())
.pipe(rename("index.min.css"))
.pipe(gulp.dest("dist/css"))
})
gulp.task("css", ["sass"] function(){ // 异步要做处理
})
```
```
// gulp-concat 合并文件(将文件合并)
const concat = require("gulp-concat");
// gulp-uglify 压缩.js
const uglify = require("gulp-uglify");
gulp.task("script", function(){
return gulp.src("js/*.js")
.pipe(concat("index.js"))
.pipe(uglify())
.pipe(rename("index.min.js")) // {suffix: ‘.min‘}
.pipe(gulp.dest("dist/js"))
// .pipe(connect.reload()) // 添加livereload后每个任务都需要监听一下
})
```
```
// gulp-connect 来启动一个服务器
const connect = require("gulp-connect");
gulp.task("server", function(){
connect.server({
root: "dist", // 设置根目录
port: "8888",
livereload: true, // 启动实时刷新功能
})
})
```
同时启动监听和服务 default我们可以直接在控制台通过 gulp命令启动
gulp.task("default", ["watch", "server"])
重点:jQuery编写的代码大家不要合并和压缩
gulp.task(‘watch‘, function () {
// gulp.watch([‘dist/index.html‘], [‘html‘]);
gulp.watch("src/index.html", ["copy-html"]); // 第二个参数是数组
gulp.watch("src/images/**/*", ["images"]);
gulp.watch("src/js/*.js", ["script"]);
});
var connect = require("gulp-connect");
var { createProxyMiddleware } = require(‘http-proxy-middleware‘);
gulp.task("server", function(){
connect.server({
// root: "dist", // 设置根目录
root: "src", // 设置根目录
port: "8888",
livereload: true, // 启动实时刷新功能
middleware: function (connect, opt) {
return [
createProxyMiddleware(‘/fs‘, {
target: ‘https://www.lagou.com‘,
changeOrigin:true,
pathRewrite:{ // 路径重写规则
‘^/fs‘:‘‘
}
}),
// proxy(‘/product‘, {
// target: ‘http://172.16.1.60:8080‘,
// changeOrigin:true
// }),
// proxy(‘/bpauth‘, {
// target: ‘http://192.168.24.77:8080‘,
// changeOrigin:true
// })
]
}
})
})
// 注册默认任务
//运行Gulp时,默认的Task
gulp.task(‘default‘, gulp.series([‘server‘, ‘watch‘], function(){
console.log("任务执行完毕")
}))
// 生产 gulp build
gulp.task(‘build‘, gulp.series([‘html‘,‘js‘,‘lib‘, ‘css‘, ‘images‘]));
原文:https://www.cnblogs.com/lisaShare/p/13182394.html