This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
forked from swapagarwal/swag-for-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
197 lines (170 loc) Β· 4.81 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const del = require('del');
const gulp = require('gulp');
const cachebust = require('gulp-rev');
const cacheClean = require('gulp-rev-delete-original');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify-es').default;
const pug = require('gulp-pug');
const htmlmin = require('gulp-htmlmin');
const stylus = require('gulp-stylus');
const webserver = require('gulp-webserver');
const concat = require('gulp-concat');
const download = require('gulp-download-stream');
const responsive = require('gulp-responsive');
const merge = require('merge-stream');
const {swagList, swagImages} = require('./get-data');
const PRODUCTION = process.env.NODE_ENV === 'production';
const REV_PATH = './dist/rev-manifest.json';
const RESIZE_OPTS = {
quality: 90,
progressive: true,
compressionLevel: 9,
errorOnEnlargement: false,
errorOnUnusedConfig: false
};
let manifest = {
'css/index.css': 'css/index.css',
'js/index.js': 'js/index.js'
};
gulp.task('pug', () => {
const tags = Array.from(swagList.reduce(
(tagList, {tags}) => {
tags.forEach(tag => tagList.add(tag));
return tagList;
},
new Set()
)).sort();
if (PRODUCTION) {
delete require.cache[require.resolve(REV_PATH)];
manifest = require(REV_PATH);
}
const bustedAssets = {
css: `/assets/${manifest['css/index.css']}`,
js: `/assets/${manifest['js/index.js']}`
};
return gulp.src('src/pug/*.pug')
.pipe(pug({
pretty: true,
locals: {swagList, tags, bustedAssets}
}))
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true
}))
.pipe(gulp.dest('dist/'));
});
gulp.task('styl', () => {
return gulp.src('src/styl/index.styl')
.pipe(stylus({compress: true}))
.pipe(gulp.dest('dist/assets/css'));
});
gulp.task('binaries', () => {
const paths = {
'src/img/*': 'dist/assets/img',
'src/fonts/*': 'dist/assets/fonts'
};
return merge(Object.entries(paths).map(([from, to]) =>
gulp.src(from).pipe(gulp.dest(to))
));
});
gulp.task('js', () => {
const presets = [
['@babel/env', {targets: {browsers: ['> 75%']}}]
];
return gulp.src('src/js/*.js')
.pipe(concat('index.js'))
.pipe(babel({presets}))
.pipe(uglify())
.pipe(gulp.dest('dist/assets/js'));
});
gulp.task('swag-img:clean', () => {
return del('dist/assets/swag-img/*');
});
gulp.task('swag-img:download', () => {
return download(swagImages)
.pipe(gulp.dest('dist/assets/swag-img'));
});
gulp.task('swag-img:optimize', cb => {
const jpegOutputConfig = {
name: '*.jpeg',
height: 300,
flatten: true
};
const webpOutputConfig = Object.assign({
rename: i => `${i.basename}.webp`
}, jpegOutputConfig);
return gulp.src('dist/assets/swag-img/*')
.pipe(responsive([jpegOutputConfig, webpOutputConfig], RESIZE_OPTS))
.pipe(gulp.dest('dist/assets/swag-img'))
.on('end', () => {
swagList.forEach(swag => {
const baseName = swag.image.split('.').slice(0, -1).join('.');
swag.images = {
jpeg: swag.image,
webp: `${baseName}.webp`
};
});
cb();
});
});
gulp.task('swag-img', gulp.series('swag-img:clean', 'swag-img:download', 'swag-img:optimize'));
gulp.task('clean:styl', () => del('dist/assets/css/*'));
gulp.task('clean:js', () => del('dist/assets/js/*'));
gulp.task('clean:binaries', () => del(['dist/assets/img/*', 'dist/assets/fonts/*']));
gulp.task('clean:assets', gulp.parallel('clean:styl', 'clean:js', 'clean:binaries'));
gulp.task('clean:pug', () => del('dist/index.html'));
gulp.task('clean', gulp.parallel('clean:pug', 'clean:assets'));
gulp.task('cachebust', cb => {
if (!PRODUCTION) {
return cb();
}
const basePath = 'dist/assets';
const bustedFiles = [
'dist/assets/css/*',
'dist/assets/js/*',
'dist/assets/swag-img/*'
];
return gulp.src(bustedFiles, {base: basePath})
.pipe(cachebust())
.pipe(cacheClean())
.pipe(gulp.dest(basePath))
.pipe(cachebust.manifest({
path: REV_PATH,
base: basePath
}))
.pipe(gulp.dest(basePath))
.on('end', () => {
delete require.cache[require.resolve(REV_PATH)];
manifest = require(REV_PATH);
swagList.forEach(swag => {
Object.entries(swag.images).forEach(([extension, fileName]) => {
fileName = `swag-img/${fileName.split('/').pop()}`;
if (!manifest[fileName]) {
console.warn(`Unable to find image ${fileName} in the manifest`);
return;
}
swag.images[extension] = `/assets/${manifest[fileName]}`;
});
});
cb();
});
});
gulp.task('webserver', () => {
return gulp.src('dist')
.pipe(webserver({
livereload: true,
open: true
}));
});
gulp.task('watch', () => {
gulp.watch('src/pug/**/*.pug', gulp.series('pug'));
gulp.watch('src/styl/**/*.styl', gulp.series('styl'));
gulp.watch('src/js/*.js', gulp.series('js'));
});
gulp.task('build', gulp.series(
'clean',
gulp.parallel(
gulp.series('swag-img', 'cachebust', 'pug'), 'styl', 'js', 'binaries'
)
));
gulp.task('default', gulp.series('build', 'webserver', 'watch'));