-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
229 lines (205 loc) · 6.55 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
const { src, dest, watch, series, parallel } = require("gulp");
const clean = require("gulp-clean");
const options = require("./config");
const browserSync = require("browser-sync").create();
// Core plugins for file processing
const sass = require("gulp-sass")(require("sass"));
const postcss = require("gulp-postcss");
const concat = require("gulp-concat");
const uglify = require("gulp-terser");
const rename = require("gulp-rename");
const imagemin = require("gulp-imagemin");
const mozjpeg = require("imagemin-mozjpeg");
const pngquant = require("imagemin-pngquant");
const purgecss = require("gulp-purgecss");
const logSymbols = require("log-symbols");
const includePartials = require("gulp-file-include");
const htmlPretty = require("gulp-pretty-html");
// Function to start BrowserSync preview during development
function livePreview(done) {
browserSync.init({
server: {
baseDir: options.paths.dist.base, // Serve files from the 'dist' folder
},
port: options.config.port || 5000, // Use configured port or default to 5000
});
done();
}
// Function to reload the browser preview
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()) // Include partials if needed
.pipe(htmlPretty()) // Format HTML for readability
.pipe(dest(options.paths.dist.base)); // Output to 'dist' folder
}
function devStyles() {
const autoprefixer = require("autoprefixer");
return src(`${options.paths.src.css}/**/*.scss`)
.pipe(sass().on("error", sass.logError)) // Compile SASS with error logging
.pipe(postcss([autoprefixer()])) // Add vendor prefixes
.pipe(concat({ path: "style.css" }))
.pipe(dest(options.paths.dist.css)); // Output to 'dist/css' folder
}
function devScripts() {
return src([
`${options.paths.src.js}/libs/**/*.js`,
`${options.paths.src.js}/**/*.js`,
]).pipe(dest(options.paths.dist.js));
}
function devImages() {
return src(`${options.paths.src.images}/**/*`).pipe(
dest(options.paths.dist.images)
);
}
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.paths.src.css}/**/*.scss`],
series(devStyles, previewReload)
);
watch(`${options.paths.src.js}/**/*.js`, series(devScripts, previewReload));
watch(`${options.paths.src.images}/**/*`, series(devImages, previewReload));
watch(`${options.paths.src.fonts}/**/*`, series(devFonts, previewReload));
watch(
`${options.paths.src.thirdParty}/**/*`,
series(devThirdParty, previewReload)
);
console.log(`${options.paths.src.css}/**/*.scss`);
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
// ---------------------------------------------------------------------------
function prodHTML() {
return src(`${options.paths.src.base}/**/*.{html,php}`)
.pipe(includePartials())
.pipe(htmlPretty())
.pipe(dest(options.paths.build.base));
}
function prodStyles() {
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
return (
src(`${options.paths.src.css}/**/*.scss`)
.pipe(sass().on("error", sass.logError))
.pipe(postcss([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)) // Output to 'build/css' folder
);
}
function prodScripts() {
return src([
`${options.paths.src.js}/libs/**/*.js`,
`${options.paths.src.js}/**/*.js`,
])
.pipe(uglify())
// .pipe(
// rename(function (path) {
// path.basename += ".min";
// })
// )
.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.images + "/**/*")
.pipe(imagemin([...plugins]))
.pipe(dest(options.paths.build.images));
}
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();
}
// Default task for development
exports.default = series(
devClean, // Clean 'dist' folder
parallel(devStyles, devScripts, devImages, devFonts, devThirdParty, devHTML), // Run tasks in parallel for efficiency
livePreview, // Start BrowserSync preview
watchFiles // Watch for changes and rerun tasks
);
// Production task
exports.prod = series(
prodClean, // Clean 'build' folder
parallel(
prodStyles,
prodScripts,
prodImages,
prodHTML,
prodFonts,
prodThirdParty
), // Run tasks in parallel
buildFinish // Notify completion and output location
);