From 8979bad9a04714d5dc8a11b284a6ba0bd2642d40 Mon Sep 17 00:00:00 2001 From: atomtigerzoo Date: Thu, 10 Aug 2017 15:39:08 +0200 Subject: [PATCH 1/4] Fixed linting errors --- gulpfile.babel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gulpfile.babel.js b/gulpfile.babel.js index ccf48fc..13ecf12 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,6 +1,6 @@ import gulp from "gulp"; -import { spawn } from "child_process"; -import hugoBin from "hugo-bin" +import {spawn} from "child_process"; +import hugoBin from "hugo-bin"; import gutil from "gulp-util"; import postcss from "gulp-postcss"; import cssImport from "postcss-import"; From 4f7c68fd3bcaf5911f330c088b095fd9a3ea34b3 Mon Sep 17 00:00:00 2001 From: atomtigerzoo Date: Thu, 10 Aug 2017 16:17:45 +0200 Subject: [PATCH 2/4] Environment variables added for gulp tasks, renamed defaultArgs --- gulpfile.babel.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 13ecf12..88fff33 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -10,13 +10,18 @@ import webpack from "webpack"; import webpackConfig from "./webpack.conf"; const browserSync = BrowserSync.create(); -const defaultArgs = ["-d", "../dist", "-s", "site", "-v"]; +// 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, ["--buildDrafts", "--buildFuture"])); +gulp.task("hugo-preview", (cb) => buildSite(cb, hugoArgsPreview)); -gulp.task("build", ["css", "js", "hugo"]); -gulp.task("build-preview", ["css", "js", "hugo-preview"]); +// Build/production tasks +gulp.task("build", ["css", "js"], (cb) => buildSite(cb, [], "production")); +gulp.task("build-preview", ["css", "js"], (cb) => buildSite(cb, hugoArgsPreview, "production")); gulp.task("css", () => ( gulp.src("./src/css/*.css") @@ -50,8 +55,10 @@ gulp.task("server", ["hugo", "css", "js"], () => { gulp.watch("./site/**/*", ["hugo"]); }); -function buildSite(cb, options) { - const args = options ? defaultArgs.concat(options) : defaultArgs; +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) { From 6f52084d7b214ed48962be6c6f127b0b216cb8ee Mon Sep 17 00:00:00 2001 From: atomtigerzoo Date: Thu, 10 Aug 2017 16:22:31 +0200 Subject: [PATCH 3/4] Some comments for better understanding --- gulpfile.babel.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 88fff33..d0f3cd0 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -23,6 +23,7 @@ gulp.task("hugo-preview", (cb) => buildSite(cb, hugoArgsPreview)); gulp.task("build", ["css", "js"], (cb) => buildSite(cb, [], "production")); gulp.task("build-preview", ["css", "js"], (cb) => buildSite(cb, hugoArgsPreview, "production")); +// Compile CSS with PostCSS gulp.task("css", () => ( gulp.src("./src/css/*.css") .pipe(postcss([cssImport({from: "./src/css/main.css"}), cssnext()])) @@ -30,6 +31,7 @@ gulp.task("css", () => ( .pipe(browserSync.stream()) )); +// Compile Javascript gulp.task("js", (cb) => { const myConfig = Object.assign({}, webpackConfig); @@ -44,6 +46,7 @@ gulp.task("js", (cb) => { }); }); +// Development server with browsersync gulp.task("server", ["hugo", "css", "js"], () => { browserSync.init({ server: { @@ -55,6 +58,9 @@ gulp.task("server", ["hugo", "css", "js"], () => { gulp.watch("./site/**/*", ["hugo"]); }); +/** + * Run hugo and build the site + */ function buildSite(cb, options, environment = "development") { const args = options ? hugoArgsDefault.concat(options) : hugoArgsDefault; From ec4b9411a8e3732d618b2617a0f1136b24b7e00e Mon Sep 17 00:00:00 2001 From: atomtigerzoo Date: Thu, 10 Aug 2017 16:34:17 +0200 Subject: [PATCH 4/4] Updated Readme to include environment variables info --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 6c26b83..d09dfb1 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,20 @@ You can use ES6 and use both relative imports or import libraries from npm. Any CSS file directly under the `src/css/` folder will get compiled with [PostCSS Next](http://cssnext.io/) to `/dist/css/{filename}.css`. Import statements will be resolved as part of the build +## Environment variables + +Two seperate the development and production *- aka build -* stages, all gulp +tasks run with a node environment variable named either `development` or +`production`. + +You can access the environment variable inside the theme files with +`getenv "NODE_ENV"`. See the following example for a conditional statement: + + {{ if eq (getenv "NODE_ENV") "development" }}You're in development!{{ end }} + +All tasks starting with *build* set the environment variable to `production` - +the other will set it to `development`. + ## Deploying to netlify - Push your clone to your own GitHub repository.