forked from danurbanowicz/eleventy-netlify-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
206 lines (177 loc) · 5.81 KB
/
gulpfile.js
File metadata and controls
206 lines (177 loc) · 5.81 KB
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
'use strict';
// ------
// SETUP, CONFIG, IMPORTS AND GLOBALS
// ------
// Import the image handling config values from our custom config js file.
const { lfs, sizes, sizeNames, sourceDir } = require('./images.config');
// Require gulp core utils and all gulp plugins
const {dest, src, series, watch } = require('gulp');
const imagemin = require('gulp-imagemin');
const webp = require('gulp-webp');
const responsive = require('gulp-responsive');
const del = require('del');
const favicons = require("gulp-favicons");
const changed = require('gulp-changed');
const debug = require('gulp-debug');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
sass.compiler = require('node-sass');
// Require eleventy's metadata
const metadata = require ("./src/_data/metadata.json");
// Require Node.js utils
const fs = require('fs');
const path = require('path');
// ------
// HELPER FUNCTIONS
// ------
// Move a file
function moveFile(file, dir2) {
//gets file name and adds it to dir2
var f = path.basename(file);
var dest = path.resolve(dir2, f);
fs.rename(file, dest, (err)=>{
if(err) throw err;
else console.log('Successfully moved');
});
};
// ------
// GULP FUNCTIONS
// ------
// Image minification.
// Happens in-place (directly on src folder)
function minifyImages() {
return src('src/public/images/dist/**/*.{png,jpg,jpeg,webp}')
// .pipe(changed('src/public/images/dist'))
.pipe(debug({ title : 'Minify'}))
.pipe(imagemin())
.pipe(dest('src/public/images/dist'))
};
// Image conversion to webP
function createWebp() {
return src('src/public/images/dist/**/*.{png,jpg,jpeg}')
// .pipe(changed('src/public/images/dist'))
.pipe(debug({ title : 'webp'}))
.pipe(webp())
.pipe(dest('src/public/images/dist'))
};
// Favicon generation, based on a mandatory src/assets/img/favicon.jpg
function generatePwaFavicons() {
return src("src/assets/img/favicon.jpg")
.pipe(favicons({
appName: metadata.title,
appDescription: metadata.metatags.description,
developerName: metadata.author.name,
developerURL: metadata.author.github,
background: metadata.mobileColor,
path: "/assets/pwa",
url: metadata.url,
display: "standalone",
orientation: "portrait",
scope: "/",
start_url: "/?homescreen=1",
version: 1.0,
logging: false,
html: "favicons.html",
pipeHTML: true,
replace: true
}))
.pipe(dest("src/assets/pwa"))
}
// Moves the auto-generated favicons.html to the src templates folder
function moveFaviconHtml() {
return src('src/assets/pwa/favicons.html')
.pipe(dest('src/_includes/components'))
}
// Remove all the generated images. Only the originals will be left.
function cleanImages() {
return del([
'src/public/images/dist'
])
};
// Generate the gulp-responsive array of sizes based on the user-defined configuration
function generateGulpResponsiveConfiguration(){
// Loop the sizes array and create an object that fits the gulp-responsive reference
var responsiveImages = sizes.map(function(item){
var object = {
width: item.width,
rename: function(path) {
path.dirname += `/${item.name}`;
return path;
}
}
if(item.height) {
object.height = item.height;
}
return object;
});
// Loop the sizes array and create an object that fits the gulp-responsive reference
// and is a retina version of the former
var responsiveImages2x = sizes.map(function(item){
var object = {
width: item.width * 2,
rename: function(path) {
path.dirname += `/${item.name}`;
path.basename += '@2x';
return path;
}
}
if(item.height) {
object.height = item.height * 2;
}
return object;
});
return [ ...responsiveImages, ...responsiveImages2x ];
}
// Generate all the images.
// Beware: This can become a really expensive operation!
function resizeImages() {
return src('src/public/images/src/**/*.{png,jpg,jpeg}')
.pipe(dest('src/public/images/dist'))
.pipe(debug({ title : 'Resize'}))
// .pipe(changed('src/public/images/dist'))
.pipe(responsive(
{
'**/*.{png,jpg,webp}': generateGulpResponsiveConfiguration()
},
// Globals
{
withoutEnlargement: true,
skipOnEnlargement: true,
errorOnEnlargement: false, // Change this to allow to skip crops
quality: 85,
progressive: true,
withMetadata: false,
}
))
.pipe(dest('src/public/images/dist'));
}
// SCSS Compilation
function scss() {
return src('./src/assets/scss/**/*.scss')
.pipe(sass({
includePaths: ['node_modules']
})
.on('error', sass.logError))
.pipe(postcss([ autoprefixer() ]))
.pipe(dest('./src/assets/css'));
}
// ------
// GULP TASKS
// Define publicly available tasks
// ------
exports.cleanImages = cleanImages;
exports.resizeImages = resizeImages;
exports.minifyImages = minifyImages;
exports.createWebp = createWebp;
exports.generatePwaFavicons = series(generatePwaFavicons, moveFaviconHtml);
exports.scss = scss;
exports.scssWatch = function() {
watch('./src/assets/scss/**/*.scss', scss);
}
// Set the correct processImages task
if(lfs) {
exports.processImages = series(cleanImages, minifyImages);
} else {
exports.processImages = series(cleanImages,resizeImages, createWebp, minifyImages);
}