-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
270 lines (251 loc) · 7.03 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** @format */
import gulp from "gulp";
import concat from "gulp-concat";
import postcss from "gulp-postcss";
import autoprefixer from "autoprefixer";
import * as sass from "sass";
import gulpSass from "gulp-sass";
import pug from "gulp-pug";
import connect from "gulp-connect";
import uglify from "gulp-uglify";
import notify from "gulp-notify";
import livereload from "gulp-livereload";
import cleanCSS from "gulp-clean-css";
import ts from "gulp-typescript";
import sourcemaps from "gulp-sourcemaps";
import plumber from "gulp-plumber";
import imagemin from "gulp-imagemin";
import { deleteAsync } from "del";
import dotenv from "dotenv";
import zip from "gulp-zip";
import imageminMozjpeg from "imagemin-mozjpeg";
import imageminPngquant from "imagemin-pngquant";
import imageminSvgo from "imagemin-svgo";
// !Initialize gulpSass with the Sass compiler
const sassCompiler = gulpSass(sass);
// !Load environment variables from .env file
dotenv.config();
// !Utility function to get the target environment
let getTargetEnvironment = () => {
const env = process.env.NODE_ENV || "production"; // *Default to development
if (!["development", "production"].includes(env)) {
console.warn(`Invalid environment: ${env}. Defaulting to "development".`);
return "development";
}
return env;
};
// !Dynamically set the target environment
let target = getTargetEnvironment();
let path = target === "development" ? "public" : "dist";
// !Directories
const paths = {
html: {
src: "src/pug/pages/*.pug",
dest: `${path}/`,
},
styleScss: {
src: "src/scss/*.scss",
dest: `${path}/css/`,
sourcemaps: "../../sourcemaps/",
},
stylesCss: {
src: "src/css/*.{css,map}",
dest: `${path}/css/`,
},
scripts: {
src: "src/js/*.{js,map}",
dest: `${path}/js/`,
},
images: {
src: "src/assets/images/**/*.{jpg,jpeg,png,gif,svg}",
dest: `${path}/assets/images/`,
},
videos: {
src: "src/assets/videos/**/*",
dest: `${path}/assets/videos/`,
},
webfonts: {
src: "src/assets/webfonts/**/*",
dest: `${path}/webfonts/`,
},
ts: {
src: "src/ts/**/*.ts",
dest: `${path}/js/`,
sourcemaps: "../../sourcemaps/",
},
};
// !Task to clean the folder using dynamic import for del
gulp.task("clean", function () {
return deleteAsync(`${path}`).then((paths) => {
console.log("Deleted files and folders:", paths.join("\n")); // *Log the deleted paths
});
});
// !Task to copy HTML files
gulp.task("pug", function () {
return gulp
.src(paths.html.src)
.pipe(pug({ pretty: false })) // *Compile Pug files to HTML
.pipe(gulp.dest(paths.html.dest))
.pipe(livereload());
});
// !Task to compile SCSS to CSS
gulp.task("styles", function () {
return gulp
.src(paths.styleScss.src)
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>"),
})
) // *Prevents pipe breaking caused by errors
.pipe(sourcemaps.init()) // *Initialize sourcemaps
.pipe(sassCompiler().on("error", sassCompiler.logError)) // *Compile SCSS
.pipe(
postcss([
autoprefixer({
cascade: false,
}),
])
) // *Add vendor prefixes
.pipe(concat("style.css")) // *Concatenate all files into one
.pipe(cleanCSS()) // *Minify CSS
.pipe(sourcemaps.write(paths.styleScss.sourcemaps)) // *Write sourcemaps to specified folder
.pipe(gulp.dest(paths.styleScss.dest)) // *Output concatenated and minified CSS to destination
.pipe(livereload());
// TODO .pipe(notify("Styles task complete"));
});
// !Task to copy CSS files
gulp.task("css", function () {
return gulp
.src(paths.stylesCss.src)
.pipe(cleanCSS()) // *Minify CSS
.pipe(gulp.dest(paths.stylesCss.dest))
.pipe(livereload());
// TODO .pipe(notify("CSS Files copied"));
});
// !Task to minify and concatenate JavaScript files
gulp.task("scripts", function () {
return gulp
.src(paths.scripts.src)
.pipe(uglify()) // *Minify JS
.pipe(gulp.dest(paths.scripts.dest)) // *Output minified files to destination
.pipe(livereload());
// TODO .pipe(notify("Scripts task complete"));
});
// !Task to compile TypeScript
gulp.task("typescript", function () {
return gulp
.src(paths.ts.src)
.pipe(
plumber({
errorHandler: notify.onError("Error: <%= error.message %>"),
})
)
.pipe(sourcemaps.init()) // *Initialize sourcemap generation
.pipe(ts())
.pipe(concat("main.js")) // *Concatenate files
.pipe(uglify()) // *Minify JS
.pipe(sourcemaps.write(paths.ts.sourcemaps)) // *Write sourcemaps to specified folder
.pipe(gulp.dest(paths.ts.dest))
.pipe(livereload());
// TODO .pipe(notify("TypeScript task complete"));
});
// !Task to optimize images
gulp.task("images", () => {
return gulp
.src(paths.images.src)
.pipe(
plumber({
errorHandler: notify.onError({
title: "Image Optimization Error",
message: "Error: <%= error.message %>",
}),
})
)
.pipe(
imagemin([
imageminMozjpeg({ quality: 75, progressive: true }),
imageminPngquant({
quality: [0.6, 0.8],
speed: 1,
}),
imageminSvgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }],
}),
])
)
.pipe(gulp.dest(paths.images.dest))
.pipe(
notify({
message: "Images optimized successfully!",
onLast: true,
})
)
.on(
"error",
notify.onError({
title: "Gulp Error",
message: "Error: <%= error.message %>",
})
); // TODO Global error handler
});
// !Task to copy videos
gulp.task("videos", function () {
return gulp
.src(paths.videos.src)
.pipe(gulp.dest(paths.videos.dest))
.pipe(livereload());
// TODO .pipe(notify("Videos copied"));
});
// !Task to copy webfonts
gulp.task("webfonts", function () {
return gulp
.src(paths.webfonts.src)
.pipe(gulp.dest(paths.webfonts.dest))
.pipe(livereload());
// TODO .pipe(notify("Webfonts copied"));
});
// !Task to create a zip file of the folder
gulp.task("compress", async () => {
return gulp
.src("dist/**/*")
.pipe(zip("website.zip"))
.pipe(gulp.dest("./"))
.pipe(notify("Project compressed to website.zip"));
});
// !Serve and watch files for changes
gulp.task("serve", function (done) {
connect.server({
root: "public",
livereload: true,
port: 8000,
});
done(); // *Server is running
});
// !Watch task
gulp.task("watch", function () {
livereload.listen();
gulp.watch("src/pug/**/*.pug", gulp.series("pug"));
gulp.watch("src/scss/**/*.scss", gulp.series("styles"));
gulp.watch("src/js/**/*.js", gulp.series("scripts"));
gulp.watch(paths.images.src, gulp.series("images"));
gulp.watch(paths.videos.src, gulp.series("videos"));
gulp.watch("src/ts/**/*.ts", gulp.series("typescript"));
});
// !Default task
gulp.task(
"default",
gulp.series(
"clean",
gulp.parallel(
"pug",
"styles",
"scripts",
"images",
"videos",
"webfonts",
"css",
"typescript"
),
gulp.parallel("serve", "watch")
)
);