-
Notifications
You must be signed in to change notification settings - Fork 69
/
gulpfile.js
236 lines (212 loc) · 6.73 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
/**
* Gulp with TailwindCSS - An CSS Utility framework build setup with SCSS
* Author : Manjunath G
* URL : manjumjn.com | lazymozek.com
* Twitter : twitter.com/manju_mjn
**/
/*
Usage:
1. npm install //To install all dev dependencies of package
2. npm run dev //To start development and server for live preview
3. npm run prod //To generate minifed files for live server
*/
const { src, dest, watch, series, parallel } = require("gulp");
const clean = require("gulp-clean"); //For Cleaning build/dist for fresh export
const options = require("./config"); //paths and other options from config.js
const browserSync = require("browser-sync").create();
const sass = require("gulp-sass")(require("sass")); //For Compiling SASS files
const postcss = require("gulp-postcss"); //For Compiling tailwind utilities with tailwind config
const concat = require("gulp-concat"); //For Concatinating js,css files
const uglify = require("gulp-terser"); //To Minify JS files
const imagemin = require("gulp-imagemin"); //To Optimize Images
const mozjpeg = require("imagemin-mozjpeg"); // imagemin plugin
const pngquant = require("imagemin-pngquant"); // imagemin plugin
const purgecss = require("gulp-purgecss"); // Remove Unused CSS from Styles
const logSymbols = require("log-symbols"); //For Symbolic Console logs :) :P
const includePartials = require("gulp-file-include"); //For supporting partials if required
//Load Previews on Browser on dev
function livePreview(done) {
browserSync.init({
server: {
baseDir: options.paths.dist.base,
},
port: options.config.port || 5000,
});
done();
}
// Triggers Browser reload
function previewReload(done) {
console.log("\n\t" + logSymbols.info, "Reloading Browser Preview.\n");
browserSync.reload();
done();
}
//Development Tasks
function devHTML() {
return src(`${options.paths.src.base}/**/*.html`)
.pipe(includePartials())
.pipe(dest(options.paths.dist.base));
}
function devStyles() {
const tailwindcss = require("tailwindcss");
const autoprefixer = require("autoprefixer");
return src(`${options.paths.src.css}/**/*.scss`)
.pipe(sass().on("error", sass.logError))
.pipe(postcss([tailwindcss(options.config.tailwindjs), autoprefixer()]))
.pipe(concat({ path: "style.css" }))
.pipe(dest(options.paths.dist.css));
}
function devScripts() {
return src([
`${options.paths.src.js}/libs/**/*.js`,
`${options.paths.src.js}/**/*.js`,
`!${options.paths.src.js}/**/external/*`,
])
.pipe(concat({ path: "scripts.js" }))
.pipe(dest(options.paths.dist.js));
}
function devImages() {
return src(`${options.paths.src.img}/**/*`).pipe(
dest(options.paths.dist.img)
);
}
function devFonts() {
return src(`${options.paths.src.fonts}/**/*`).pipe(
dest(options.paths.dist.fonts)
);
}
function devThirdParty() {
return src(`${options.paths.src.thirdParty}/**/*`).pipe(
dest(options.paths.dist.thirdParty)
);
}
function watchFiles() {
watch(
`${options.paths.src.base}/**/*.{html,php}`,
series(devHTML, devStyles, previewReload)
);
watch(
[options.config.tailwindjs, `${options.paths.src.css}/**/*.scss`],
series(devStyles, previewReload)
);
watch(`${options.paths.src.js}/**/*.js`, series(devScripts, previewReload));
watch(`${options.paths.src.img}/**/*`, series(devImages, previewReload));
watch(`${options.paths.src.fonts}/**/*`, series(devFonts, previewReload));
watch(
`${options.paths.src.thirdParty}/**/*`,
series(devThirdParty, previewReload)
);
console.log("\n\t" + logSymbols.info, "Watching for Changes..\n");
}
function devClean() {
console.log(
"\n\t" + logSymbols.info,
"Cleaning dist folder for fresh start.\n"
);
return src(options.paths.dist.base, { read: false, allowEmpty: true }).pipe(
clean()
);
}
//Production Tasks (Optimized Build for Live/Production Sites)
function prodHTML() {
return src(`${options.paths.src.base}/**/*.{html,php}`)
.pipe(includePartials())
.pipe(dest(options.paths.build.base));
}
function prodStyles() {
const tailwindcss = require("tailwindcss");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
return src(`${options.paths.src.css}/**/*.scss`)
.pipe(sass().on("error", sass.logError))
.pipe(
postcss([
tailwindcss(options.config.tailwindjs),
autoprefixer(),
cssnano(),
])
)
// .pipe(
// purgecss({
// ...options.config.purgecss,
// defaultExtractor: (content) => {
// // without arbitray selectors
// // const v2Regex = /[\w-:./]+(?<!:)/g;
// // with arbitray selectors
// const v3Regex = /[(\([&*\])|\w)-:./]+(?<!:)/g;
// const broadMatches = content.match(v3Regex) || [];
// const innerMatches =
// content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || [];
// return broadMatches.concat(innerMatches);
// },
// })
// )
.pipe(dest(options.paths.build.css));
}
function prodScripts() {
return src([
`${options.paths.src.js}/libs/**/*.js`,
`${options.paths.src.js}/**/*.js`,
])
.pipe(concat({ path: "scripts.js" }))
.pipe(uglify())
.pipe(dest(options.paths.build.js));
}
function prodImages() {
const pngQuality = Array.isArray(options.config.imagemin.png)
? options.config.imagemin.png
: [0.7, 0.7];
const jpgQuality = Number.isInteger(options.config.imagemin.jpeg)
? options.config.imagemin.jpeg
: 70;
const plugins = [
pngquant({ quality: pngQuality }),
mozjpeg({ quality: jpgQuality }),
];
return src(options.paths.src.img + "/**/*")
.pipe(imagemin([...plugins]))
.pipe(dest(options.paths.build.img));
}
function prodFonts() {
return src(`${options.paths.src.fonts}/**/*`).pipe(
dest(options.paths.build.fonts)
);
}
function prodThirdParty() {
return src(`${options.paths.src.thirdParty}/**/*`).pipe(
dest(options.paths.build.thirdParty)
);
}
function prodClean() {
console.log(
"\n\t" + logSymbols.info,
"Cleaning build folder for fresh start.\n"
);
return src(options.paths.build.base, { read: false, allowEmpty: true }).pipe(
clean()
);
}
function buildFinish(done) {
console.log(
"\n\t" + logSymbols.info,
`Production build is complete. Files are located at ${options.paths.build.base}\n`
);
done();
}
exports.default = series(
devClean, // Clean Dist Folder
parallel(devStyles, devScripts, devImages, devFonts, devThirdParty, devHTML), //Run All tasks in parallel
livePreview, // Live Preview Build
watchFiles // Watch for Live Changes
);
exports.prod = series(
prodClean, // Clean Build Folder
parallel(
prodStyles,
prodScripts,
prodImages,
prodHTML,
prodFonts,
prodThirdParty
), //Run All tasks in parallel
buildFinish
);