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

103 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-08-10 03:54:58 +00:00
import browserSync from "browser-sync";
import { spawn } from "child_process";
import gulp from "gulp";
import atimport from "postcss-import";
import autoprefixer from "gulp-autoprefixer";
import cleancss from "gulp-clean-css";
import postcss from "gulp-postcss";
import purgecss from "gulp-purgecss";
import tailwindcss from "tailwindcss";
const mainStylesheet = "src/style.css"; /* Main stylesheet (pre-build) */
2018-03-11 05:37:24 +00:00
const siteRoot = "_site";
const tailwindConfig = "tailwind.config.js"; /* Tailwind config */
2017-11-21 01:37:43 +00:00
2018-02-09 05:15:15 +00:00
/**
* Fix Windows compatibility issue
*/
const jekyll = process.platform === "win32" ? "jekyll.bat" : "jekyll";
2017-11-15 12:53:20 +00:00
/**
2017-11-21 01:37:43 +00:00
* Build Jekyll Site
2017-11-15 12:53:20 +00:00
*/
2018-08-10 03:54:58 +00:00
const buildJekyll = () => {
2018-02-09 05:15:15 +00:00
browserSync.notify("Building Jekyll site...");
2018-08-10 03:54:58 +00:00
return spawn(jekyll, ["build"], { stdio: "inherit" });
};
2017-11-09 02:34:49 +00:00
2017-11-15 12:53:20 +00:00
/**
2018-02-09 05:15:15 +00:00
* Custom PurgeCSS Extractor
* https://github.com/FullHuman/purgecss
*/
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g);
}
}
/**
* Compile CSS
2017-11-15 12:53:20 +00:00
*/
2018-08-10 03:54:58 +00:00
const compileStyles = () => {
2018-02-09 05:15:15 +00:00
browserSync.notify("Compiling CSS...");
return gulp
2018-08-10 03:54:58 +00:00
.src(mainStylesheet)
2018-02-09 05:15:15 +00:00
.pipe(postcss([atimport(), tailwindcss(tailwindConfig)]))
.pipe(
purgecss({
content: ["_site/**/*.html"],
extractors: [
{
extractor: TailwindExtractor,
extensions: ["html", "js"]
}
]
})
)
.pipe(
autoprefixer({
browsers: ["last 2 versions"],
cascade: false
})
)
2017-11-09 02:34:49 +00:00
.pipe(cleancss())
2018-02-09 05:15:15 +00:00
.pipe(gulp.dest("assets/css/"))
.pipe(gulp.dest("_site/assets/css/"));
2018-08-10 03:54:58 +00:00
};
const buildSite = gulp.series(buildJekyll, compileStyles);
2017-11-09 02:34:49 +00:00
2017-11-15 12:53:20 +00:00
/**
2017-11-22 22:35:50 +00:00
* Serve site with Browsersync
2017-11-15 12:53:20 +00:00
*/
2018-08-10 03:54:58 +00:00
const 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
}
});
2018-02-09 05:15:15 +00:00
gulp.watch(
[
2018-08-10 03:54:58 +00:00
mainStylesheet,
tailwindConfig,
"**/*.html",
"**/*.md",
"**/*.yml",
"!_site/**/*",
"!node_modules"
],
2018-02-09 05:15:15 +00:00
{ interval: 500 },
2018-08-10 03:54:58 +00:00
buildSite
2018-02-09 05:15:15 +00:00
);
2018-08-10 03:54:58 +00:00
};
const serve = gulp.series(buildSite, startServer);
2017-11-09 02:34:49 +00:00
2018-08-10 03:54:58 +00:00
export default serve;