|
| 1 | +const { src, dest } = require("gulp"); |
| 2 | +const sharpResponsive = require("gulp-sharp-responsive"); |
| 3 | +const imagemin = require("gulp-imagemin"); |
| 4 | + |
| 5 | +const compressProjectImages = async () => { |
| 6 | + await src("src/assets/images/projectImages/*.{png,jpg}") |
| 7 | + .pipe( |
| 8 | + sharpResponsive({ |
| 9 | + formats: [ |
| 10 | + // jpeg |
| 11 | + { width: 256, format: "jpeg", rename: { suffix: "-256" } }, |
| 12 | + { width: 512, format: "jpeg", rename: { suffix: "-512" } }, |
| 13 | + { width: 1024, format: "jpeg", rename: { suffix: "-1024" } }, |
| 14 | + // webp |
| 15 | + { width: 256, format: "webp", rename: { suffix: "-256" } }, |
| 16 | + { width: 512, format: "webp", rename: { suffix: "-512" } }, |
| 17 | + { width: 1024, format: "webp", rename: { suffix: "-1024" } }, |
| 18 | + // avif |
| 19 | + { width: 256, format: "avif", rename: { suffix: "-256" } }, |
| 20 | + { width: 512, format: "avif", rename: { suffix: "-512" } }, |
| 21 | + { width: 1024, format: "avif", rename: { suffix: "-1024" } }, |
| 22 | + ], |
| 23 | + }) |
| 24 | + ) |
| 25 | + .pipe(dest("src/assets/images/projectImages/compressed")); |
| 26 | + await src("src/assets/images/projectImages/compressed/*.{webp,avif,jpg}") |
| 27 | + .pipe(imagemin()) |
| 28 | + .pipe(dest("src/assets/images/projectImages/compressed/")); |
| 29 | +}; |
| 30 | +const compressAuthorImages = async () => { |
| 31 | + await src("src/assets/images/authorImages/*.{png,jpg}") |
| 32 | + .pipe( |
| 33 | + sharpResponsive({ |
| 34 | + formats: [ |
| 35 | + // jpeg |
| 36 | + { width: 256, format: "jpeg", rename: { suffix: "-256" } }, |
| 37 | + { width: 512, format: "jpeg", rename: { suffix: "-512" } }, |
| 38 | + // webp |
| 39 | + { width: 256, format: "webp", rename: { suffix: "-256" } }, |
| 40 | + { width: 512, format: "webp", rename: { suffix: "-512" } }, |
| 41 | + // avif |
| 42 | + { width: 256, format: "avif", rename: { suffix: "-256" } }, |
| 43 | + { width: 512, format: "avif", rename: { suffix: "-512" } }, |
| 44 | + ], |
| 45 | + }) |
| 46 | + ) |
| 47 | + .pipe(dest("src/assets/images/authorImages/compressed")); |
| 48 | + await src("src/assets/images/authorImages/compressed/*.{webp,avif,jpg}") |
| 49 | + .pipe(imagemin()) |
| 50 | + .pipe(dest("src/assets/images/authorImages/compressed/")); |
| 51 | +}; |
| 52 | +const compressPostImages = async () => { |
| 53 | + await src("src/assets/images/postImages/*.{png,jpg}") |
| 54 | + .pipe( |
| 55 | + sharpResponsive({ |
| 56 | + formats: [ |
| 57 | + // jpeg |
| 58 | + { width: 256, format: "jpeg", rename: { suffix: "-256" } }, |
| 59 | + { width: 512, format: "jpeg", rename: { suffix: "-512" } }, |
| 60 | + { width: 1024, format: "jpeg", rename: { suffix: "-1024" } }, |
| 61 | + { width: 1600, format: "jpeg", rename: { suffix: "-1600" } }, |
| 62 | + // webp |
| 63 | + { width: 256, format: "webp", rename: { suffix: "-256" } }, |
| 64 | + { width: 512, format: "webp", rename: { suffix: "-512" } }, |
| 65 | + { width: 1024, format: "webp", rename: { suffix: "-1024" } }, |
| 66 | + { width: 1600, format: "webp", rename: { suffix: "-1600" } }, |
| 67 | + // avif |
| 68 | + { width: 256, format: "avif", rename: { suffix: "-256" } }, |
| 69 | + { width: 512, format: "avif", rename: { suffix: "-512" } }, |
| 70 | + { width: 1024, format: "avif", rename: { suffix: "-1024" } }, |
| 71 | + { width: 1600, format: "avif", rename: { suffix: "-1600" } }, |
| 72 | + ], |
| 73 | + }) |
| 74 | + ) |
| 75 | + .pipe(dest("src/assets/images/postImages/compressed")); |
| 76 | + |
| 77 | + await src("src/assets/images/postImages/compressed/*.{webp,avif,jpg}") |
| 78 | + .pipe(imagemin()) |
| 79 | + .pipe(dest("src/assets/images/postImages/compressed/")); |
| 80 | +}; |
| 81 | + |
| 82 | +// compressAllImages = () => |
| 83 | +// src("src/assets/images/**.{png,jpg}") |
| 84 | +// .pipe(imagemin()) |
| 85 | +// .pipe(dest("src/assets/images/minified")); |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + compressProjectImages, |
| 89 | + compressAuthorImages, |
| 90 | + compressPostImages, |
| 91 | +}; |
0 commit comments