2016-07-21 17:33:50 +00:00
|
|
|
import gulp from "gulp";
|
2017-08-10 13:39:08 +00:00
|
|
|
import {spawn} from "child_process";
|
|
|
|
import hugoBin from "hugo-bin";
|
2016-07-21 17:33:50 +00:00
|
|
|
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";
|
2017-10-06 17:17:14 +00:00
|
|
|
import watch from "gulp-watch";
|
2016-07-21 17:33:50 +00:00
|
|
|
import webpack from "webpack";
|
|
|
|
import webpackConfig from "./webpack.conf";
|
|
|
|
|
|
|
|
const browserSync = BrowserSync.create();
|
|
|
|
|
2017-08-10 14:17:45 +00:00
|
|
|
// Hugo arguments
|
|
|
|
const hugoArgsDefault = ["-d", "../dist", "-s", "site", "-v"];
|
|
|
|
const hugoArgsPreview = ["--buildDrafts", "--buildFuture"];
|
|
|
|
|
|
|
|
// Development tasks
|
2016-09-01 14:59:24 +00:00
|
|
|
gulp.task("hugo", (cb) => buildSite(cb));
|
2017-08-10 14:17:45 +00:00
|
|
|
gulp.task("hugo-preview", (cb) => buildSite(cb, hugoArgsPreview));
|
2016-07-03 22:07:47 +00:00
|
|
|
|
2017-08-10 14:17:45 +00:00
|
|
|
// Build/production tasks
|
|
|
|
gulp.task("build", ["css", "js"], (cb) => buildSite(cb, [], "production"));
|
|
|
|
gulp.task("build-preview", ["css", "js"], (cb) => buildSite(cb, hugoArgsPreview, "production"));
|
2016-07-03 22:07:47 +00:00
|
|
|
|
2017-08-10 14:22:31 +00:00
|
|
|
// Compile CSS with PostCSS
|
2016-07-21 17:33:50 +00:00
|
|
|
gulp.task("css", () => (
|
|
|
|
gulp.src("./src/css/*.css")
|
2017-04-14 15:40:53 +00:00
|
|
|
.pipe(postcss([cssImport({from: "./src/css/main.css"}), cssnext()]))
|
2016-08-09 22:38:45 +00:00
|
|
|
.pipe(gulp.dest("./dist/css"))
|
2016-07-21 17:33:50 +00:00
|
|
|
.pipe(browserSync.stream())
|
|
|
|
));
|
2016-07-03 22:07:47 +00:00
|
|
|
|
2017-08-10 14:22:31 +00:00
|
|
|
// Compile Javascript
|
2016-07-21 17:33:50 +00:00
|
|
|
gulp.task("js", (cb) => {
|
2016-07-03 22:07:47 +00:00
|
|
|
const myConfig = Object.assign({}, webpackConfig);
|
|
|
|
|
|
|
|
webpack(myConfig, (err, stats) => {
|
2016-07-21 17:33:50 +00:00
|
|
|
if (err) throw new gutil.PluginError("webpack", err);
|
|
|
|
gutil.log("[webpack]", stats.toString({
|
2016-07-03 22:07:47 +00:00
|
|
|
colors: true,
|
|
|
|
progress: true
|
|
|
|
}));
|
2016-07-21 17:33:50 +00:00
|
|
|
browserSync.reload();
|
2016-07-03 22:07:47 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-10 14:22:31 +00:00
|
|
|
// Development server with browsersync
|
2016-07-21 17:33:50 +00:00
|
|
|
gulp.task("server", ["hugo", "css", "js"], () => {
|
|
|
|
browserSync.init({
|
|
|
|
server: {
|
|
|
|
baseDir: "./dist"
|
2016-07-05 18:38:18 +00:00
|
|
|
}
|
2016-07-21 17:33:50 +00:00
|
|
|
});
|
2017-10-06 17:17:14 +00:00
|
|
|
watch("./src/js/**/*.js", () => { gulp.start(["js"]) });
|
|
|
|
watch("./src/css/**/*.css", () => { gulp.start(["css"]) });
|
|
|
|
watch("./site/**/*", () => { gulp.start(["hugo"]) });
|
2016-07-03 22:07:47 +00:00
|
|
|
});
|
2016-09-01 14:59:24 +00:00
|
|
|
|
2017-08-10 14:22:31 +00:00
|
|
|
/**
|
|
|
|
* Run hugo and build the site
|
|
|
|
*/
|
2017-08-10 14:17:45 +00:00
|
|
|
function buildSite(cb, options, environment = "development") {
|
|
|
|
const args = options ? hugoArgsDefault.concat(options) : hugoArgsDefault;
|
|
|
|
|
|
|
|
process.env.NODE_ENV = environment;
|
2016-09-01 14:59:24 +00:00
|
|
|
|
2017-07-30 02:03:01 +00:00
|
|
|
return spawn(hugoBin, args, {stdio: "inherit"}).on("close", (code) => {
|
2016-10-19 05:40:29 +00:00
|
|
|
if (code === 0) {
|
|
|
|
browserSync.reload();
|
|
|
|
cb();
|
|
|
|
} else {
|
|
|
|
browserSync.notify("Hugo build failed :(");
|
|
|
|
cb("Hugo build failed");
|
|
|
|
}
|
2016-09-01 14:59:24 +00:00
|
|
|
});
|
|
|
|
}
|