-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
83 lines (70 loc) · 2.52 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
83
const config = require('./config');
const gulp = require('gulp');
const argv = require('yargs').argv;
const mjml = require('gulp-mjml');
const i18n = require('gulp-html-i18n');
const replace = require('gulp-replace');
const environments = require('gulp-environments');
const connect = require('gulp-connect');
const nunjucks = require('gulp-nunjucks');
const I18N_REGEX = /\_\(([\w-\.+]+)\)/g; // used to find placeholder for i18n keys in templates
const production = environments.production;
const mjmlEngine = require('mjml');
// should never end with a slash
const imageBase = production() ? config.imageBase : './assets';
const argOutput = argv.out || './output';
const dirs = {
output: argOutput ,
output_assets: argOutput + '/assets',
locales: './src/locales',
templates: './src/templates/html',
templates_text: './src/templates/text',
assets: './src/assets'
};
const files = {
templates: dirs.templates + '/*.mjml',
templates_all: dirs.templates + '/**/*.mjml',
templates_text: dirs.templates_text + '/*.txt',
templates_text_all: dirs.templates_text + '/**/*.txt',
assets: dirs.assets + '/**/*',
locales: dirs.locales + '/**/*.yaml'
};
gulp.task('copy:assets', function() {
return gulp.src(files.assets)
.pipe(gulp.dest(dirs.output_assets))
});
gulp.task('build:html', function() {
return gulp.src(files.templates)
.pipe(nunjucks.compile({}, {searchPath: dirs.templates}))
.pipe(replace('$IMGBASE$', imageBase)) // could be replaced with Nunjucks vars
.pipe(mjml(mjmlEngine))
.pipe(i18n({
langDir: dirs.locales,
langRegExp: I18N_REGEX,
}))
.pipe(gulp.dest(dirs.output))
.pipe(connect.reload());
});
gulp.task('build:text', function() {
return gulp.src(files.templates_text)
.pipe(nunjucks.compile({}, {searchPath: dirs.templates_text}))
.pipe(i18n({
langDir: dirs.locales,
langRegExp: I18N_REGEX,
}))
.pipe(gulp.dest(dirs.output))
.pipe(connect.reload());
});
gulp.task('build', ['copy:assets', 'build:text', 'build:html']);
gulp.task('watch', ['build'], function() {
gulp.watch([files.templates_all, files.assets, files.locales], ['build:html']);
gulp.watch([files.templates_text_all, files.locales], ['build:text']);
});
gulp.task('server', ['watch'], function(event) {
connect.server({
root: dirs.output,
port: 1980,
livereload: true
});
});
gulp.task('default', ['server']);