1.引入gulp模块
yarn add gulp --dev
2.添加入口文件gulpfile.js
3.引入gulp批量插件gulp-load-plugins ,减少手动引入每一个gulp插件
yarn add gulp-load-plugins --dev
4.样式编译,并对css文件进行编译
yarn add gulp-sass
const style = () => {
return src('src/assets/styles/*.scss', { base: 'src' })//通过base确定转换过后的基准目录
.pipe(plugins.sass({ outputStyle: 'expanded' }))//完全展开
.pipe(dest('temp'))//建立temp,防止边读取边写入造成文件无法生成问题
}
5.脚本编译
使用gulp-babel、@babel/core、@babel/preset-env插件编译js文件,将es6转换为es5
yarn add gulp-babel @babel/core @babel/preset-env --dev
const script = () => {
return src('src/assets/scripts/*.js', { base: 'src' })
.pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
.pipe(dest('temp'))
}
6.页面模板编译
引入gulp-swin插件编译
yarn add gulp-swig --dev
const page = () => {
return src('src/*.html', { base: 'src' })
.pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新
.pipe(dest('temp'))
}
7.图片和字体文件转换
一般字体不需要压缩,主要是图片压缩
yarn add gulp-imagemin --dev
const image = () => {
return src('src/assets/images/**', { base: 'src' })
.pipe(plugins.imagemin())
.pipe(dest('dist'))
}
const font = () => {
return src('src/assets/fonts/**', { base: 'src' })
.pipe(plugins.imagemin())
.pipe(dest('dist'))
}
8.其他文件及文件清除
public文件直接拷贝;使用del插件将原先编译后的文件目录删除
yarn add del --dev
const extra = () => {
return src('public/**', { base: 'public' })
.pipe(dest('dist'))
}
const clean = () => {
return del(['dist', 'temp'])
}
9.开发服务器
安装browser-sync插件,当代码修改并保存时,浏览器热更新,提高开发效率
yarn add browser-sync --dev
const serve = () => {
//监听源文件变化
watch('src/assets/styles/*.scss', style)
watch('src/assets/scripts/*.js', script)
watch('src/*.html', page)
//减少构建次数
watch([
'src/assets/images/**',
'src/assets/fonts/**',
'public/**'
], bs.reload)
bs.init({
notify: false,//服务器提示
port: 2080,//端口
// open: false,//默认开启浏览器
// files: 'temp/**',//启动后自动监听的文件
server: {
baseDir: ['temp', 'src', 'public'],//设置根目录
routes: {//特殊路由,优先basedir从这里寻找文件
'/node_modules': 'node_modules' }
}
})
}
10.useref文件引用处理及文件压缩
1.使用 gulp-useref 插件可以将 HTML 引用的多个 CSS 和 JS 合并起来,减小依赖的文件个数,从而减少浏览器发起的请求次数。gulp-useref 根据注释将 HTML 中需要合并压缩的区块找出来,对区块内的所有文件进行合并。需要注意的是,gulp-useref只负责合并,不负责压缩
使用 gulp-if 插件来判断读取流文件类型,并压缩对应文件
yarn add gulp-if --dev
yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
yarn add gulp-useref --dev
const useref = () => {
return src('temp/*.html', { base: 'temp' })
.pipe(plugins.useref({ searchPath: ['temp', '.'] }))
.pipe(plugins.if(/\.js$/, plugins.uglify()))
.pipe(plugins.if(/\.css$/, plugins.cleanCss()))
.pipe(plugins.if(/\.html$/, plugins.htmlmin({
collapseWhitespace: true,//删除空格
minifyCSS: true,//压缩行内样式
minifyJS: true//压缩行内js
})))
.pipe(dest('dist'))
}
11.重新梳理构建过程及完整代码
// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
//建立temp,防止边读取边写入造成文件无法生成问题
//区别开发时build和上线之前构建,建立个临时目录temp,最终上线用dist
const del = require('del')
// yarn add browser-sync --dev
const browserSync = require('browser-sync')
const loadPlugins = require('gulp-load-plugins')
//自动加载插件 yarn add gulp-load-plugins --dev
const plugins = loadPlugins()
//创建开发服务器
const bs = browserSync.create()
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' })//通过base确定转换过后的基准目录
.pipe(plugins.sass({ outputStyle: 'expanded' }))//完全展开
.pipe(dest('temp'))//建立temp,防止边读取边写入造成文件无法生成问题
}
const script = () => {
return src('src/assets/scripts/*.js', { base: 'src' })
.pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
.pipe(dest('temp'))
}
const page = () => {
return src('src/*.html', { base: 'src' })
.pipe(plugins.swig({ data, defaults: { cache: false } })) // 防止模板缓存导致页面不能及时更新
.pipe(dest('temp'))
}
const image = () => {
return src('src/assets/images/**', { base: 'src' })
.pipe(plugins.imagemin())
.pipe(dest('dist'))
}
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/**',//指定dist文件发生改变及时热部署
server: {
baseDir: ['temp', 'src', 'public'],//设置根目录
routes: {//特殊路由,优先basedir从这里寻找文件
'/node_modules': 'node_modules' }
}
})
}
const useref = () => {
//yarn add gulp-useref --dev
return src('temp/*.html', { base: 'temp' })
.pipe(plugins.useref({ searchPath: ['temp', '.'] }))
// html js css
//yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
//yarn add gulp-if --dev
.pipe(plugins.if(/\.js$/, plugins.uglify()))
.pipe(plugins.if(/\.css$/, plugins.cleanCss()))
.pipe(plugins.if(/\.html$/, plugins.htmlmin({
collapseWhitespace: true,//删除空格
minifyCSS: true,//压缩行内样式
minifyJS: true//压缩行内js
})))
.pipe(dest('dist'))
}
const compile = parallel(style, script, page)
// 上线之前执行的任务
const build = series(
clean,
parallel(
series(compile, useref),//useref依赖compile
image,
font,
extra
)
)
//开发时执行
const develop = series(compile, serve)
module.exports = {
clean,
build,
develop
}