前端自动化构建
自动化转换工作流,作用是为了脱离运行环境兼容带来的问题
开发阶段使用一些提高效率的语法、规范和标准
常用的自动化构建工具,
grunt gulp fis
Grunt
基本使用
mkdir sample-grunt
sudo yarn init --yes
sudo yarn link
新建入口文件gruntfile.js
//grunt的入口文件,用于定义一些要grunt自动执行的任务
//需要导出一个函数,此函数接受一个形参,内部提供一些创建任务时需要的API
module.exports = grunt => {
//注册任务 指定任务名字,指定任务函数,任务发生时自动执行的函数
//yarn grunt foo
grunt.registerTask('foo', ()=>{
console.log('hello grunt')
})
//yarn grunt --help获取grunt的帮助信息 yarn grunt bar
grunt.registerTask('bar', '任务描述', ()=>{
console.log('hello grunt')
})
//yarn grunt
// grunt.registerTask('default', ()=>{
// console.log('自动调用default')
// })
//yarn grunt
grunt.registerTask('default', ['foo', 'bar'])
//grunt对异步任务的支持,默认支持同步模式
// grunt.registerTask('async-task', ()=>{
// setTimeout(()=>{
// console.log('async task work')
// }, 1000)
// })
grunt.registerTask('async-task', function(){
const done = this.async()
setTimeout(()=>{
console.log('async task work')
done()
}, 1000)
})
}
Grunt标记任务失败
//grunt的入口文件,用于定义一些要grunt自动执行的任务
//需要导出一个函数,此函数接受一个形参,内部提供一些创建任务时需要的API
module.exports = grunt => {
grunt.registerTask('bad', '任务失败', ()=>{
console.log('grunt bad')
return false
})
//注册任务 指定任务名字,指定任务函数,任务发生时自动执行的函数
//yarn grunt foo
grunt.registerTask('foo', ()=>{
console.log('hello grunt')
})
//yarn grunt --help获取grunt的帮助信息 yarn grunt bar
grunt.registerTask('bar', '任务描述', ()=>{
console.log('hello grunt')
})
//yarn grunt bar任务不会被执行
//yarn grunt --force会被强制执行
grunt.registerTask('default', ['foo', 'bad', 'bar'])
//yarn grunt async-bad异步执行抛出错误,标记此任务为失败的任务
grunt.registerTask('async-bad', function(){
const done = this.async()
setTimeout(()=>{
console.log('async task work')
done(false)
}, 1000)
})
}
Grunt的配置选项方法
//grunt的入口文件,用于定义一些要grunt自动执行的任务
//需要导出一个函数,此函数接受一个形参,内部提供一些创建任务时需要的API
module.exports = grunt => {
// grunt.initConfig({
// foo:'bar'
// })
grunt.initConfig({
foo:{
bar:'123'
}
})
grunt.registerTask('foo', ()=>{
console.log(grunt.config('foo.bar'))
})
}
Grunt多目标任务
module.exports = grunt => {
//yarn grunt build
//yarn grunt build:css
grunt.initConfig({
build:{
options:{
foo:'bar',
},
css:{
options:{
foo:'baz'
}
},
js:'2'
}
})
//多目标模式,可以将任务根据配置形成多个子任务
grunt.registerMultiTask('build', function() {
console.log(this.options())
console.log(`target:${this.target}, data:${this.data}`)
})
}
Grunt插件的使用
sudo yarn add grunt-contrib-clean
yarn grunt clean 删除temp下对应的文件
module.exports = grunt => {
grunt.initConfig({
clean:{
temp:'temp/*.txt'
}
})
grunt.loadNpmTasks('grunt-contrib-clean')
}
Grunt常用的插件以及总结
sudo yarn add grunt-sass sass -dev
yarn grunt sass
// 核心模块 预设
sudo yarn add grunt-babel @babel/core @babel/preset-env --dev
yarn grunt babel
sudo yarn grunt-contrib-watch --dev
目标文件发生变化,触发监听
yarn grunt watch
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')
module.exports = grunt => {
grunt.initConfig({
sass:{
options:{
//调试配置
sourceMap:true,
implementation: sass
},
main: {
files: {
//目标文件路径:当前文件路径
'dist/css/main.css': 'src/scss/main.scss'
}
}
},
babel:{
options:{
sourceMap:true,
presets:['@babel/preset-env']
},
main: {
files: {
//目标文件路径:当前文件路径
'dist/js/app.js': 'src/js/app.js'
}
}
},
// sudo yarn grunt watch su当下面的文件内容发生变化触发
watch:{
js:{
files:['src/js/*.js'],
tasks:['babel']
},
css:{
files:['src/scss/*.scss'],
tasks:['sass']
},
}
})
//当模块变多时,loadNpmTasks也会被多次使用,为了减少使用,
//sudo yarn add load-grunt-tasks --dev
// grunt.loadNpmTasks('grunt-sass')
loadGruntTasks(grunt)//自动加载所有grunt插件中的任务
//一开始为了避免watch不执行
grunt.registerTask('default', ['sass', 'babel', 'watch'])
}
Gulp
Gulp的基本使用
mkdir sample-gulp
sudo yarn init
sudo yarn add gulp --dev
新建入口文件gulpfile.js
//gulp的入口文件
//yarn gulp foo
exports.foo = done => {
console.log('foo task')
done()
}
//yarn gulp default
exports.default = done => {
console.log('default task')
done()
}
//gulpd 4.0之前使用
//yarn grunt bar
const gulp = require('gulp')
gulp.task('bar', done => {
console.log('bar task')
done()
})
Gulp的组合任务
//创建并行任务和串行任务
const {series, parallel} = require('gulp')
const task1 = done=>{
setTimeout(()=>{
console.log('task1 working')
done()
}, 1000)
}
const task2 = done=>{
setTimeout(()=>{
console.log('task2 working')
done()
}, 1000)
}
const task3 = done=>{
setTimeout(()=>{
console.log('task3 working')
done()
}, 1000)
}
//yarn grunt foo task1->task2->task3
exports.foo = series(task1, task2, task3)
//yarn grunt bar 会一起执行 比如说css和js编译 部署时
exports.bar = parallel(task1, task2, task3)
Gulp中的异步任务的三种方式
//创建并行任务和串行任务
exports.callback = done=>{
console.log('callback task')
done()
}
exports.callback_error = done=>{
console.log('callback_error task')
done(new Error('task failed'))
//当有报错时后续的任务不会被执行
}
exports.Promise = done=>{
console.log('promise task')
return Promise.resolve()
}
exports.Promise_error = done=>{
console.log('promise_error task')
return Promise.reject(new Error('task failed'))
}
//处理异步的方式
const timeout = time =>{
return new Promise(resolve => {
setTimeout(resolve, time)
})
}
exports.async= async()=>{
await(1000)
console.log('async task')
}
//最常用的饿处理异步的方式,stream
const fs = require('fs')
// exports.stream=()=>{
// const readStream = fs.createReadStream('package.json')
// const writeStream = fs.createWriteStream('temp.txt')
// readStream.pipe(writeStream)
// return readStream
// }
//模拟gulp中结束stream事件
exports.stream = done =>{
const readStream = fs.createReadStream('package.json')
const writeStream = fs.createWriteStream('temp.txt')
readStream.pipe(writeStream)
readStream.on('end', ()=>{
done()
})
}
构建过程核心的工作原理
将文件读出来写入到另外一个位置
The streaming build system
//最常用的饿处理异步的方式,stream
const fs = require('fs')
const {Transform } = require('stream')
exports.default = () =>{
//文件读取流
const read = fs.createReadStream('normalize.css')
//文件写入流
const write = fs.createWriteStream('normalize.min.css')
//文件转换流
const transform = new Transform({
transform:(chunk, encoding, callback) =>{
//核心转换过程
//chunk=>读取流读到的内容(Buffer)
const input = chunk.toString()
const output = input.replace(/\s+/g, '').repalce(/\/\*.+?\*\//g, '')
callback(null, output)
}
})
//把读取的文件流写入文件流
read.pipe(transform)//转换
.pipe(write)//写入
return read
}
Gulp
文件操作API+插件的使用
sudo yarn add gulp-clean-css
sudo yarn add gulp-rename
//最常用的饿处理异步的方式,stream
const {src, dest} = require('gulp')
const cleanCss = require('gulp-clean-css')
const reName = require('gulp-rename')
exports.default = ()=>{
//src转换流,dest写入流
return src('src/*.css')
.pipe(cleanCss())
.pipe(reName({
extname:'.min.css'
}))
.pipe(dest('dist'))
}
Gulp案例-样式编译
sudo yarn add gulp --dev
sudo yarn add gulp-sass --dev
yarn gulp style
const {src, dest} = require('gulp')
const sass = require('gulp-sass')
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(sass())
.pipe(dest('dist'))
}
module.exports={
style
}
脚本编译
sudo yarn add gulp-babel @babel/core @babel/preset-env --dev
yarn gulp script
const {src, dest} = require('gulp')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
module.exports={
style,
script
}
页面模版编译
sudo yarn add gulp-swig --dev
yarn gulp page
yarn gulp compile
const {src, dest, parallel} = require('gulp')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
const swig = require('gulp-swig')
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 style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(swig({data }))
.pipe(dest('dist'))
}
const compile = parallel(style, script, page)
module.exports={
compile
}
图片和文字文件转换
sudo yarn add gulp-imagemin --dev
yarn gulp compile
const {src, dest, parallel} = require('gulp')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
const swig = require('gulp-swig')
const imagemin = require('gulp-imagemin')
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 style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(swig({data }))
.pipe(dest('dist'))
}
//图片压缩
const image = () =>{
return src('src/assets/images/**', {base:'src'})
.pipe(imagemin())
.pipe(dest('dist'))
}
const font = () =>{
return src('src/assets/fonts/**', {base:'src'})
.pipe(imagemin())
.pipe(dest('dist'))
}
const compile = parallel(style, script, page, image, font)
module.exports={
compile
}
其他文件及文件清除
//自动帮我删除指定文件
sudo yarn add del --dev
const {src, dest, parallel, series} = require('gulp')
const delet = require('del')
const sass = require('gulp-sass')
const babel = require('gulp-babel')
const swig = require('gulp-swig')
const imagemin = require('gulp-imagemin')
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 delete(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(swig({data }))
.pipe(dest('dist'))
}
//图片压缩
const image = () =>{
return src('src/assets/images/**', {base:'src'})
.pipe(imagemin())
.pipe(dest('dist'))
}
const font = () =>{
return src('src/assets/fonts/**', {base:'src'})
.pipe(imagemin())
.pipe(dest('dist'))
}
const extra = () =>{
return src('public/**', {base:'public'})
.pipe(dest('dist'))
}
const compile = parallel(style, script, page, image, font)
const build = series(clean, parallel(compile, extra))
module.exports={
compile,
build
}
自动加载插件
sudo yarn add gulp-load-plugins --dev
const {src, dest, parallel, series} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delete(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.pipe(dest('dist'))
}
//图片压缩
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 compile = parallel(style, script, page, image, font)
const build = series(clean, parallel(compile, extra))
module.exports={
compile,
build
}
热更新开发服务器
sudo yarn add browser-sync --dev
支持代码修改后自动热更新到浏览器中
只能修改dist文件夹下的文件浏览器发生修改
yarn gulp serve
const {src, dest, parallel, series} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.pipe(dest('dist'))
}
//图片压缩
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 = () =>{
//初始化相关配置
bs.init({
//提示
notify:false,
//端口号
// port:2080,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir: 'dist',
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page, image, font)
const build = series(clean, parallel(compile, extra))
module.exports={
compile,
build,
serve
}
监视变化以及构建优化
const {src, dest, parallel, series, watch} = require(‘gulp’)
const {src, dest, parallel, series, watch} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.pipe(dest('dist'))
}
//图片压缩
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 = () =>{
//文件路径,文件函数
//源代码修改后自动编译到dist中,同步到浏览器
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,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir:['dist', 'src', 'public'],
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page)
//上线之前执行任务
const build = series(clean, parallel(compile, extra, image, font))
const develop = series(compile, serve)
module.exports={
clean,
compile,
build,
serve,
develop
}
useref文件引用处理
//引用关系文件
//引入的node_modules下的从npm引入的一些插件,通过useref压缩合并转换成打包后的dist下的本地文件
sudo yarn add gulp-useref --dev
yarn gulp useref
const {src, dest, parallel, series, watch} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.pipe(dest('dist'))
}
//图片压缩
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 useref = () =>{
//把dist文件夹下的html文件做优化操作再转换入到dist文件夹下
return src('dist/*.html', {base:'dist'})
.pipe(plugins.useref({searchPath:['dist', '.']}))
.pipe(dest('dist'))
}
const serve = () =>{
//文件路径,文件函数
//源代码修改后自动编译到dist中,同步到浏览器
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,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir:['dist', 'src', 'public'],
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page)
//上线之前执行任务
const build = series(clean, parallel(compile, extra, image, font))
const develop = series(compile, serve)
module.exports={
clean,
compile,
build,
serve,
develop,
useref
}
文件压缩
sudo yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
sudo yarn add gulp-if --dev
yarn gulp compile
yarn gulp useref
const {src, dest, parallel, series, watch} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('dist'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('dist'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.pipe(dest('dist'))
}
//图片压缩
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'))
}
//sudo yarn add gulp-useref --dev
const useref = () =>{
//把dist文件夹下的html文件做优化操作再转换入到dist文件夹下
return src('dist/*.html', {base:'dist'})
.pipe(plugins.useref({searchPath:['dist', '.']}))
//html js css进行代码压缩
//sudo yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
//sudo 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
})))
.pipe(dest('release'))
}
const serve = () =>{
//文件路径,文件函数
//源代码修改后自动编译到dist中,同步到浏览器
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,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir:['dist', 'src', 'public'],
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page)
//上线之前执行任务
const build = series(clean, parallel(compile, extra, image, font))
const develop = series(compile, serve)
module.exports={
clean,
compile,
build,
serve,
develop,
useref
}
重新规划构建过程
开发时的编译目录为了避免和上线的构建目录区分,可以新增一个临时目录
const {src, dest, parallel, series, watch} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist', 'temp'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('temp'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('temp'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.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'))
}
//sudo yarn add gulp-useref --dev
const useref = () =>{
//把dist文件夹下的html文件做优化操作再转换入到dist文件夹下
return src('temp/*.html', {base:'temp'})
.pipe(plugins.useref({searchPath:['temp', '.']}))
//html js css进行代码压缩
//sudo yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
//sudo 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
})))
//为了避免文件冲突
.pipe(dest('release'))
}
const serve = () =>{
//文件路径,文件函数
//源代码修改后自动编译到dist中,同步到浏览器
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,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir:['temp', 'src', 'public'],
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page)
//上线之前执行任务
const build = series(clean, parallel(series(compile, useref), extra, image, font))
const develop = series(compile, serve)
module.exports={
clean,
compile,
build,
serve,
develop,
useref
}
补充
可以剔出部分导出的插件
将剩余的部分命令放入packag.json
直接使用
yarn clean
yarn build
yarn develop
"scripts": {
"clean": "gulp clean",
"build": "gulp build",
"develop": "gulp develop"
},
const {src, dest, parallel, series, watch} = require('gulp')
const delet = require('del')
const loadPlugins = require('gulp-load-plugins')
//自动创建一个开发服务器
const plugins = loadPlugins()
const browerSync = require('browser-sync')
const bs = browerSync.create()
// const sass = require('gulp-sass')
// const babel = require('gulp-babel')
// const swig = require('gulp-swig')
// const imagemin = require('gulp-imagemin')
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 delet(['dist', 'temp'])
}
const style = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/styles/*.scss', {base:'src'})
.pipe(plugins.sass())
.pipe(dest('temp'))
}
const script = ()=>{
//_开头的scss文件不会被编译
return src('src/assets/scripts/*.js', {base:'src'})
//.pipe(babel()) babel只是一个默认的转换平台,需要通过内部的插件转换成想要的代码
.pipe(plugins.babel({presets:['@babel/preset-env']}))
.pipe(dest('temp'))
}
const page = ()=>{
return src('src/*.html', {base:'src'})
//data作为动态数据转换进入html
.pipe(plugins.swig({data }))
.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'))
}
//sudo yarn add gulp-useref --dev
const useref = () =>{
//把dist文件夹下的html文件做优化操作再转换入到dist文件夹下
return src('temp/*.html', {base:'temp'})
.pipe(plugins.useref({searchPath:['temp', '.']}))
//html js css进行代码压缩
//sudo yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev
//sudo 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
})))
//为了避免文件冲突
.pipe(dest('release'))
}
const serve = () =>{
//文件路径,文件函数
//源代码修改后自动编译到dist中,同步到浏览器
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,
//是否启动点额时候打开浏览器
//browser-sync路径启动监听的通配符
files:'dist/**',
server: {
//网页根目录
baseDir:['temp', 'src', 'public'],
//特殊路由,将目标模版指定到对应的配置里去
routes:{
'/node_modules': 'node_modules'
}
}
})
}
const compile = parallel(style, script, page)
//上线之前执行任务
const build = series(clean, parallel(series(compile, useref), extra, image, font))
const develop = series(compile, serve)
module.exports={
clean,
build,
develop,
}
封装工作流-准备
安装脚手架模版
sudo yarn global add zce-cli
zce init nm zce-pages
git remote add https://github.com/xiaodashi12/cjg-pages.git
git add .
git commit -m ‘first commit’
新模块zce-pages
yarn
yarn link
老的模块
yarn link ‘zce-pages’
封装工作流-提取gulpfile
封装工作流-解决模块问题
封装工作流-抽象路径配置
封装工作流-包装gulp cli
封装工作流-发布并使用模块
封装工作流-总结
FIS
FIS的基本使用
sudo yarn global add fis3
fis3 release -d output
新增文件fis-conf.js
编译与压缩