-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
119 lines (108 loc) · 2.96 KB
/
gulpfile.babel.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
// create require() method
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const gulp = require('gulp');
import { deleteSync } from "del";
const ordered = require('ordered-read-streams');
// process css
const sass = require('gulp-sass')(require('sass'));
const purgecss = require('gulp-purgecss');
const autoprefixer = require('autoprefixer');
const postcss = require('gulp-postcss');
// process html
const htmlmin = require('gulp-html-minifier-terser');
const sriHash = require('gulp-sri-hash');
// static asset revisioning by appending content hash to filenames
import rev from 'gulp-rev';
import revRewrite from 'gulp-rev-rewrite';
/**
* Clean dist
*/
gulp.task('clean', (done) => {
deleteSync(['dist/**'], { force: true });
done();
});
/**
* Minify CSS
*/
gulp.task('pack-css', () => {
return gulp.src([
// main css
'src/assets/scss/main.scss'
])
// compile sass to css
.pipe(sass({ style: 'compressed' }))
// remove unused css
.pipe(purgecss({
content: [
'src/**/*.html',
// 'node_modules/bootstrap/dist/js/bootstrap.bundle.min.js'
],
variables: true
}))
// add vendor prefixes to rules by Can I Use
.pipe(postcss([
autoprefixer()
]))
.pipe(gulp.dest('dist/assets/css'));
});
/**
* Minify HTML
*/
gulp.task('pack-html', () => {
return ordered(
gulp.src("src/index.html")
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest('dist/')),
gulp.src("src/404.html")
.pipe(htmlmin({ collapseWhitespace: true }))
.pipe(gulp.dest('dist/')),
);
});
/**
* Copy assets
*/
gulp.task('copy-assets', () => {
return ordered(
gulp.src("src/robots.txt")
.pipe(gulp.dest("dist/")),
gulp.src(["src/assets/img/**"], {
encoding: false
})
.pipe(gulp.dest("dist/assets/img/"))
);
});
/**
* Version and rewrite assets by appending content hash to filenames
* Allows for automated cache busting
*/
function revision() {
return gulp.src('dist/**/*.css')
.pipe(rev())
.pipe(gulp.src('dist/**/*.html'))
.pipe(revRewrite())
.pipe(gulp.dest('dist'));
}
/**
* Generate SRI hashes
* Note: Do not modify contents of any referenced css- and js-files after the sriHash() task.
* Manipulating html files further is perfectly fine.
*/
gulp.task('sri-hash', () => {
return ordered(
gulp.src("dist/index.html")
.pipe(sriHash({
algo: "sha512"
}))
.pipe(gulp.dest('dist/')),
gulp.src("dist/404.html")
.pipe(sriHash({
algo: "sha512"
}))
.pipe(gulp.dest('dist/')),
);
});
/**
* Run everything
*/
gulp.task('default', gulp.series('clean', 'pack-css', 'pack-html', 'copy-assets', revision, 'sri-hash'));