-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
245 lines (216 loc) · 6.48 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
// Initialize Modules
const gulp = require("gulp");
const { src, dest, watch, series, parallel } = require("gulp");
// Importing all gulp files
const clean = require("gulp-clean"); // Remove file
const browserSync = require("browser-sync").create(); // Automatically create server, watch files and reload on browser
const autoprefixer = require("autoprefixer"); // Browser Support Ad
const cssnano = require("cssnano"); // Optimize or minifiy css which consume size of css
const concat = require("gulp-concat"); // Two or more files push on a single file and give them new name
const postcss = require("gulp-postcss"); // Give editing eligibily to css change while render such as adding autoprefixer
const replace = require("gulp-replace"); // For rmoving cash files
const sass = require("gulp-sass")(require("sass")); // Wrap the scss and convert it to the style.css
const sourcemaps = require("gulp-sourcemaps"); // It shows where is the scss written
const uglify = require("gulp-uglify"); // Make js code minify
const babel = require("gulp-babel");
const terser = require("gulp-terser"); // Make js code minify
const useref = require("gulp-useref"); // Multiple css & js file combine and make new file
const imagemin = require("gulp-imagemin"); // Optimize image file size
const nunjucksRender = require("gulp-nunjucks-render"); // Nunjucks convert to html
const prettyHtml = require("gulp-pretty-html"); // Decorating Html
const gulpif = require("gulp-if"); // This gulp plugin is used when we want to run a task only if certain condition is met
// File path variables
const files = {
htmlPath: "dist/**/*.html",
njkPath: "src/views/**/*.+(html|nunjucks|njk)",
njkPages: "src/views/pages/**/*.+(html|nunjucks|njk)",
scssPath: "src/assets/scss/**/*.scss",
jsPath: "src/assets/js/**/*.js",
fontPath: "src/assets/fonts/**/*.{eot,svg,ttf,woff,woff2}",
imagePath: "src/assets/images/**/*",
distImagePath: "dist/assets/images/**/*",
vendorPath: "src/assets/vendor/**/*",
vendorSass: "src/assets/vendor/**/*.scss",
};
// Html Task
function htmlTask() {
const cbString = new Date().getTime();
return src(files.njkPages)
.pipe(
nunjucksRender({
path: ["src/views/"],
})
)
.pipe(replace(/cb=\d+/g, "cb=" + cbString))
.pipe(
prettyHtml({
indent_size: 4,
indent_char: " ",
unformatted: ["code", "pre", "em", "strong", "span", "i", "b", "br"],
extra_liners: ["head", "body"],
max_preserve_newlines: 1,
})
)
.pipe(dest("dist"));
}
// Scss Task
function scssTask() {
const autoprefixBrowsers = [
"> 1%",
"last 50 versions",
"firefox >= 4",
"safari 7",
"safari 8",
"IE 8",
"IE 9",
"IE 10",
"IE 11",
];
const plugins = [
autoprefixer({ overrideBrowserslist: autoprefixBrowsers }),
cssnano(),
];
return (
src(files.scssPath, { sourcemaps: true })
// .pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
.pipe(postcss(plugins))
// .pipe(concat('custom-style-all.min.css'))
// .pipe(sourcemaps.write('.'))
.pipe(dest("dist/assets/css", { sourcemaps: "." }))
);
}
// Vendor Scss Task
function vendorScssTask() {
const plugins = [
autoprefixer({ overrideBrowserslist: ["last 20 version"] }),
cssnano(),
];
return (
src(files.vendorSass, { sourcemaps: true })
// .pipe(sourcemaps.init())
.pipe(sass().on("error", sass.logError))
.pipe(postcss(plugins))
// .pipe(concat('vendor-style-all.css'))
// .pipe(sourcemaps.write('.'))
.pipe(dest("dist/assets/css", { sourcemaps: "." }))
);
}
// Javascript Task
function jsTask() {
return (
src(files.jsPath, { sourcemaps: true })
// .pipe(sourcemaps.init())
.pipe(
babel({
presets: ["@babel/env"],
})
)
// .pipe(concat('custom-script-all.min.js'))
.pipe(terser())
// .pipe(sourcemaps.write('.'))
.pipe(dest("dist/assets/js", { sourcemaps: "." }))
);
}
// Move Vendor Files to dist For Client
function vendorMove() {
return src(files.vendorPath).pipe(dest("dist/assets/vendor"));
}
// Move scss Files to dist for Client
function scssMove() {
return src(files.scssPath).pipe(dest("dist/assets/scss"));
}
// Image Move Task
function imageTask() {
return src(files.imagePath).pipe(dest("dist/assets/images/"));
}
// Fonts Move Task
function fontsTask() {
return src(files.fontPath).pipe(dest("dist/assets/fonts/"));
}
// BroweserSync Task : Initialize a server
function browserSyncServer(done) {
browserSync.init({
port: "3001",
server: {
baseDir: "./dist",
},
});
done();
}
// BroweserSync Task : Reload the server automatically
function browserSyncReload(done) {
browserSync.reload();
done();
}
// Combine Task
function combineTask() {
return src(files.htmlPath)
.pipe(useref({ searchPath: "./dist" }))
.pipe(dest("./dist"));
}
// Watch Task
function watchTask() {
watch(
[
files.scssPath,
files.jsPath,
files.imagePath,
files.njkPath,
files.vendorPath,
],
parallel(
scssTask,
jsTask,
imageTask,
htmlTask,
vendorMove,
browserSyncReload
)
);
}
// #########################################################
// Default Tasks =======================================
// #########################################################
exports.default = series(
vendorScssTask,
parallel(scssTask, jsTask, htmlTask, fontsTask),
scssMove,
vendorMove,
imageTask,
combineTask,
browserSyncReload,
browserSyncServer,
watchTask
);
// #########################################################
// Non Default Tasks =======================================
// #########################################################
//Image Minify Task
function imageMinify() {
return src(files.imagePath)
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ quality: 75, progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [{ removeViewBox: true }, { cleanupIDs: false }],
}),
])
)
.pipe(dest("dist/assets/images/"));
}
exports.imageMinify = imageMinify;
// Clean dist Folder Task
function cleanDist() {
return src("dist", { read: false }).pipe(clean());
}
exports.cleanDist = cleanDist;
// Clean Images Task
function imageClean() {
return src("dist/assets/images/", { read: false, allowEmpty: true }).pipe(
clean({ force: true })
);
}
exports.imageClean = imageClean;