gulp使用
gulp基本使用
yarn init 生成package.json文件
yarn add gulp --dev
code gulpfile.js 创建gulp入口文件
export.任务名 = done=>{
…
done()//任务完成标识
}
gulp 组合任务
const{series,parallel} = require(‘gulp’)
export.foo = series(‘任务1’,‘任务2’,‘任务3’)//串行执行
gulp.bar = parallel(‘任务1’,‘任务2’,‘任务3’)//并行执行
gulp异步任务
callback回调:done()执行完成标识
promise异步,返回Promise对象;Promise.resolve();Promise.reject(new Error(‘task failed’))
async()语法糖
const timeOut= time =>{
return new Promise(resolve=>{
setTimeout(resolve,time)
})
}
exports.async = async() =>{
await timeOut(1000)
}
gulp操作文件API
src创建读取流【原文件路径】 ,dist创建写入流【新文件】
例:
const {src,dest} = require(gulp)
export.default = ()=>{
return src(‘url’).pipe(dest(‘dist’))【生成dist文件】
}
gulp-clean-css 压缩css文件【yarn add gulp-clean-css --dev】
gulp-rename 重命名文件扩展名等【yarn add gulp-rename --dev】
gulp案例
gulp清除文件 :gulp-del
gulp自动加载插件:gulp-loda-plugins
gulp-useref 文件引用处理
gulp-if 判断【文件类型】
yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev 压缩html,js,css文件
gulpfile.js文件
const { src, dest, parallel, series, watch } = require('gulp')
//删除文件
const del = require('del')
//开发服务器 browser-sync
const browserSync = require('browser-sync')
const bs = browserSync.create()
//自动加载插件
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()
const data = {
menus: [
{
name: 'Home',
icon: 'aperture',
link: 'index.html'
},
{
name: 'Features',
link: 'features.html'
},
{
name: 'About',
link: 'about.html'
},
{
name: 'Contact',
link: '#',
children: [
{
name: 'Twitter',
link: 'https://twitter.com/w_zce'
},
{
name: 'About',
link: 'https://weibo.com/zceme'
},
{
name: 'divider'
},
{
name: 'About',
link: 'https://github.com/zce'
}
]
}
],
pkg: require('./package.json'),
date: new Date()
}
//文件清理
const clean = () => {
return del(['dist', 'temp'])
}
const style = () => {
return src('src/assets/styles/*.scss', { base: 'src' })
.pipe(plugins.sass({ outputStyle: 'expanded' }))
.pipe(dest('temp'))
.pipe(bs.reload({ stream: true }))
}
const script = () => {
return src('src/assets/scripts/*.js', { base: 'src' })
.pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
.pipe(dest('temp'))
.pipe(bs.reload({ stream: true }))
}
const page = () => {
return src('src/*.html', { base: 'src' })
.pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新
.pipe(dest('temp'))
.pipe(bs.reload({ stream: true }))
}
//图片压缩:gulp-imagemin
const image = () => {
return src('src/assets/images/**', { base: 'src' })
.pipe(plugins.imagemin())
.pipe(dest('dist'))
}
//字体处理:gulp-imagemin
const font = () => {
return src('src/assets/fonts/**', { base: 'src' })
.pipe(plugins.imagemin())
.pipe(dest('dist'))
}
const extra = () => {
return src('public/**', { base: 'public' })
.pipe(dest('dist'))
}
//编译
const serve = () => {
watch('src/assets/styles/*.scss', style)//监听文件是否改变,改变后重新执行目标任务
watch('src/assets/scripts/*.js', script)
watch('src/*.html', page)
// watch('src/assets/images/**', image)
// watch('src/assets/fonts/**', font)
// watch('public/**', extra)
watch([
'src/assets/images/**',
'src/assets/fonts/**',
'public/**'
], bs.reload)
bs.init({
notify: false,//页面提示关闭
port: 2080,//端口号配置
// open: false,//启动自动打开浏览器
// files: 'dist/**',//修改文件,页面自动刷新
server: {
baseDir: ['temp', 'src', 'public'],//指定构建文件夹
routes: {
'/node_modules【原文件夹】': 'node_modules【目标文件夹】'//现在routes中查找node_包文件复制到目标文件夹
}
}
})
}
const useref = () => {
return src('temp/*.html', { base: 'temp' })
.pipe(plugins.useref({ searchPath: ['temp', '.'] }))//gulp-useref 文件引用处理
// html js css
//gulp-htmlmin gulp-uglify gulp-clean-css 压缩html,js,css文件
.pipe(plugins.if(/\.js$/, plugins.uglify()))//gulp-if 判断文件类型
.pipe(plugins.if(/\.css$/, plugins.cleanCss()))
.pipe(plugins.if(/\.html$/, plugins.htmlmin({
collapseWhitespace: true,//删除换行,空白字符
minifyCSS: true,//删除html文件中行内样式空格
minifyJS: true//删除htmml文件中script标签空格
})))
.pipe(dest('dist'))
}
//组合任务 parallel
const compile = parallel(style, script, page)
// 上线之前执行的任务
const build = series(
clean,
parallel(
series(compile, useref),
image,
font,
extra
)
)
const develop = series(compile, serve)
module.exports = {
clean,
build,
develop
}