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

99 lines
2.3 KiB
JavaScript
Raw Normal View History

import autoprefixer from "autoprefixer";
import browserSync from "browser-sync";
2018-12-16 21:27:09 +00:00
import { spawn } from "child_process";
import cssnano from "cssnano";
2018-12-16 21:27:09 +00:00
import { dest, series, src, task, watch } from "gulp";
import gulpif from "gulp-if";
2018-08-10 03:54:58 +00:00
import postcss from "gulp-postcss";
import purgecss from "gulp-purgecss";
2018-12-16 21:27:09 +00:00
import sourcemaps from "gulp-sourcemaps";
import atimport from "postcss-import";
2018-08-10 03:54:58 +00:00
import tailwindcss from "tailwindcss";
2018-12-16 21:27:09 +00:00
const rawStylesheet = "src/style.css";
2018-03-11 05:37:24 +00:00
const siteRoot = "_site";
2018-12-16 21:27:09 +00:00
const cssRoot = `${siteRoot}/assets/css/`;
const tailwindConfig = "tailwind.config.js";
2017-11-21 01:37:43 +00:00
2018-12-16 21:27:09 +00:00
const devBuild =
(process.env.NODE_ENV || "development").trim().toLowerCase() ===
"development";
2018-08-10 03:54:58 +00:00
// Fix for Windows compatibility
const jekyll = process.platform === "win32" ? "jekyll.bat" : "jekyll";
2017-11-09 02:34:49 +00:00
// Custom PurgeCSS Extractor
// https://github.com/FullHuman/purgecss
2018-02-09 05:15:15 +00:00
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/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
const args = ["exec", jekyll, "build"];
2018-12-16 21:27:09 +00:00
if (devBuild) {
args.push("--incremental");
}
return spawn("bundle", args, { stdio: "inherit" });
});
2018-12-16 21:27:09 +00:00
task("processStyles", done => {
browserSync.notify("Compiling styles...");
2018-12-16 21:27:09 +00:00
return src(rawStylesheet)
2018-02-09 05:15:15 +00:00
.pipe(postcss([atimport(), tailwindcss(tailwindConfig)]))
.pipe(gulpif(devBuild, sourcemaps.init()))
2018-12-16 21:27:09 +00:00
.pipe(
gulpif(
!devBuild,
new purgecss({
content: ["_site/**/*.html"],
extractors: [
{
extractor: TailwindExtractor,
extensions: ["html", "js"]
}
]
})
)
)
.pipe(gulpif(!devBuild, postcss([autoprefixer(), cssnano()])))
2018-12-16 21:27:09 +00:00
.pipe(gulpif(devBuild, sourcemaps.write("")))
.pipe(dest(cssRoot));
});
2018-12-16 21:27:09 +00:00
task("startServer", () => {
2017-11-09 02:34:49 +00:00
browserSync.init({
2018-02-09 05:15:15 +00:00
files: [siteRoot + "/**"],
open: "local",
2018-02-09 05:15:15 +00:00
port: 4000,
2017-11-09 02:34:49 +00:00
server: {
baseDir: siteRoot
}
});
watch(
[
2018-12-16 21:27:09 +00:00
rawStylesheet,
tailwindConfig,
"**/*.html",
"**/*.md",
"**/*.yml",
"!_site/**/*",
"!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);