saudade/gulpfile.js

114 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-12-10 20:37:03 -08:00
const { src, dest, watch, parallel } = require("gulp");
const tap = require("gulp-tap");
const rename = require("gulp-rename");
const browserSync = require("browser-sync").create();
const posthtml = require("gulp-posthtml");
const posthtmlUrls = require("posthtml-urls");
const posthtmlPostcss = require("posthtml-postcss");
const posthtmlModules = require("posthtml-modules");
const sourcemaps = require("gulp-sourcemaps");
const postcss = require("gulp-postcss");
const postcssPresetEnv = require("postcss-preset-env");
const cssnano = require("cssnano");
2023-07-07 16:02:16 -07:00
2023-12-10 20:37:03 -08:00
const posthtmlUrlsFilter = require("./posthtmlUrlsConfig");
const bsConfig = require("./bs-config");
2023-12-10 20:37:03 -08:00
const htmlFiles = [
"**/*.html",
"!**/index.html",
"!**/includes/**/*.html",
"!node_modules/**/*.*",
];
const cssFiles = [
2023-12-10 20:37:03 -08:00
"**/*.css",
"!**/style.css",
"!style/*.css",
"!node_modules/**/*.*",
];
const postcssPlugins = [
2023-07-07 16:02:16 -07:00
postcssPresetEnv({
2023-07-07 15:19:59 -07:00
enableClientSidePolyfills: true,
}),
2023-07-07 16:02:16 -07:00
cssnano({
2023-12-10 20:37:03 -08:00
preset: "default",
}),
];
const posthtmlPlugins = [
2023-07-07 16:02:16 -07:00
posthtmlUrls({
2023-07-07 15:19:59 -07:00
eachURL: (url) => {
2023-12-10 20:37:03 -08:00
if (url.endsWith("?@root")) {
return url.replace("?@root", "");
} else if (url.startsWith("/")) {
return `/${site}${url}`;
2023-07-07 15:19:59 -07:00
} else {
2023-12-10 20:37:03 -08:00
return url;
2023-07-07 15:19:59 -07:00
}
},
2023-07-07 16:02:16 -07:00
filter: posthtmlUrlsFilter,
2023-07-07 15:19:59 -07:00
}),
2023-12-10 20:37:03 -08:00
posthtmlPostcss(postcssPlugins, {}, /^text\/css$/),
];
2023-12-10 20:37:03 -08:00
let site = "";
function html() {
2023-07-07 15:19:59 -07:00
const stream = src(htmlFiles)
2023-12-10 20:37:03 -08:00
.pipe(
tap((file) => {
const path = file.path.split("/");
site = path[path.indexOf("saudade") + 1];
2023-11-27 20:55:40 -08:00
// console.log(path[path.length - 1])
2023-12-10 20:37:03 -08:00
}),
)
.pipe(
posthtml([
2023-11-27 20:55:40 -08:00
...posthtmlPlugins,
posthtmlModules({
2023-12-10 20:37:03 -08:00
plugins: posthtmlPlugins,
2023-11-27 20:55:40 -08:00
}),
2023-12-10 20:37:03 -08:00
]),
)
.pipe(
rename((path) => {
path.basename = "index";
}),
)
.pipe(dest("."));
2023-12-10 20:37:03 -08:00
return stream;
}
function css() {
2023-07-07 15:19:59 -07:00
const stream = src(cssFiles)
2023-12-10 20:37:03 -08:00
.pipe(sourcemaps.init())
.pipe(postcss(postcssPlugins))
.pipe(
rename((path) => {
path.basename = "style";
}),
)
.pipe(sourcemaps.write())
.pipe(dest("."));
2023-07-07 15:19:59 -07:00
if (browserSync.active) {
2023-12-10 20:37:03 -08:00
stream.pipe(browserSync.stream());
2023-07-07 15:19:59 -07:00
}
2023-12-10 20:37:03 -08:00
return stream;
2023-06-14 22:10:06 -07:00
}
exports.watch = function () {
2023-12-10 20:37:03 -08:00
browserSync.init(bsConfig);
2023-06-14 22:10:06 -07:00
2023-12-10 20:37:03 -08:00
watch(cssFiles, { ignoreInitial: false }, css);
2023-11-27 20:55:40 -08:00
2023-12-10 20:37:03 -08:00
watch(htmlFiles, { ignoreInitial: false }, html);
watch("**/index.html").on("all", browserSync.reload);
};
2023-06-14 22:10:06 -07:00
2023-12-10 20:37:03 -08:00
exports.build = parallel(html, css);