-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
190 lines (173 loc) · 5.21 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
// Initialize Modules
const gulp = require("gulp");
const { src, dest, watch, series, parallel } = require("gulp");
const clean = require("gulp-clean");
const browserSync = require("browser-sync").create();
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
const fs = require("fs");
const postcss = require("gulp-postcss");
const replace = require("gulp-replace");
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const uglify = require("gulp-uglify");
const imagemin = require("gulp-imagemin");
const useref = require("gulp-useref");
const nunjucksRender = require("gulp-nunjucks-render");
const prettyHtml = require("gulp-pretty-html");
const gulpif = require("gulp-if");
// File Path Variables
const files = {
vendorPath: "app/assets/vendor/**/*",
vendorSass: "app/assets/vendor/**/*.scss",
scssPath: "app/assets/scss/**/*.scss",
jsPath: "app/assets/js/**/*.js",
fontPath: "app/assets/fonts/**/*.{eot,svg,ttf,woff,woff2}",
imagePath: "app/assets/images/**/*",
distImagePath: "dist/assets/images/**/*",
htmlPath: "dist/**/*.html",
njkPath: "app/views/**/*.+(html|nunjucks|njk)",
njkPages: "app/views/pages/**/*.+(html|nunjucks|njk)",
manifest: "./manifest.json",
};
// Sass task
function scssTask() {
return src(files.scssPath)
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(sourcemaps.write("."))
.pipe(dest("dist/assets/css"));
}
// Vendor Sass task
function vendorScssTask() {
return src(files.vendorSass)
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(sourcemaps.write("."))
.pipe(dest("dist/assets/css"));
}
// js task
function jsTask() {
return src(files.jsPath).pipe(dest("dist/assets/js"));
}
// Move Vendor Files to dist For Client
function vendorMove() {
return src(files.vendorPath).pipe(dest("dist/assets/vendor"));
}
// Move Sass Files to dist For Client
function scssMove() {
return src(files.scssPath).pipe(dest("dist/assets/sass"));
}
//Image Task
function imageTask() {
return src(files.imagePath).pipe(gulp.dest("dist/assets/images"));
}
// Html Task
const cbString = new Date().getTime();
function htmlTask() {
return src(files.njkPages)
.pipe(
nunjucksRender({
path: ["app/views"],
watch: true,
manageEnv: function (env) {
var data = JSON.parse(fs.readFileSync(files.manifest));
env.addGlobal("site", data);
},
})
)
.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"));
}
// Combine Everything
function combineTask() {
return src(files.htmlPath)
.pipe(useref())
.pipe(gulpif("*.js", uglify()))
.pipe(dest("dist"));
}
// browser Sync: To Initialize
function serve(done) {
browserSync.init({
port: 3001,
server: {
baseDir: "./dist",
},
});
done();
}
// browser Sync: To Reload
function reload(done) {
browserSync.reload();
done();
}
// watch task
function watchTask() {
watch(
[
files.manifest,
files.scssPath,
files.jsPath,
files.imagePath,
files.njkPath,
],
parallel(scssTask, jsTask, imageTask, htmlTask, reload)
);
}
// #########################################################
// Default Tasks =======================================
// #########################################################
exports.default = series(
vendorScssTask,
parallel(scssTask, jsTask, htmlTask),
scssMove,
vendorMove,
imageTask,
reload,
serve,
watchTask
);
exports.combine = series(combineTask);
// #########################################################
// Non Default Tasks =======================================
// #########################################################
//Image Compression Task
gulp.task("optimize", () => {
return gulp
.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(gulp.dest("dist/assets/images"));
});
// Clean Everything
gulp.task("clean", function () {
return gulp.src("./dist", { read: false }).pipe(clean());
});