vac.dev-experimental-old/gulpfile.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-02-09 05:15:15 +00:00
const gulp = require("gulp");
const gutil = require("gulp-util");
const child = require("child_process");
const browserSync = require("browser-sync").create();
const siteRoot = "_site";
const mainCSS = "src/style.css"; /* Main stylesheet (pre-build) */
const tailwindConfig = "tailwind.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
*/
gulp.task("jekyll-build", function() {
2018-02-09 05:15:15 +00:00
browserSync.notify("Building Jekyll site...");
return child.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
*/
gulp.task("css", ["jekyll-build"], function() {
2018-02-09 05:15:15 +00:00
const atimport = require("postcss-import");
const postcss = require("gulp-postcss");
const tailwindcss = require("tailwindcss");
const purgecss = require("gulp-purgecss");
const autoprefixer = require("gulp-autoprefixer");
const cleancss = require("gulp-clean-css");
browserSync.notify("Compiling CSS...");
return gulp
.src(mainCSS)
.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/"));
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
*/
gulp.task("serve", ["css"], () => {
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(
[
mainCSS,
tailwindConfig,
"**/*.html",
"**/*.md",
"**/*.yml",
"!_site/**/*",
"!node_modules"
],
2018-02-09 05:15:15 +00:00
{ interval: 500 },
["css"]
2018-02-09 05:15:15 +00:00
);
2017-11-09 02:34:49 +00:00
});
2018-02-09 05:15:15 +00:00
gulp.task("default", ["serve"]);