-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
126 lines (114 loc) · 3.25 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
const gulp = require('gulp');
const postcss = require('gulp-postcss');
const babel = require('gulp-babel');
const terser = require('gulp-terser');
const del = require('del');
const rev = require('gulp-rev');
const revRewrite = require('gulp-rev-rewrite');
const paths = require('vinyl-paths');
const workboxBuild = require('workbox-build');
const fs = require('fs');
const PUBLIC_PATH = 'docs';
// Styles
gulp.task('styles', () => {
return gulp
.src(`${PUBLIC_PATH}/style.css`)
.pipe(
postcss([
require('postcss-color-hex-alpha'),
require('autoprefixer')({ grid: 'autoplace' }),
require('postcss-csso'),
]),
)
.pipe(gulp.dest(PUBLIC_PATH));
});
// Scripts
gulp.task('scripts', () => {
return gulp
.src(`${PUBLIC_PATH}/*.js`)
.pipe(
babel({
presets: [
[
'@babel/preset-env',
{
targets: {
esmodules: true,
},
},
],
],
}),
)
.pipe(terser())
.pipe(gulp.dest(PUBLIC_PATH));
});
// Cache
gulp.task('cache:hash', () => {
return gulp
.src(
[
`${PUBLIC_PATH}/*.js`,
`${PUBLIC_PATH}/style.css`,
`${PUBLIC_PATH}/favicon/**/*.{svg,png}`,
`${PUBLIC_PATH}/manifest.webmanifest`,
],
{
base: PUBLIC_PATH,
},
)
.pipe(paths(del))
.pipe(rev())
.pipe(gulp.dest(PUBLIC_PATH))
.pipe(rev.manifest('rev.json'))
.pipe(gulp.dest(PUBLIC_PATH));
});
gulp.task('cache:replace', () => {
return gulp
.src([
`${PUBLIC_PATH}/**/*.{html,css}`,
`${PUBLIC_PATH}/manifest-*.webmanifest`,
])
.pipe(
revRewrite({
manifest: fs.readFileSync(`${PUBLIC_PATH}/rev.json`),
}),
)
.pipe(gulp.dest(PUBLIC_PATH));
});
gulp.task('service-worker', () => {
return workboxBuild.generateSW({
globDirectory: PUBLIC_PATH,
globPatterns: ['**/*.{js,css,webmanifest,ico,woff2}'],
swDest: `${PUBLIC_PATH}/sw.js`,
runtimeCaching: [
{
urlPattern: /\.(?:png|jpg|avif|svg|ico)$/,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 30,
},
},
},
{
urlPattern: /(\.(?:html))|(\/)$/,
handler: 'NetworkFirst',
options: {
cacheName: 'pages',
expiration: {
maxEntries: 20,
},
},
},
],
mode: 'production',
skipWaiting: true,
clientsClaim: true,
sourcemap: false,
});
});
gulp.task('cache', gulp.series('cache:hash', 'cache:replace'));
// Build
gulp.task('build', gulp.series('styles', 'scripts', 'cache', 'service-worker'));