Repository navigation
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
194 lines (172 loc) · 4.78 KB
/
Copy pathgulpfile.babel.js
File metadata and controls
194 lines (172 loc) · 4.78 KB
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
//utils
import gulp from "gulp";
import BrowserSync from "browser-sync";
import gutil from "gulp-util";
import cp from "child_process";
import postcss from "gulp-postcss";
import webpack from "webpack";
import webpackConfig from "./webpack.conf";
import svgstore from "gulp-svgstore";
import svgmin from "gulp-svgmin";
import inject from "gulp-inject";
import cssnano from "cssnano";
import autoprefixer from "autoprefixer";
import clean from "gulp-clean";
import sass from "gulp-sass";
import rename from "gulp-rename";
import workbox from "workbox-build";
const browserSync = BrowserSync.create();
const defaultArgs = ["-d", "../dist", "-s", "site"];
const browserslist = [
"last 1 version",
"> 1%",
"maintained node versions",
"not dead"
];
var hugoBin = `./bin/hugo.${process.platform === "win32" ? "exe" : process.platform}`;
if (process.env.HUGO_VERSION) {
hugoBin = "hugo";
}
if (process.env.DEBUG) {
defaultArgs.unshift("--debug");
}
gulp.task("hugo", (cb) => buildSite(cb));
gulp.task("hugo-preview", (cb) => buildSite(cb, function() { gulp.series("--buildDrafts", "--buildFuture") }));
gulp.task("build-preview", function() { gulp.series("sass", "js", "hugo-preview") });
gulp.task("browser-sync", function() {
browserSync.init({
server: {
baseDir: "./"
}
});
});
gulp.task("sass", () => {
const plugins = [autoprefixer({"overrideBrowserslist": browserslist}), cssnano()];
return gulp.src("./design/scss/main.scss")
.pipe(sass().on("error", sass.logError))
.pipe(postcss(plugins))
.pipe(rename({suffix: ".min"}))
.pipe(gulp.dest("./dist/css"))
.pipe(browserSync.stream());
});
gulp.task("js", (cb) => {
const myConfig = Object.assign({}, webpackConfig);
webpack(myConfig, (err, stats) => {
if (err) throw new gutil.PluginError("webpack", err);
gutil.log("[webpack]", stats.toString({
colors: true,
progress: true
}));
browserSync.reload();
cb();
});
});
gulp.task("svg", () => {
const svgs = gulp
.src("site/static/img/icons-*.svg")
.pipe(svgmin())
.pipe(svgstore({inlineSvg: true}));
function fileContents(filePath, file) {
return file.contents.toString();
}
return gulp
.src("site/layouts/partials/svg.html")
.pipe(inject(svgs, {transform: fileContents}))
.pipe(gulp.dest("site/layouts/partials/"));
});
// Clean
gulp.task("clean", function() {
return gulp.src("dist", {read: false})
.pipe(clean());
});
gulp.task("server", gulp.series("clean", "hugo", "sass", "js", "svg", (done) => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch("./design/js/**/*.js", gulp.series("js"));
gulp.watch("./design/scss/**/*.scss", gulp.series("sass") );
gulp.watch("./site/static/img/icons-*.svg", gulp.series("svg") );
gulp.watch("./site/**/*", gulp.series("hugo") );
done();
}));
gulp.task("generate-service-worker", (cb) => {
workbox.generateSW({
cacheId: "codecusp",
globDirectory: "./dist",
globPatterns: [
"**/*.{css,js,eot,ttf,woff,woff2,otf}"
],
swDest: "./dist/sw.js",
modifyUrlPrefix: {
"": "/"
},
clientsClaim: true,
skipWaiting: true,
ignoreUrlParametersMatching: [/./],
offlineGoogleAnalytics: true,
maximumFileSizeToCacheInBytes: 50 * 1024 * 1024,
runtimeCaching: [
{
urlPattern: /(?:\/)$/,
handler: "staleWhileRevalidate",
options: {
cacheName: "html",
expiration: {
maxAgeSeconds: 60 * 60 * 24 * 7,
},
},
},
{
urlPattern: /\.(?:png|jpg|jpeg|gif|bmp|webp|svg|ico)$/,
handler: "cacheFirst",
options: {
cacheName: "images",
expiration: {
maxEntries: 1000,
maxAgeSeconds: 60 * 60 * 24 * 365,
},
},
},
{
urlPattern: /\.(?:mp3|wav|m4a)$/,
handler: "cacheFirst",
options: {
cacheName: "audio",
expiration: {
maxEntries: 1000,
maxAgeSeconds: 60 * 60 * 24 * 365,
},
},
},
{
urlPattern: /\.(?:m4v|mpg|avi)$/,
handler: "cacheFirst",
options: {
cacheName: "videos",
expiration: {
maxEntries: 1000,
maxAgeSeconds: 60 * 60 * 24 * 365,
},
},
}
],
});
cb();
});
gulp.task("build", gulp.series("hugo", "sass", "js", "svg", "generate-service-worker", (done) => {
done();
}));
function buildSite(cb, options) {
const args = options ? defaultArgs.concat(options) : defaultArgs;
return cp.spawn(hugoBin, args, { stdio: "inherit" }).on("close", (code) => {
if (code === 0) {
browserSync.reload("notify:false");
cb();
} else {
browserSync.notify("Hugo build failed :(");
cb("Hugo build failed");
}
});
}