Advertisement
Guest User

mail template gulpfile

a guest
May 9th, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by pkosinski on 03.03.2016.
  3.  */
  4. var gulp = require('gulp'),
  5.     sass = require('gulp-sass'),
  6.     del = require('del'),
  7.     runSequence = require('run-sequence'),
  8.     replace = require('gulp-replace'),
  9.     cache = require('gulp-cached'),
  10.     htmlmin = require('gulp-htmlmin'),
  11.     livereload = require('gulp-livereload'),
  12.     premailer = require('gulp-premailer'),
  13.     fileinclude = require('gulp-file-include');
  14.  
  15. var paths = {
  16.     src: {
  17.         sass: 'sample/sass/**/*.scss',
  18.         css: 'sample/css/**/*.css',
  19.         tmp: 'sample/tmp/**/*.html',
  20.         templates: 'sample/template/**/*.html',
  21.         preview: 'sample/preview/**/*.html',
  22.         partials: 'sample/partials/**/*.html'
  23.     },
  24.     dest: {
  25.         css: 'sample/styles/css',
  26.         tmp: 'sample/tmp',
  27.         preview: 'sample/preview'
  28.     }
  29. };
  30.  
  31. gulp.task('default', function () {
  32.     gulp.watch([paths.src.templates, paths.src.partials, paths.src.css], ['watch']);
  33.     gulp.watch(paths.src.sass, ['sass']);
  34.  
  35.     livereload.listen();
  36. });
  37.  
  38. gulp.task('sass', function () {
  39.     return gulp.src(paths.src.sass)
  40.         .pipe(sass())
  41.         .pipe(gulp.dest(paths.dest.css));
  42. });
  43.  
  44.  
  45. gulp.task('includePartials', function () {
  46.     var fileIncludeConf = {
  47.         basepath: '@root',
  48.         context: {
  49.             header: "'partials/header.html'",
  50.             wrapper: "780",
  51.             width: "690",
  52.             grid: "690",
  53.             col: "",
  54.             col2: "345",    // gird cols
  55.             col3: "230",
  56.             col4: "172",
  57.             col2img: "310",
  58.             col3img: "200",
  59.             col4img: "142",
  60.             btnClass: ""
  61.         }
  62.     };
  63.  
  64.     return gulp.src(paths.src.templates)
  65.         .pipe(fileinclude(fileIncludeConf)).on("error",errLog)
  66.         .pipe(gulp.dest(paths.dest.tmp));
  67. });
  68. gulp.task('includeNonPremailingPartials', function () {
  69.     var fileIncludeConf = {
  70.         basepath: '@root',
  71.         prefix: '##'
  72.     };
  73.  
  74.     return gulp.src(paths.src.tmp)
  75.         .pipe(fileinclude(fileIncludeConf))
  76.         .pipe(gulp.dest(paths.dest.tmp));
  77. });
  78.  
  79.  
  80. gulp.task('clean', function () {
  81.     cache.caches = {};
  82.     return del([paths.dest.preview, paths.dest.tmp]);
  83. });
  84.  
  85. gulp.task('premailer', function () {
  86.  
  87.     return gulp.src(paths.src.tmp)
  88.         .pipe(premailer({escape_url_attributes: false}))
  89.         .pipe(replace('%60', '`'))
  90.         .pipe(replace('%5B', '['))
  91.         .pipe(replace('%5D', ']'))
  92.         .pipe(gulp.dest(paths.dest.tmp));
  93. });
  94.  
  95. gulp.task('minify', function() {
  96.     // https://github.com/kangax/html-minifier
  97.     var htmlMinConfig = {
  98.         collapseWhitespace: true,
  99.         conservativeCollapse: true,
  100.         keepClosingSlash: true,
  101.         removeEmptyAttributes: true,
  102.         preventAttributesEscaping: true,
  103.         caseSensitive: true,
  104.         minifyCSS: true,
  105.         maxLineLength: 76
  106.     };
  107.  
  108.     return gulp.src(paths.src.tmp)
  109.         .pipe(htmlmin(htmlMinConfig)).on('error', errLog)
  110.         .pipe(gulp.dest(paths.dest.tmp));
  111. });
  112.  
  113. gulp.task('watch', function(cb) {
  114.     return runSequence('includePartials', 'premailer', 'includeNonPremailingPartials', 'minify', 'updatePreview', cb);
  115. });
  116.  
  117. gulp.task('updatePreview', function () {
  118.  
  119.     return gulp.src(paths.src.tmp)
  120.         .pipe(cache('preview'))
  121.         .pipe(gulp.dest(paths.dest.preview))
  122.         .pipe(livereload());
  123. });
  124.  
  125. /**
  126.  * Creates new mail template previews
  127.  */
  128. gulp.task('build', function(cb) {
  129.     return runSequence('clean', 'includePartials', 'premailer',
  130.         'includeNonPremailingPartials', 'minify', 'updatePreview', cb);
  131. });
  132.  
  133. function errLog(err) {
  134.     return console.error(err);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement