1、简介
- 定义
- gulp:基于流的打包构建工具
- webpack:基于 js 文件的打包构建工具
- webpack:基于 js 文件的打包构建工具
- 依赖于 node 环境进行开发
- 底层封装的内容就是 node 里面的读写文件
- 作用
- 安装
- npm install --global gulp
2、常用配置
const gulp = require("gulp");
const cssmin = require("gulp-cssmin");
const antoPreFixer = require("gulp-autoprefixer");
const sass = require("gulp-sass")(require("sass"));
const uglify = require("gulp-uglify");
const babel = require("gulp-babel");
const htmlMin = require("gulp-htmlmin");
const clean = require("gulp-clean");
const webServer = require("gulp-webserver");
const fileInclude = require("gulp-file-include");
const cssHandler = function () {
return gulp
.src("./src/css/*.css")
.pipe(antoPreFixer())
.pipe(cssmin())
.pipe(gulp.dest("./dist/css/"));
};
const sassHandler = function () {
return gulp
.src("./src/css/*.scss")
.pipe(sass())
.pipe(antoPreFixer())
.pipe(cssmin())
.pipe(gulp.dest("./dist/sass/"));
};
const jsHandler = function () {
return gulp
.src("./src/js/*.js")
.pipe(
babel({
presets: ["@babel/env"],
})
)
.pipe(uglify())
.pipe(gulp.dest("./dist/js/"));
};
const htmlHandler = function () {
return gulp
.src("./index.html")
.pipe(fileInclude({
prefix: '@-@',
basepath: './src/components'
}))
.pipe(
htmlMin({
collapseWhitespace: true,
removeEmptyAttributes: true,
removeAttributeQuotes: true,
minifyCSS: true,
minifyJS: true,
})
)
.pipe(gulp.dest("./dist/"));
};
const imagesHandler = function () {
return gulp
.src("./src/images/**")
.pipe(gulp.dest("./dist/images/"));
};
const libHandler = function () {
return gulp
.src("./src/lib/**/*")
.pipe(gulp.dest("./dist/lib/"));
};
const cleanHandler = function () {
return gulp.src('./dist/', { allowEmpty: true }).pipe(clean());
}
const webServerHandler = function () {
return gulp
.src('./dist/')
.pipe(webServer({
host: 'www.mzy.com',
port: '8088',
livereload: true,
open: './index.html',
proxies: [
{
source: '/ddd',
target: 'https://www.bai.com/aa/bb/cc'
}
]
}))
}
const watchHandler = function () {
gulp.watch('./src/css/*.scss', sassHandler)
gulp.watch('./src/js/*.js', jsHandler)
gulp.watch('./index.html', htmlHandler)
}
module.exports.cssHandler = cssHandler;
module.exports.sassHandler = sassHandler;
module.exports.jsHandler = jsHandler;
module.exports.htmlHandler = htmlHandler;
module.exports.imagesHandler = imagesHandler;
module.exports.libHandler = libHandler;
module.exports.cleanHandler = cleanHandler;
module.exports.webServerHandler = webServerHandler;
module.exports.watchHandler = watchHandler;
module.exports.default = gulp.series(
cleanHandler,
gulp.parallel(cssHandler, sassHandler, jsHandler, htmlHandler, imagesHandler, libHandler),
webServerHandler,
watchHandler
)