This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.babel.js
134 lines (117 loc) · 3.61 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
import gulp from "gulp";
import cp from "child_process";
import gutil from "gulp-util";
import postcss from "gulp-postcss";
import cssImport from "postcss-import";
import cssnext from "postcss-cssnext";
import BrowserSync from "browser-sync";
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 replace from "gulp-replace";
import cssnano from "cssnano";
import watch from "gulp-watch";
const browserSync = BrowserSync.create();
const hugoBin = "hugo";
// Hugo arguments
const hugoArgsDefault = ["-d", "../dist", "-s", "site", "-v"];
const hugoArgsPreview = ["--buildDrafts", "--buildFuture"];
// Development tasks
gulp.task("hugo", (cb) => buildSite(cb));
gulp.task("hugo-preview", (cb) => buildSite(cb, hugoArgsPreview));
// Build/production tasks
gulp.task("build", ["css", "js", "cms"], (cb) => buildSite(cb, [], "production"));
gulp.task("build-preview", ["css", "js", "cms"], (cb) => buildSite(cb, hugoArgsPreview, "production"));
gulp.task("cms", () => {
const match = process.env.REPOSITORY_URL ? process.env.REPOSITORY_URL : cp.execSync("git remote -v", {encoding: "utf-8"});
let repo = null;
match.replace(/github.com:(\S+)(\.git)?/, (_, m) => {
repo = m.replace(/\.git$/, "");
});
gulp.src("./src/cms/*")
.pipe(replace("<% GITHUB_REPOSITORY %>", repo))
.pipe(gulp.dest("./dist/admin"))
.pipe(browserSync.stream());
});
// Compile CSS with PostCSS
gulp.task("css", () => (
gulp.src("./src/css/*.css")
.pipe(postcss([
cssImport({from: "./src/css/main.css"}),
cssnext(),
cssnano(),
]))
.pipe(gulp.dest("./dist/css"))
.pipe(browserSync.stream())
));
// Compile Javascript
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/"));
});
// Development server with browsersync
gulp.task("server", ["hugo", "css", "js", "svg", "cms"], () => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
watch("./src/js/**/*.js", ["js"]);
watch("./src/css/**/*.css", ["css"]);
watch("./src/cms/*", ["cms"]);
watch("./site/static/img/icons/*.svg", ["svg"]);
watch("./site/**/*", ["hugo"]);
});
gulp.task("generate-service-worker", function(callback) {
var swPrecache = require("sw-precache");
var rootDir = "dist";
swPrecache.write(
`${rootDir}/service-worker.js`,
{
staticFileGlobs: [
rootDir + "/**/*.{js,html,xml,css,png,jpg,gif,svg,eot,ttf,woff}"
],
stripPrefix: rootDir
},
callback
);
});
/**
* Run hugo and build the site
*/
function buildSite(cb, options, environment = "development") {
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("notify:false");
cb();
} else {
browserSync.notify("Hugo build failed :(");
cb("Hugo build failed");
}
});
}