前端工程化之自动化构建工具--GULP的配置文件gulpfile.js

const { src, dest, parallel, series, watch } = require('gulp')//src读取流,dest 写入流

const del = require('del')//是个promise方法,不是gulp的插件,
const browserSync = require('browser-sync')//不是gulp 的插件

const loadPlugins = require('gulp-load-plugins')

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' })
    .pipe(plugins.sass({ outputStyle: 'expanded' }))//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'] }))//@babel/preset-env  es6的新特性都可转换
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))//以流的方式向浏览器更新
}

const page = () => {
  return src('src/*.html', { base: 'src' })//src/**/*.html  ==>src下面所有子目录下的html文件
    .pipe(plugins.swig({ data, defaults: { cache: false } })) // data:模版需要转换的数据,defaults: { cache: false }防止模板缓存导致页面不能及时更新
    .pipe(dest('temp'))
    .pipe(bs.reload({ stream: true }))
}

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('监视文件路径的通配符',任务函数)
  watch('src/assets/scripts/*.js', script)
  watch('src/*.html', page)
  // watch('src/assets/images/**', image)//image、font、extra无损压缩,发布上线前构建一次即可,不用实时监听,减少开销
  // watch('src/assets/fonts/**', font)
  // watch('public/**', extra)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload)  //bs.reload自动更新浏览器

  //热更新开发服务器 yarn add brower-sync --dev
  bs.init({
    notify: false,//browserSync连接成功的提示
    port: 2080,//端口
    // open: false,//是否自动打开浏览器
    // files: 'dist/**',//监听改变的路径,也可以不用files,可以用bs.reload
    server: {
      baseDir: ['temp', 'src', 'public'],//根目录可以是字符串可以是数组,是数组的时候会依次查找,当temp中找不到的时候到src 中去找
      routes: {
        '/node_modules': 'node_modules' //优先于baseDir  请求前缀:目录的相对路径(相对根目录)
      }
    }
  })
}

//yarn add gulp-useref 使用的依赖
const useref = () => {
  return src('temp/*.html', { base: 'temp' })//找的是打包后的文件
    .pipe(plugins.useref({ searchPath: ['temp', '.'] })) //会找build:css endbuild这种构建注释 才会执行
    // html js css 压缩 读取流不同种类型文件,需要分别处理时,需要用 gulp-if的插件
    .pipe(plugins.if(/\.js$/, plugins.uglify())) //plugins.if(正则匹配, 需要工作的转换流)
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,//折叠掉空白字符
      minifyCSS: true,//html中的行内样式压缩
      minifyJS: true//html中的js压缩
      //还有删除注释,或者空属性的参数,可参考文档
    })))
    .pipe(dest('dist'))//temp dist 防止读写冲突 useref 处理的是编译后的文件如果也是读取流和写入流都是dist,可能会引起读写混乱 
}

const compile = parallel(style, script, page)

// 上线之前执行的任务  parallel 异步执行(并行)  series依次执行(串行)
const build =  series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  )
)

const develop = series(compile, serve) 

module.exports = {
  clean,
  build,
  develop
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值