javascript gulpfile.js在创建html邮件时使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript gulpfile.js在创建html邮件时使用相关的知识,希望对你有一定的参考价值。

var autoprefixer = require('autoprefixer')
var browserSync = require('browser-sync').create()
var csscomb = require('gulp-csscomb')
var eslint = require('gulp-eslint')
var gulp = require('gulp')
var htmlmin = require('gulp-htmlmin')
var juice = require('gulp-juice')
var mail = require('gulp-mail')
var notify = require('gulp-notify')
var plumber = require('gulp-plumber')
var postcss = require('gulp-postcss')
var rename = require('gulp-rename')

var errorHandler = {
  errorHandler: notify.onError('Error: <%= error.message %>')
}

var mailInfo = {
  to: [
    'sekine+to@rabi.co.jp'
  ],
  from: 'sekine+from@rabi.co.jp',
  smtp: {
    auth: {
      user: 'sekine@rabi.co.jp',
      pass: 'ihlbqmowwdkohzts'
    },
    host: 'smtp.gmail.com',
    secureConnection: true,
    port: 465
  }
}

gulp.task('default', ['css', 'js'])

gulp.task('css', () => {
  var plugins = [
    autoprefixer()
  ]
  return gulp.src('./public/style.css')
    .pipe(plumber(errorHandler))
    .pipe(postcss(plugins))
    .pipe(csscomb())
    .pipe(gulp.dest('./public/'))
})

gulp.task('js', () => {
  return gulp.src('./public/script.js')
    .pipe(plumber(errorHandler))
    .pipe(eslint({ fix: true }))
    .pipe(eslint.failAfterError())
    .pipe(gulp.dest('./public/'))
})

gulp.task('js-watch', ['js'], (done) => {
  browserSync.reload()
  done()
})

gulp.task('watch', ['css', 'js'], () => {
  browserSync.init({
    server: './public',
    startPath: '/'
  })

  gulp.watch('public/style.css', ['css'])
  gulp.watch('public/script.js', ['js-watch'])
  gulp.watch('public/index.html').on('change', browserSync.reload)
})

gulp.task('email:build', () => {
  return gulp.src('./email/index.html')
    .pipe(juice())
    .pipe(rename({
      suffix: '.build'
    }))
    .pipe(gulp.dest('./email'))
    .pipe(htmlmin({
      removeComments: true,
      collapseWhitespace: true
    }))
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(gulp.dest('./email'))
})

gulp.task('email:build-watch', ['email:build'], (done) => {
  browserSync.reload()
  done()
})

gulp.task('email:send', () => {
  return gulp.src('./email/index.build.min.html')
    .pipe(mail({
      subject: '[TEST] ' + new Date(),
      to: mailInfo.to,
      from: mailInfo.from,
      smtp: mailInfo.smtp
    }))
})

gulp.task('email:watch', ['email:build'], () => {
  browserSync.init({
    server: './email',
    startPath: '/index.build.min.html'
  })

  gulp.watch(['email/index.html', 'email/style.css'], ['email:build-watch'])
})

以上是关于javascript gulpfile.js在创建html邮件时使用的主要内容,如果未能解决你的问题,请参考以下文章

Javascript:在gulpfile.js中获取package.json数据

javascript Gulpfile.js(第4版)

javascript Gulp 4示例gulpfile.js

javascript gulpfile.js

javascript 各种hexo gulpfile.js收集

javascript 我-gulpfile.js