This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
gulpfile.js
128 lines (109 loc) · 3.94 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
const gulp = require('gulp');
const plugins = require('gulp-load-plugins')();
const del = require('del');
const dirs = {
dist: 'dist',
compresseable: 'dist/**/*.{css,html,ico,js,svg,txt,xml,webmanifest}',
src: 'src'
};
// ---------------------------------------------------------------------
// | Helper tasks |
// ---------------------------------------------------------------------
gulp.task('clean', () => {
return del(dirs.dist);
});
// Copies all the files that are not taken care but optimizations into `dist`
gulp.task('copy:others', () => {
return gulp.src(`${dirs.src}/**/*.ico`)
.pipe(gulp.dest(`${dirs.dist}`));
});
// Optimizes JS using uglify saving the results into `dist`
gulp.task('optimize:js', () => {
return gulp.src(`${dirs.src}/**/*.js`)
//.pipe(plugins.uglify())
.pipe(gulp.dest(dirs.dist));
});
// Optimizes CSS using clean css saving the results into `dist`
gulp.task('optimize:css', () => {
return gulp.src(`${dirs.src}/**/*.css`)
.pipe(plugins.cleanCss())
.pipe(gulp.dest(dirs.dist));
});
// Cache busts the static files in `dist` deleting the source file (also from `dist`)
gulp.task('revfiles', () => {
return gulp.src([`${dirs.dist}/{images,styles,scripts}/**/*`])
.pipe(plugins.rev())
.pipe(plugins.revDeleteOriginal())
.pipe(gulp.dest(`${dirs.dist}`))
.pipe(plugins.rev.manifest())
.pipe(gulp.dest(`${dirs.dist}`));
});
// Updates all the references to static assets using the manifest created in `revfiles`
gulp.task('revreplace', () => {
const manifest = gulp.src(`${dirs.dist}/rev-manifest.json`);
return gulp.src(`${dirs.dist}/**/*`)
.pipe(plugins.revReplace({
manifest
}))
.pipe(gulp.dest(dirs.dist));
});
// Optimizes HTML files using htmlmin saving the results into `dist`
gulp.task('optimize:html', () => {
const htmlminOptions = {
caseSensitive: true,
collapseBooleanAttributes: false,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
preserveLineBreak: true,
removeAttributeQuotes: false,
removeComments: true,
removeCommentsFromCDATA: false,
removeEmptyAttributes: false,
removeOptionalTags: false,
removeRedundantAttributes: false
};
return gulp.src(`${dirs.src}/**/*.html`)
.pipe(plugins.htmlmin(htmlminOptions))
.pipe(gulp.dest(dirs.dist));
});
// Optimizes images using imagemin and saving into `dist`
gulp.task('imagemin', () => {
return gulp.src(`${dirs.src}/**/*.{gif,ico,jpg,png,svg}`)
.pipe(plugins.imagemin())
.pipe(gulp.dest(dirs.dist));
});
// Adds the `sri` attribute for CSS and JS of all HTML files in `dist`
gulp.task('sri', () => {
return gulp.src(`${dirs.dist}/**/*.html`)
.pipe(plugins.sriHash())
.pipe(gulp.dest(dirs.dist));
});
// Compresses all the compreseable files in `dist` using zopfli
gulp.task('compress:zopfli', () => {
return gulp.src(dirs.compresseable)
.pipe(plugins.zopfli())
.pipe(gulp.dest(dirs.dist));
});
// Compresses all the compreseable files in `dist` using Brotli
gulp.task('compress:brotli', () => {
return gulp.src(dirs.compresseable)
.pipe(plugins.brotli.compress())
.pipe(gulp.dest(dirs.dist));
});
// ---------------------------------------------------------------------
// | Main tasks |
// ---------------------------------------------------------------------
gulp.task('build', gulp.series(
'clean',
'optimize:js',
'optimize:css',
'imagemin',
'optimize:html',
'revfiles',
'revreplace',
'sri',
'compress:zopfli',
'compress:brotli'
));
gulp.task('default', gulp.series('build'));