This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
gulpfile.babel.js
executable file
·185 lines (166 loc) · 4.92 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const autoprefixer = require("autoprefixer");
const browserSync = require("browser-sync").create();
const cp = require("child_process");
const cssnano = require("cssnano");
const csso = require("postcss-csso");
const del = require("del");
const gulp = require("gulp");
const htmlmin = require("gulp-htmlmin");
const hugoBin = require("hugo-bin");
const imagemin = require("gulp-imagemin");
const log = require("fancy-log");
const PluginError = require("plugin-error");
const postcss = require("gulp-postcss");
const sass = require("gulp-sass");
const sourcemaps = require("gulp-sourcemaps");
const webpack = require("webpack");
const webpackConfig = require("./webpack.prod");
const webpackDevConfig = require("./webpack.dev");
// DEV: Compress SASS
// Does not purify it since that takes longer
gulp.task("sass", () => {
return gulp
.src("./assets/sass/styles.scss")
.pipe(
sass({
outputStyle: "compressed"
}).on("error", sass.logError)
)
.pipe(gulp.dest("./dist/assets/css"))
.pipe(browserSync.stream());
});
// PROD: Compress SASS
gulp.task("sass-minify", () => {
return gulp
.src("./assets/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());
});
// DEV: Move images
gulp.task("img", () => {
return gulp.src("./assets/img/**/*").pipe(gulp.dest("./dist/assets/img"));
});
// PROD: Move & minify images
gulp.task("img-minify", () => {
return gulp.src("./assets/img/**/*").pipe(imagemin()).pipe(gulp.dest("./dist/assets/img"));
});
// DEV & PROD: Compile Javascript
gulp.task("js", (done) => {
const environment = process.env.NODE_ENV;
log("ENVIRONMENT: " + environment);
let myConfig = {};
if (environment === "dev") {
myConfig = Object.assign({}, webpackDevConfig);
} else {
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();
});
done();
});
// PROD: Minify HTML
gulp.task("html-minify", () => {
return gulp
.src("./dist/**/*.html")
.pipe(
htmlmin({
collapseBooleanAttributes: true,
collapseWhitespace: true,
conservativeCollapse: true,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: false,
minifyJS: true,
removeComments: true,
removeRedundantAttributes: true
})
)
.pipe(gulp.dest("./dist"));
});
// DEV & PROD: Clean up dist
gulp.task("clean", () => {
return del(["dist"]);
});
// DEV & PROD: Move PDF
gulp.task("pdf", () => {
return gulp.src("./assets/pdf/**/*").pipe(gulp.dest("./dist/assets/pdf"));
});
// DEV & PROD: Server with browsersync
const runServer = (options) => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
gulp.watch("./assets/js/**/*.js", gulp.series("js"));
gulp.watch("./assets/sass/**/*.scss", gulp.series("sass"));
gulp.watch("./assets/img/**/*", gulp.series("img"));
gulp.watch(["./content/**/*", "./data/**/*", "./layouts/**/*", "./resources/**/*", "./static/**/*", "./config.toml"], gulp.series(options));
};
// DEV & PROD: Run Hugo
const buildSite = (done, options, environment) => {
const args = options ? hugoArgsDefault.concat(options) : hugoArgsDefault;
process.env.NODE_ENV = environment;
return cp
.spawn(hugoBin, args, {
stdio: "inherit"
})
.on("close", (code) => {
if (code === 0) {
browserSync.reload();
browserSync.notify("Your changes were applied :)");
done();
} else {
browserSync.notify("The Hugo build failed :(");
done("Hugo build failed");
}
});
};
// Hugo arguments
const hugoArgsDefault = ["-d", "./dist", "-s", "./", "--verbose"];
// const hugoArgsDefault = ["-d", "./dist", "-s", "./", "--templateMetrics"];
const hugoArgsPreview = ["--buildDrafts", "--buildFuture"];
// DEVELOPMENT
gulp.task("hugo-dev", (done) => buildSite(done, [], "dev"));
gulp.task("hugo-preview", (done) => buildSite(done, hugoArgsPreview, "dev"));
gulp.task(
"server",
gulp.series("hugo-dev", "sass", "img", "js", (done) => {
runServer("hugo-dev");
done();
})
);
gulp.task(
"server-preview",
gulp.series("hugo-preview", "sass", "img", "js", (done) => {
runServer("hugo-preview");
done();
})
);
gulp.task("build-dev", gulp.series("clean", "hugo-dev", "sass", "img", "js"));
// PRODUCTION
gulp.task("hugo", (done) => buildSite(done, [], "prod"));
gulp.task(
"server-prod",
gulp.series("hugo", "img-minify", "js", "sass-minify", "html-minify", (done) => {
runServer("hugo");
done();
})
);
gulp.task("build", gulp.series("clean", "hugo", "img-minify", "js", "sass-minify", "html-minify"));