-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
120 lines (106 loc) · 3.56 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
/**
* Copyright 2019 SME Texaplex Virtual Chapter Contributors. All Rights Reserved.
* Copyright 2018 SME Virtual Network Contributors. All Rights Reserved.
* See LICENSE in the repository root for license information.
* =============================================================================
*/
import BrowserSync from "browser-sync";
import { spawn } from "child_process";
import del from "del";
import gulp from "gulp";
import log from "fancy-log";
import webpack from "webpack";
import webpackConfig from "./webpack.config.babel";
const browserSync = BrowserSync.create();
const IS_PRODUCTION = process.env.NODE_ENV === "production";
log.warn("[Gulp] build mode: ", IS_PRODUCTION ? "PRODUCTION" : "DEVELOPMENT");
let suppressHugoErrors = false;
const defaultHugoArgs = ["-d", "docs", "--config", "config.toml"];
// Sitemaps need the absolute URL (along with the scheme) to be compatible with
// major search engines. This changes the `baseURL` Hugo configuration setting
// prior to deployment.
if (process.env.DEPLOY_BASE_URL) {
defaultHugoArgs.push("-b");
defaultHugoArgs.push(process.env.DEPLOY_BASE_URL);
}
/**
* WEBPACK BUNDLE TASK
* -----------------------------------------------------------------------------
*/
gulp.task("bundle", callback => {
webpack(webpackConfig, (err, stats) => {
if (err || stats.hasErrors()) {
log.error("Bundle error: ", err);
callback();
}
log.info("[Webpack] running...");
log.info(
stats.toString({
chunks: false,
colors: true
})
);
browserSync.reload();
callback();
});
});
/**
* COPY IMAGES TASK
* -----------------------------------------------------------------------------
*/
gulp.task("copy:images", () => gulp.src(["frontend/images/**", "frontend/meta/**"]).pipe(gulp.dest("docs")));
/**
* COPY CONFIGS TASK
* -----------------------------------------------------------------------------
*/
gulp.task("copy:configs", () => gulp.src(["frontend/**/*.json"]).pipe(gulp.dest("docs")));
/**
* CLEAN TASK
* -----------------------------------------------------------------------------
*/
gulp.task("clean", () => del(["docs/**", "!docs", "!docs/CNAME"]));
/**
* RUN DEVELOPMENT SERVER TASK
* -----------------------------------------------------------------------------
* Run the development server with Browsersync. Gulp will watch for source file
* changes and Browsersync will reload the browser as necessary.
*/
gulp.task("dev-server", () => {
suppressHugoErrors = true;
browserSync.init({
host: "lvh.me",
port: 3000,
open: "external",
server: {
baseDir: "./docs"
}
});
gulp.watch(["*.toml", "./archetypes/**/*", "./content/**/*", "./layouts/**/*"], gulp.series("hugo"));
gulp.watch(["./frontend/**/*"], gulp.series("bundle", "copy:images", "copy:configs"));
});
/**
* HUGO BUILD TASK
* -----------------------------------------------------------------------------
* Builds the Hugo static site.
*/
gulp.task("hugo", done =>
spawn("hugo", defaultHugoArgs, { stdio: "inherit" }).on("close", code => {
if (suppressHugoErrors || code === 0) {
browserSync.reload();
done();
} else {
log.error("Hugo build task failed.");
done();
}
})
);
/**
* BUILD TASK
* -----------------------------------------------------------------------------
*/
gulp.task("build", gulp.series("clean", "bundle", "hugo", "copy:images", "copy:configs"));
/**
* LOCAL SERVER RUN TASK
* -----------------------------------------------------------------------------
*/
gulp.task("serve", gulp.series("build", "dev-server"));