-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
108 lines (94 loc) · 2.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// gulp
const { src, dest, series, parallel, watch } = require('gulp');
// gulp plugins
const njk = require('gulp-nunjucks-render');
const minify = require('gulp-minifier');
const svgmin = require('gulp-svgmin');
// other
const fs = require('fs');
const path = require('path');
const { rimraf } = require('rimraf');
const liveServer = require('live-server');
const defaultPort = 8000;
const njkPaths = {
'templates': 'src/templates',
'blogPosts': 'src/templates/views/blog',
};
const gulpPaths = {
'views': { src: 'src/templates/views/**/*.njk', dst: 'public/' },
'static': { src: 'src/static/**/*', dst: 'public/' },
};
const buildTimestamp = {
locale: 'en-US',
options: {
year: 'numeric',
month: 'long',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
timeZoneName: 'short',
timeZone: 'America/New_York',
},
};
function clean() {
return rimraf('public/*', { glob: true });
}
// compile nunjucks view templates
function processViews() {
return src(gulpPaths['views'].src)
.pipe(njk({
path: [ njkPaths['templates'] ],
data: {
ctx: {
'build_timestamp':
new Date().toLocaleString(
buildTimestamp.locale,
buildTimestamp.options
),
'blog_posts':
fs.readdirSync(njkPaths['blogPosts'])
.map(x => path.parse(x).name)
.filter(x => x != 'index'),
},
},
}))
.pipe(minify({
minify: true,
minifyHTML: { collapseWhitespace: true, },
}))
.pipe(dest(gulpPaths['views'].dst));
}
function processStatic() {
return src(gulpPaths['static'].src)
.pipe(minify({
minify: true,
minifyCSS: { sourceMap: true },
minifyJS: { sourceMap: true },
}))
.pipe(dest(gulpPaths['static'].dst));
}
function watchAll() {
watch(njkPaths['templates'], processViews);
watch(gulpPaths['static'].src, processStatic);
}
function serve() {
liveServer.start({
root: 'public',
open: false,
});
}
exports.default = series(
clean,
parallel(
processViews,
processStatic,
),
);
exports.dev = series(
exports.default,
parallel(
serve,
watchAll,
),
);
exports.clean = clean;