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";
|
2018-08-21 18:42:12 +00:00
|
|
|
import log from "fancy-log";
|
|
|
|
import pluginError from "plugin-error";
|
2017-10-24 21:35:55 +00:00
|
|
|
import flatten from "gulp-flatten";
|
2016-07-21 17:33:50 +00:00
|
|
|
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";
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-14 14:16:17 +00:00
|
|
|
// Run server tasks
|
|
|
|
gulp.task("server", ["hugo", "css", "js", "fonts"], (cb) => runServer(cb));
|
|
|
|
gulp.task("server-preview", ["hugo-preview", "css", "js", "fonts"], (cb) => runServer(cb));
|
|
|
|
|
2017-08-10 14:17:45 +00:00
|
|
|
// Build/production tasks
|
2017-10-24 21:35:55 +00:00
|
|
|
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
|
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) => {
|
2018-08-21 18:42:12 +00:00
|
|
|
if (err) throw new pluginError("webpack", err);
|
|
|
|
log(`[webpack] ${stats.toString({
|
2016-07-03 22:07:47 +00:00
|
|
|
colors: true,
|
|
|
|
progress: true
|
2018-08-21 18:42:12 +00:00
|
|
|
})}`);
|
2016-07-21 17:33:50 +00:00
|
|
|
browserSync.reload();
|
2016-07-03 22:07:47 +00:00
|
|
|
cb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-10-24 21:35:55 +00:00
|
|
|
// 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
|
2018-05-14 14:16:17 +00:00
|
|
|
function runServer() {
|
2016-07-21 17:33:50 +00:00
|
|
|
browserSync.init({
|
|
|
|
server: {
|
|
|
|
baseDir: "./dist"
|
2016-07-05 18:38:18 +00:00
|
|
|
}
|
2016-07-21 17:33:50 +00:00
|
|
|
});
|
2018-02-19 11:29:30 +00:00
|
|
|
gulp.watch("./src/js/**/*.js", ["js"]);
|
|
|
|
gulp.watch("./src/css/**/*.css", ["css"]);
|
|
|
|
gulp.watch("./src/fonts/**/*", ["fonts"]);
|
|
|
|
gulp.watch("./site/**/*", ["hugo"]);
|
2018-05-14 14:16:17 +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
|
|
|
});
|
|
|
|
}
|