vac.dev-experimental-old/gulpfile.babel.js

99 lines
2.4 KiB
JavaScript
Raw Normal View History

import autoprefixer from "autoprefixer";
import browserSync from "browser-sync";
import cssnano from "cssnano";
2018-12-16 21:27:09 +00:00
import { dest, series, src, task, watch } from "gulp";
2018-08-10 03:54:58 +00:00
import postcss from "gulp-postcss";
2020-01-25 18:35:18 +00:00
import purgecss from "@fullhuman/postcss-purgecss";
2018-12-16 21:27:09 +00:00
import atimport from "postcss-import";
2018-08-10 03:54:58 +00:00
import tailwindcss from "tailwindcss";
2020-01-25 18:08:59 +00:00
const SITE_ROOT = "./_site";
const POST_BUILD_STYLESHEET = `${SITE_ROOT}/assets/css/`;
const PRE_BUILD_STYLESHEET = "./src/style.css";
const TAILWIND_CONFIG = "./tailwind.config.js";
2017-11-21 01:37:43 +00:00
// Fix for Windows compatibility
2020-01-25 18:40:26 +00:00
const isWindowsPlatform = process.platform === "win32";
const jekyll = isWindowsPlatform ? "jekyll.bat" : "jekyll";
2020-01-25 18:41:17 +00:00
const spawn = isWindowsPlatform
? require("win-spawn")
: require("child_process").spawn;
2017-11-09 02:34:49 +00:00
const isDevelopmentBuild = process.env.NODE_ENV === "development";
2020-01-25 18:35:18 +00:00
// Custom PurgeCSS Extractor for Tailwind CSS
const purgeFromTailwind = content => content.match(/[\w-/:]+(?<!:)/g) || [];
2018-02-09 05:15:15 +00:00
2018-12-16 21:27:09 +00:00
task("buildJekyll", () => {
browserSync.notify("Building Jekyll site...");
2018-12-16 21:27:09 +00:00
2020-01-25 18:40:26 +00:00
const args = ["exec", jekyll, "build"];
2020-01-25 18:08:59 +00:00
if (isDevelopmentBuild) {
2018-12-16 21:27:09 +00:00
args.push("--incremental");
}
return spawn("bundle", args, { stdio: "inherit" });
});
2020-01-25 18:08:59 +00:00
task("processStyles", () => {
2018-12-16 21:27:09 +00:00
browserSync.notify("Compiling styles...");
2020-01-25 18:08:59 +00:00
return src(PRE_BUILD_STYLESHEET)
2018-12-16 21:27:09 +00:00
.pipe(
2020-01-25 18:35:18 +00:00
postcss([
atimport(),
tailwindcss(TAILWIND_CONFIG),
...(!isDevelopmentBuild
? [
purgecss({
content: [`${SITE_ROOT}/**/*.html`],
extractors: [
{
extractor: purgeFromTailwind,
extensions: ["html", "js"]
}
]
}),
autoprefixer(),
cssnano()
]
: [])
])
2018-12-16 21:27:09 +00:00
)
2020-01-25 18:08:59 +00:00
.pipe(dest(POST_BUILD_STYLESHEET));
2018-12-16 21:27:09 +00:00
});
2018-12-16 21:27:09 +00:00
task("startServer", () => {
2017-11-09 02:34:49 +00:00
browserSync.init({
2020-01-25 18:08:59 +00:00
files: [SITE_ROOT + "/**"],
open: "local",
2018-02-09 05:15:15 +00:00
port: 4000,
2017-11-09 02:34:49 +00:00
server: {
2020-01-25 18:08:59 +00:00
baseDir: SITE_ROOT,
serveStaticOptions: {
extensions: ["html"]
}
2017-11-09 02:34:49 +00:00
}
});
watch(
[
2019-07-17 03:48:16 +00:00
"**/*.css",
"**/*.html",
"**/*.js",
"**/*.md",
"**/*.markdown",
2019-07-17 03:45:00 +00:00
"!_site/**/*",
2019-07-17 03:48:16 +00:00
"!node_modules/**/*"
2018-12-16 21:27:09 +00:00
],
{ interval: 500 },
2018-08-10 03:54:58 +00:00
buildSite
2018-02-09 05:15:15 +00:00
);
});
2018-12-16 21:27:09 +00:00
const buildSite = series("buildJekyll", "processStyles");
2017-11-09 02:34:49 +00:00
2018-12-16 21:27:09 +00:00
exports.serve = series(buildSite, "startServer");
exports.default = series(buildSite);