This repository has been archived by the owner on Jul 13, 2018. It is now read-only.
forked from techqueria/site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
executable file
·161 lines (147 loc) · 3.62 KB
/
gulpfile.babel.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
import autoprefixer from "autoprefixer";
import BrowserSync from "browser-sync";
import {spawn} from "child_process";
import cssnano from "cssnano";
import del from "del";
import log from "fancy-log";
import gulp from "gulp";
import brotli from "gulp-brotli";
import gzip from "gulp-gzip";
import htmlmin from "gulp-htmlmin";
import imagemin from "gulp-imagemin";
import postcss from "gulp-postcss";
import sass from "gulp-sass";
import sourcemaps from "gulp-sourcemaps";
import watch from "gulp-watch";
import hugoBin from "hugo-bin";
import PluginError from "plugin-error";
import csso from "postcss-csso";
import runSequence from "run-sequence";
import webpack from "webpack";
import webpackConfig from "./webpack.config";
// Browser Sync
const browserSync = BrowserSync.create();
// Hugo arguments
const hugoArgsDefault = ["-d", "../dist", "-s", "site"];
// Verbose Hugo Log
// const hugoArgsVerbose = ["-d", "../dist", "-s", "site", "-v"];
// Development tasks
gulp.task("hugo", (cb) => buildSite(cb));
// Build/production tasks
gulp.task("build", ["clean", "hugo", "sass", "js", "img", "static"], (callback) => {
runSequence("minify", callback);
});
// Minify HTML
gulp.task("minify", () =>
gulp
.src("./dist/**/*.html")
.pipe(
htmlmin({
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
useShortDoctype: true
})
)
.pipe(
brotli.compress({
skipLarger: true,
mode: 0,
quality: 11,
lgblock: 0
})
)
.pipe(
gzip({
skipGrowingFiles: true
})
)
.pipe(gulp.dest("./dist"))
);
// Compress SASS
gulp.task("sass", () =>
gulp
.src("./src/sass/styles.scss")
.pipe(
sass({
outputStyle: "compressed"
}).on("error", sass.logError)
)
.pipe(postcss([autoprefixer(), cssnano(), csso()]))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("./dist/assets/css"))
.pipe(browserSync.stream())
);
// Compress images
gulp.task("img", () =>
gulp
.src("./src/img/**/*")
.pipe(imagemin())
.pipe(gulp.dest("./dist/assets/img"))
);
// Copy static files
gulp.task("static", () =>
gulp
.src("./src/static/**/*")
.pipe(gulp.dest("./dist/assets"))
.pipe(browserSync.stream())
);
// Clean up dist
gulp.task("clean", () => {
return del.sync("dist");
});
// Compile Javascript
gulp.task("js", () => {
const myConfig = Object.assign({}, webpackConfig);
webpack(myConfig, (err, stats) => {
if (err) throw new PluginError("webpack", err);
log(
"[webpack]",
stats.toString({
colors: true,
progress: true
})
);
browserSync.reload();
});
});
// Development server with browser sync
gulp.task("server", ["hugo", "sass", "js", "img", "static"], () => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
watch("./src/sass/**/*.scss", () => {
gulp.start(["sass"]);
});
watch("./src/js/**/*.js", () => {
gulp.start(["js"]);
});
watch("./src/img/**/*", () => {
gulp.start(["img"]);
});
watch("./src/assets/**/*", () => {
gulp.start(["static"]);
});
watch("./site/**/*", () => {
gulp.start(["hugo"]);
});
});
// Run Hugo and build site
function buildSite(cb, options, environment = "development") {
const args = options ? hugoArgsDefault.concat(options) : hugoArgsDefault;
process.env.NODE_ENV = environment;
return spawn(hugoBin, args, {
stdio: "inherit"
}).on("close", (code) => {
if (code === 0) {
browserSync.reload();
cb();
} else {
browserSync.notify("Hugo build failed :(");
cb("Hugo build failed");
}
});
}