-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
82 lines (70 loc) · 2.05 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict";
const gulp = require('gulp'),
mjml = require('gulp-mjml'),
fileinclude = require('gulp-file-include'),
imagemin = require('gulp-imagemin'),
newer = require('gulp-newer'),
browsersync = require('browser-sync').create();
var mjmlEngine = require('mjml').default
var config = {
assetsDir: './assets',
distDir: './dist',
srcDir: './src'
};
function handleError (err) {
console.log(err.toString());
this.emit('end');
}
function mjml2html() {
return gulp
.src(config.srcDir + "/*.mjml")
.pipe(fileinclude({ prefix: "@@", basepath: "@file" }))
.on("error", handleError)
.pipe(mjml(mjmlEngine, { minify: true, validationLevel: "strict" }))
.on("error", handleError)
.pipe(gulp.dest(config.distDir))
.on("error", handleError)
.pipe(browsersync.stream());
}
// BrowserSync
function browserSync(done) {
browsersync.init({
server: {
baseDir: config.distDir
},
port: 3000
});
done();
}
// BrowserSync Reload
function browserSyncReload(done) {
browsersync.reload();
done();
}
function assets() {
gulp
.src([config.assetsDir + '/**/*'])
.pipe(newer(config.distDir + '/assets'))
.pipe(gulp.dest(config.distDir + '/assets'));
return gulp
.src(config.assetsDir + "/images/**/*")
.pipe(newer(config.distDir + "/assets/images"))
.pipe(
imagemin([
imagemin.mozjpeg({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
])
)
.pipe(gulp.dest(config.distDir + "/assets/images"));
}
function watchFiles() {
gulp.watch(config.srcDir + '/**/*.mjml', gulp.series(gulp.parallel(mjml2html, assets), browserSyncReload));
gulp.watch(config.srcDir + '/**/*.css', gulp.series(gulp.parallel(mjml2html, assets), browserSyncReload));
gulp.watch(config.assetsDir + '/images/**/*', assets);
}
const build = gulp.parallel(mjml2html, assets);
const watch = gulp.parallel(watchFiles, browserSync);
exports.assets = assets;
exports.build = build;
exports.watch = watch;
exports.default = watch;