-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
154 lines (134 loc) · 5.14 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
/**
* Gulp with TailwindCSS - An CSS Utility framework
* 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, task, watch, series, parallel } = require('gulp');
const del = require('del'); //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'); //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 cleanCSS = require('gulp-clean-css');//To Minify CSS files
const purgecss = require('gulp-purgecss');// Remove Unused CSS from Styles
//Note : Webp still not supported in major browsers including forefox
//const webp = require('gulp-webp'); //For converting images to WebP format
//const replace = require('gulp-replace'); //For Replacing img formats to webp in html
const logSymbols = require('log-symbols'); //For Symbolic Console logs :) :P
const fileinclude = require('gulp-file-include');
//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(fileinclude({prefix: "@@", basepath: "@file",}))
.pipe(dest(options.paths.dist.base))
;
}
function devStyles(){
const tailwindcss = require('tailwindcss');
return src(`${options.paths.src.css}/**/*.scss`).pipe(sass().on('error', sass.logError))
.pipe(dest(options.paths.src.css))
.pipe(postcss([
tailwindcss(options.config.tailwindjs),
require('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 watchFiles(){
watch(`${options.paths.src.base}/**/*.html`,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));
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 del([options.paths.dist.base]);
}
//Production Tasks (Optimized Build for Live/Production Sites)
function prodHTML(){
return src(`${options.paths.src.base}/**/*.html`)
.pipe(fileinclude({prefix: "@@", basepath: "@file",}))
.pipe(dest(options.paths.build.base))
;
}
function prodStyles(){
return src(`${options.paths.dist.css}/**/*`)
.pipe(purgecss({
content: ['src/**/*.{html,js}'],
defaultExtractor: content => {
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
const innerMatches = content.match(/[^<>"'`\s.()]*[^<>"'`\s.():]/g) || []
return broadMatches.concat(innerMatches)
}
}))
.pipe(cleanCSS({compatibility: 'ie8'}))
.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(){
return src(options.paths.src.img + '/**/*').pipe(imagemin()).pipe(dest(options.paths.build.img));
}
function prodClean(){
console.log("\n\t" + logSymbols.info,"Cleaning build folder for fresh start.\n");
return del([options.paths.build.base]);
}
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, 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), //Run All tasks in parallel
buildFinish
);