-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
244 lines (223 loc) · 5.17 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
/*!
* Gulp SMPL Layout Builder
*
* @version 2.0.0
* @author Aditya Sutar (Draft)
* @type Module gulp
* @license The MIT License (MIT)
*/
const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
let autoprefixer;
import("gulp-autoprefixer").then((module) => (autoprefixer = module.default));
const cleanCSS = require("gulp-clean-css");
const pug = require("gulp-pug");
const rename = require("gulp-rename");
const webpackStream = require("webpack-stream");
const webpackConfig = require("./webpack.config.js");
const connect = require("gulp-connect");
const browserSync = require("browser-sync").create();
const http = require("http-server");
const clean = require("gulp-clean");
const prettify = require("gulp-prettify");
const sitemap = require("gulp-sitemap");
// Define paths
const paths = {
src: {
styles: "src/styles/**/*.scss",
scripts: ["src/js/**/*.js", "!src/js/**/node_modules/**"],
pug: "src/pug/**/*.pug",
fonts: "src/assets/fonts/**/*",
favicons: "src/assets/favicons/**/*",
additionalAssets: [
"src/assets/**/*.png",
"src/assets/**/*.jpg",
"src/assets/**/*.jpeg",
"src/assets/**/*.svg",
"src/assets/**/*.webp",
"src/assets/**/*.mp4",
],
packageJson: "package.json",
},
dest: {
css: "dist/styles",
js: "dist/js",
html: "dist",
fonts: "dist/fonts",
favicons: "dist",
additionalAssets: "dist/assets",
},
};
// Copy font files task
function copyFonts() {
return gulp.src(paths.src.fonts).pipe(gulp.dest(paths.dest.fonts));
}
// Copy favicon files task
function copyFavicons() {
return gulp.src(paths.src.favicons).pipe(gulp.dest(paths.dest.favicons));
}
// Copy additional asset folders task
function copyAssets() {
return gulp
.src(paths.src.additionalAssets, {
base: "app",
encoding: false,
buffer: true,
removeBOM: false,
})
.pipe(gulp.dest(paths.dest.additionalAssets));
}
// Copy package.json to dist task
function copyPackageJson() {
return gulp.src(paths.src.packageJson).pipe(gulp.dest(paths.dest.html));
}
// Compile Sass task
async function sassTask() {
if (!autoprefixer) {
autoprefixer = (await import("gulp-autoprefixer")).default;
}
return gulp
.src(paths.src.styles)
.pipe(
sass({
includePaths: ["node_modules"],
}).on("error", sass.logError)
)
.pipe(autoprefixer())
.pipe(cleanCSS())
.pipe(
rename({
extname: ".min.css",
})
)
.pipe(gulp.dest(paths.dest.css))
.pipe(browserSync.stream());
}
// Bundle JS task
function jsTask() {
return gulp
.src(paths.src.scripts)
.pipe(webpackStream(webpackConfig))
.pipe(gulp.dest(paths.dest.js))
.pipe(browserSync.stream());
}
// Compile 404 Pug task
function pug404Task() {
return gulp
.src("src/pug/404.pug")
.pipe(pug())
.pipe(rename("404.html")) // Rename the output file to 404.html
.pipe(gulp.dest(paths.dest.html))
.pipe(browserSync.stream());
}
// Minify pug task
function pugTask() {
return gulp
.src(paths.src.pug)
.pipe(pug())
.pipe(gulp.dest(paths.dest.html))
.pipe(browserSync.stream());
}
// Serve task
function serveTask() {
connect.server({
root: "dist",
livereload: true,
middleware: function () {
return [
function (req, res, next) {
if (req.url.endsWith(".css")) {
res.setHeader("Content-Type", "text/css");
}
next();
},
];
},
});
browserSync.init({
server: {
baseDir: "dist",
},
open: true,
});
gulp.watch(paths.src.styles, gulp.series(sassTask));
gulp.watch(paths.src.scripts, gulp.series(jsTask));
gulp.watch(paths.src.pug, gulp.series(pugTask));
}
// HTTP server task
function httpServerTask() {
return http
.createServer({
root: "./dist",
port: 8080,
cache: 1,
robots: true,
headers: {
"Access-Control-Allow-Origin": "*",
// Add any additional headers here
},
})
.listen(8080);
}
// Clean task
function cleanTask() {
return gulp
.src("dist", {
read: false,
allowEmpty: true,
})
.pipe(clean());
}
// Prettify HTML task
function prettifyHtmlTask() {
return gulp
.src(`dist/**/*.html`)
.pipe(
prettify({
indent_size: 2,
})
)
.pipe(gulp.dest(paths.dest.html));
}
// sitemap task
gulp.task("sitemap", () => {
return gulp
.src("dist/**/*.html", {
read: false,
})
.pipe(
sitemap({
siteUrl: "https://brewnbeer.com/",
})
)
.pipe(gulp.dest("dist")); // Change this line to save the sitemap in the 'dist' folder
});
// Copy robots.txt task
function copyRobotsTxt() {
return gulp.src("src/extra/robots.txt").pipe(gulp.dest("dist"));
}
// Build task
const build = gulp.series(
cleanTask,
gulp.parallel(
copyFavicons,
copyAssets,
copyPackageJson,
sassTask,
copyFonts,
jsTask,
pugTask,
pug404Task,
copyRobotsTxt
),
prettifyHtmlTask,
"sitemap"
);
// Default task
const defaultTask = gulp.series(
build,
gulp.parallel(serveTask, httpServerTask)
);
gulp.task("clean", cleanTask);
gulp.task("build", build);
gulp.task("default", defaultTask);