This repository has been archived on 2020-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
victor-hugo/gulpfile.babel.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

import gulp from "gulp";
2017-08-10 13:39:08 +00:00
import {spawn} from "child_process";
import hugoBin from "hugo-bin";
import gutil from "gulp-util";
import flatten from "gulp-flatten";
import postcss from "gulp-postcss";
import cssImport from "postcss-import";
import cssnext from "postcss-cssnext";
import BrowserSync from "browser-sync";
import watch from "gulp-watch";
import webpack from "webpack";
import webpackConfig from "./webpack.conf";
const browserSync = BrowserSync.create();
// 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));
2016-07-03 22:07:47 +00:00
// Build/production tasks
gulp.task("build", ["css", "js", "fonts"], (cb) => buildSite(cb, [], "production"));
gulp.task("build-preview", ["css", "js", "fonts"], (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
gulp.task("css", () => (
gulp.src("./src/css/*.css")
.pipe(postcss([cssImport({from: "./src/css/main.css"}), cssnext()]))
.pipe(gulp.dest("./dist/css"))
.pipe(browserSync.stream())
));
2016-07-03 22:07:47 +00:00
2017-08-10 14:22:31 +00:00
// Compile Javascript
gulp.task("js", (cb) => {
2016-07-03 22:07:47 +00:00
const myConfig = Object.assign({}, webpackConfig);
webpack(myConfig, (err, stats) => {
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
}));
browserSync.reload();
2016-07-03 22:07:47 +00:00
cb();
});
});
// Move all fonts in a flattened directory
gulp.task('fonts', () => (
gulp.src("./src/fonts/**/*")
.pipe(flatten())
.pipe(gulp.dest("./dist/fonts"))
.pipe(browserSync.stream())
));
2017-08-10 14:22:31 +00:00
// Development server with browsersync
gulp.task("server", ["hugo", "css", "js", "fonts"], () => {
browserSync.init({
server: {
baseDir: "./dist"
}
});
watch("./src/js/**/*.js", () => { gulp.start(["js"]) });
watch("./src/css/**/*.css", () => { gulp.start(["css"]) });
watch("./src/fonts/**/*", () => { gulp.start(["fonts"]) });
watch("./site/**/*", () => { gulp.start(["hugo"]) });
2016-07-03 22:07:47 +00:00
});
2017-08-10 14:22:31 +00:00
/**
* 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 spawn(hugoBin, args, {stdio: "inherit"}).on("close", (code) => {
if (code === 0) {
browserSync.reload();
cb();
} else {
browserSync.notify("Hugo build failed :(");
cb("Hugo build failed");
}
});
}