-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.js
340 lines (292 loc) · 9.76 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// GULP
//––––––––––––––––––––––––––––––––––––––––––––––––––
// General.
import gulp from "gulp";
const { dest, src, series, watch } = gulp;
import * as dartSass from "sass";
import gulpSass from "gulp-sass";
const sass = gulpSass(dartSass);
import { exec } from "child_process";
import { stream as critical } from "critical";
import autoprefixer from "gulp-autoprefixer";
import through2 from "through2";
import { deleteAsync } from "del";
// Images.
import sharp from "sharp";
import fs from "fs/promises";
import path from "path";
// Data.
import hashCSS from "./data/css/hash.json" assert { type: "json" };
// Rollup
import { rollup } from "rollup";
import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import terser from "@rollup/plugin-terser";
import rollupSourcemaps from "rollup-plugin-sourcemaps";
import { createHash } from "crypto";
//
// SCSS
//––––––––––––––––––––––––––––––––––––––––––––––––––
/**
* Compile SCSS files to CSS
* This task:
* 1. Clears the static/css directory (preserving critical CSS)
* 2. Compiles and minifies SCSS files
* 3. Adds vendor prefixes
* 4. Hashes filenames based on content
* 5. Writes a manifest file with filename mappings
*/
async function compileScss() {
// Clear the static/css directory excluding critical CSS.
await deleteAsync(["static/css/*.css", "!static/css/critical/**/*"]);
// Initialize an empty object to store the filename mappings
const manifest = {};
return (
src("src/scss/**/*.scss")
// Compile SCSS to minified CSS
.pipe(sass.sync({ outputStyle: "compressed" }).on("error", sass.logError))
// Add vendor prefixes to CSS properties
.pipe(autoprefixer())
// Custom transform to hash filenames
.pipe(
through2.obj(function (file, enc, cb) {
if (file.isBuffer()) {
const hash = createHash("md5")
.update(file.contents)
.digest("hex")
.slice(0, 8);
// Get the original filename
const originalName = path.basename(file.path);
// Create a new filename with the hash
const hashedName = `${path.basename(
file.path,
".css"
)}.${hash}.css`;
// Update the file's path with the new hashed filename
file.path = path.join(path.dirname(file.path), hashedName);
// Add the filename mapping to the manifest
manifest[originalName] = hashedName;
}
cb(null, file);
})
)
// Write the processed and renamed files to the destination
.pipe(dest("static/css"))
// After all files have been processed
.on("end", async () => {
// Write the manifest file with filename mappings
await fs.writeFile(
"data/css/hash.json",
JSON.stringify(manifest, null, 2)
);
})
);
}
//
// JAVASCRIPT
//––––––––––––––––––––––––––––––––––––––––––––––––––
async function compileJs() {
// Clear the static/js directory
await deleteAsync(["static/js/**/*"]);
const dev = process.argv.includes("--dev");
const bundle = await rollup({
input: "src/js/main.js", // Main entry point
plugins: [
resolve(),
commonjs(),
dev && rollupSourcemaps(),
!dev && terser(),
].filter(Boolean),
});
const { output } = await bundle.generate({
file: "static/js/bundle.js",
format: "es", // Use ES module format
sourcemap: dev,
});
const { code, map } = output[0];
// Generate hash
const hash = createHash("md5").update(code).digest("hex").slice(0, 8);
const fileName = `main.${hash}.min.js`;
// Write bundle
await fs.mkdir("static/js", { recursive: true });
await fs.writeFile(`static/js/${fileName}`, code);
if (dev) {
await fs.writeFile(`static/js/${fileName}.map`, map.toString());
}
// Update hash manifest
const manifest = { "main.min.js": fileName };
await fs.writeFile("data/js/hash.json", JSON.stringify(manifest, null, 2));
// Copy and minify vendor JS files.
return src("src/js/vendor/*.js").pipe(dest("static/js/vendor"));
}
//
// IMAGES
//––––––––––––––––––––––––––––––––––––––––––––––––––
async function images() {
// Clear the /static/img/uploads/ directory.
await deleteAsync(["static/img/uploads/**/*"]);
// Process images
const srcDir = "src/img/uploads";
const destDir = "static/img/uploads";
const files = await fs.readdir(srcDir);
for (const file of files) {
// Only look for jpg and png.
if (file.match(/\.(jpg|jpeg|png)$/i)) {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);
try {
// Create low quality image placeholders of all JPG and PNG images
await sharp(srcPath)
.jpeg({ quality: 1, mozjpeg: true })
.toFile(path.join(destDir, `${path.parse(file).name}-lqip.jpg`));
// Featured images
if (file.startsWith("featured-image-")) {
const metadata = await sharp(srcPath).metadata();
await sharp(srcPath)
.resize({ width: Math.floor(metadata.width / 2) })
.jpeg({ quality: 60 })
.toFile(
path.join(
destDir,
`${path.parse(file).name}-sm${path.parse(file).ext}`
)
);
}
// Compress JPGs without scaling
// https://sharp.pixelplumbing.com/api-output#jpeg
if (file.endsWith(".jpg")) {
// await fs.copyFile(srcPath, destPath);
await sharp(srcPath).jpeg({ quality: 60 }).toFile(destPath);
// Compress PNGs without scaling
// https://sharp.pixelplumbing.com/api-output#png
} else if (file.endsWith(".png")) {
await sharp(srcPath).png({ quality: 60 }).toFile(destPath);
}
} catch (error) {
console.error(`Error processing ${file}:`, error);
}
}
}
}
//
// CRITICAL CSS
//––––––––––––––––––––––––––––––––––––––––––––––––––
// Generate & Inline Critical-path CSS
function criticalCss(cb) {
var path = "static/css/";
// Build CSS filename paths.
var main = path + hashCSS["main.css"];
var article = path + hashCSS["single-article.css"];
var contact = path + hashCSS["contact.css"];
var about = path + hashCSS["about.css"];
var photography = path + hashCSS["photography.css"];
var singlePhotography = path + hashCSS["single-photography.css"];
exec("hugo", function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
// How long to wait for Hugo to build.
const buildWait = 1000;
// Wait until Hugo has finished building.
setTimeout(function () {
// Home page / article listing.
src("public/articles/index.html")
.pipe(
critical({
base: "/",
css: [main],
width: 1200,
height: 900,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/"));
// Single article.
src("public/articles/a-better-nba-box-score/index.html")
.pipe(
critical({
base: "/",
css: [main, article],
width: 1200,
height: 600,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/articles/"));
// Contact page.
src("public/contact/index.html")
.pipe(
critical({
base: "/",
css: [main, contact],
width: 1200,
height: 1200,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/contact/"));
// About page.
src("public/about/index.html")
.pipe(
critical({
base: "/",
css: [main, about],
width: 1200,
height: 1200,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/about/"));
// Photography index.
src("public/photography/index.html")
.pipe(
critical({
base: "/",
css: [main, photography],
width: 1200,
height: 1200,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/photography/"));
// Single photography.
src("public/photography/brunswick-wall-flowers/index.html")
.pipe(
critical({
base: "/",
css: [main, singlePhotography],
width: 1200,
height: 1200,
})
)
.on("error", function (err) {
console.log(err.message);
})
.pipe(dest("static/css/critical/single-photography/"));
}, buildWait);
}
//
// WATCH
//––––––––––––––––––––––––––––––––––––––––––––––––––
function watchFiles() {
watch("src/scss/**/*", compileScss);
watch("src/js/**/*", compileJs);
watch("src/img/uploads/**/*", images);
}
//
// EXPORTS
//––––––––––––––––––––––––––––––––––––––––––––––––––
export { compileScss, compileJs, images, criticalCss, watchFiles };
export default series(compileJs, compileScss);