-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
193 lines (178 loc) · 5 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
const gulp = require("gulp");
const del = require("del");
const terser = require("gulp-terser");
const postcss = require("gulp-postcss");
const imagemin = require("gulp-imagemin");
const htmlmin = require("gulp-htmlmin");
const babel = require("gulp-babel");
const data = require("gulp-data");
const cache = require("gulp-cached");
const sass = require("gulp-sass");
const flatmap = require("gulp-flatmap");
sass.compiler = require("node-sass");
const nunjucksRender = require("gulp-nunjucks-render");
const cssnano = require("cssnano");
const uncss = require("postcss-uncss");
const autoprefixer = require("autoprefixer");
var replace = require("gulp-replace");
const fs = require("fs");
const inject = require("gulp-inject");
// Load environment variable
require("dotenv").config();
const sourceFolder = "source";
const outputFolder = "dest";
gulp.task("clean", function () {
return del(`${outputFolder}/**`, { force: true });
});
gulp.task("build:html", function () {
return gulp
.src(`${sourceFolder}/pages/**/*.+(html|nunjucks|njk)`)
.pipe(
data(function () {
return {
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY,
...JSON.parse(fs.readFileSync(`${sourceFolder}/data/skillset.json`)),
...JSON.parse(fs.readFileSync(`${sourceFolder}/data/companies.json`)),
...JSON.parse(fs.readFileSync(`${sourceFolder}/data/timeline.json`)),
};
})
)
.pipe(
nunjucksRender({
path: [`${sourceFolder}/templates`],
})
)
.pipe(gulp.dest(outputFolder));
});
gulp.task("build:assets", function () {
return gulp
.src([`${sourceFolder}/static/assets/**`])
.pipe(cache("assets"))
.pipe(imagemin([imagemin.mozjpeg({ quality: 50 })]))
.pipe(gulp.dest(`${outputFolder}/assets`));
});
gulp.task("build:js", function () {
return gulp
.src([`${sourceFolder}/static/js/*.js`])
.pipe(
babel({
presets: ["@babel/env"],
})
)
.pipe(terser())
.pipe(gulp.dest(`${outputFolder}/js`));
});
gulp.task("include:lazysizes", function () {
return gulp
.src([
"node_modules/lazysizes/lazysizes.min.js",
"node_modules/lazysizes/plugins/unveilhooks/ls.unveilhooks.min.js",
])
.pipe(gulp.dest(`${outputFolder}/js`));
});
gulp.task("build:css", function () {
return gulp
.src(`${sourceFolder}/static/css/*.+(css|scss)`)
.pipe(sass().on("error", sass.logError))
.pipe(cache("css"))
.pipe(
flatmap(function (stream, file) {
const subject = file.relative.replace(".css", "");
const htmlFile =
subject === "main" ? "index.html" : `${subject}/index.html`;
return stream.pipe(
postcss([
uncss({
htmlroot: outputFolder,
html: [`${outputFolder}/${htmlFile}`],
ignore: [/^(\.navbar-burger).*/, /^(\.navbar-menu).*/],
timeout: 100,
}),
autoprefixer,
cssnano,
])
);
})
)
.pipe(gulp.dest(`${outputFolder}/css`));
});
gulp.task("merge:css", function () {
return gulp
.src(`${outputFolder}/**/*.html`)
.pipe(
flatmap(function (stream, file) {
const subject = file.relative
.replace("index.html", "")
.replace("/", "");
const cssfile = subject === "" ? "main.css" : `${subject}.css`;
return stream
.pipe(inject(gulp.src(`${outputFolder}/css/${cssfile}`)))
.pipe(
replace(/<link rel="stylesheet" href="[^"]*"*>/g, function (
linkTag
) {
var style = fs.readFileSync(
`.${getCSSFilename(linkTag)}`,
"utf8"
);
return "<style>\n" + style + "\t</style>";
})
);
})
)
.pipe(gulp.dest(outputFolder));
});
function getCSSFilename(linkTag) {
var hrefValue = /href\=\"([A-Za-z0-9/._]*)\"/g;
var cssFilename = linkTag.match(hrefValue);
cssFilename = cssFilename[0].replace('href="', "").replace('"', "");
return cssFilename;
}
gulp.task("minify:html", function () {
return gulp
.src(`${outputFolder}/**/*.html`)
.pipe(
htmlmin({
collapseInlineTagWhitespace: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
sortAttributes: true,
sortClassName: true,
removeComments: true,
quoteCharacter: true,
minifyURLs: true,
minifyJS: true,
minifyCSS: true,
})
)
.pipe(gulp.dest(outputFolder));
});
gulp.task("deploy", function () {
// write your script here to deploy app to your server
});
gulp.task(
"build",
gulp.series([
"clean",
"build:html",
"build:js",
"build:assets",
"build:css",
"merge:css",
"include:lazysizes",
"minify:html",
])
);
gulp.task("watch", function () {
return gulp.watch(
`${sourceFolder}/**`,
gulp.series([
"build:html",
"build:js",
"build:assets",
"build:css",
"merge:css",
"include:lazysizes",
])
);
});