-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
184 lines (163 loc) · 4.91 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
require('dotenv').config();
const gulp = require('gulp');
const postcss = require('gulp-postcss');
// const swc = require('gulp-swc');
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 replace = require('gulp-replace');
const execSync = require('child_process').execSync;
const fs = require('fs');
const PUBLIC_PATH = '_public';
const SRC_PATH = 'src';
const tty = process.platform === 'win32' ? 'CON' : '/dev/tty';
// Styles
gulp.task('styles', () => {
return gulp
.src(`${PUBLIC_PATH}/styles/{styles,dark}.css`)
.pipe(
postcss([
require('postcss-import'),
require('postcss-lightningcss'),
]),
)
.pipe(gulp.dest(`${PUBLIC_PATH}/styles`));
});
// Scripts
// gulp.task('scripts', () => {
// return gulp
// .src(`${PUBLIC_PATH}/scripts/*.js`)
// .pipe(
// swc({
// minify: true,
// }),
// )
// .pipe(gulp.dest(`${PUBLIC_PATH}/scripts`));
// });
// Clean
gulp.task('clean', () => {
return del([
`${PUBLIC_PATH}/styles/**/*`,
`!${PUBLIC_PATH}/styles/{styles,dark}.css`,
// `${PUBLIC_PATH}/scripts/**/*`,
// `!${PUBLIC_PATH}/scripts/scripts.js`,
]);
});
// Cache
gulp.task('cache:hash', () => {
return gulp
.src(
[
`${PUBLIC_PATH}/fonts/*.woff2`,
`${PUBLIC_PATH}/images/**/*.{svg,png,jpg,avif}`,
// `${PUBLIC_PATH}/scripts/*.js`,
`${PUBLIC_PATH}/styles/*.css`,
`${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}', '**/404.html'],
swDest: `${PUBLIC_PATH}/sw.js`,
runtimeCaching: [
{
urlPattern: /\.(?:png|jpg|avif|svg)$/,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 30,
},
},
},
{
urlPattern: /(\.(?:html))|(\/)$/,
handler: 'NetworkFirst',
options: {
cacheName: 'articles',
expiration: {
maxEntries: 20,
},
},
},
],
mode: 'production',
skipWaiting: true,
clientsClaim: true,
sourcemap: false,
});
});
gulp.task('cache', gulp.series('cache:hash', 'cache:replace'));
gulp.task('contributors:get', () => {
// Get new contributors only on local build
if (process.env.ELEVENTY_ENV === 'production') {
return new Promise((resolve) => resolve());
}
const contributors = execSync('git shortlog -sne < ' + tty).toString();
const myEmails = [
];
const contributorsNames = contributors
.split('\n')
.filter(Boolean)
.filter((line) => !myEmails.some((myEmail) => line.includes(myEmail)))
.map((contributorLine) => {
const info = contributorLine.split('\t')[1];
const name = info.split(' <')[0];
return name;
});
return new Promise((resolve) => {
fs.writeFileSync(
`${SRC_PATH}/data/contributors.json`,
JSON.stringify(contributorsNames),
);
resolve();
});
});
gulp.task('humans:generate', () => {
const contributors = JSON.parse(
fs.readFileSync(`${SRC_PATH}/data/contributors.json`),
);
const date = new Date();
return gulp
.src(`${PUBLIC_PATH}/humans.txt`)
.pipe(replace('{LAST_UPDATE}', date.toISOString()))
.pipe(replace('{CONTRIBUTORS}', contributors.join('\n\t')))
.pipe(gulp.dest(PUBLIC_PATH));
});
// Build
gulp.task(
'build',
gulp.parallel(
gulp.series('styles', 'clean', 'cache', 'service-worker'),
gulp.series('contributors:get', 'humans:generate'),
),
);