oliverbooth.dev/Gulpfile.mjs

100 lines
3.2 KiB
JavaScript
Raw Normal View History

import fs from "fs";
import gulp from "gulp";
import cleanCSS from "gulp-clean-css";
import {deleteSync} from "del";
import noop from "gulp-noop";
import rename from "gulp-rename";
import gulpSass from "gulp-sass";
import * as nodeSass from "sass";
const sass = gulpSass(nodeSass);
import sourcemaps from "gulp-sourcemaps";
import ts from "gulp-typescript";
import webpack from "webpack-stream";
import vinylPaths from "vinyl-paths";
2023-08-04 12:45:10 +01:00
2024-02-26 00:43:57 +00:00
const srcDir = "src";
const tmpDir = "tmp";
2024-02-26 00:43:57 +00:00
const destDir = "OliverBooth/wwwroot";
2024-03-01 17:06:48 +00:00
const isDevelopment = !!process.env.DEVELOPMENT;
2023-08-04 12:45:10 +01:00
function cleanTMP() {
return gulp.src(tmpDir, {allowEmpty: true})
.pipe(vinylPaths(deleteSync));
}
function cleanWWWRoot() {
return gulp.src(destDir, {allowEmpty: true})
.pipe(vinylPaths(deleteSync));
}
2023-08-04 12:45:10 +01:00
function compileSCSS() {
2023-08-04 12:55:16 +01:00
return gulp.src(`${srcDir}/scss/**/*.scss`)
2024-03-01 17:06:48 +00:00
.pipe(isDevelopment ? sourcemaps.init() : noop())
2024-02-26 00:43:57 +00:00
.pipe(sass().on("error", sass.logError))
.pipe(cleanCSS({compatibility: "ie11"}))
.pipe(rename({suffix: ".min"}))
2024-03-01 17:06:48 +00:00
.pipe(isDevelopment ? sourcemaps.write() : noop())
2023-08-04 12:55:16 +01:00
.pipe(gulp.dest(`${destDir}/css`));
}
function compileTS() {
2023-08-10 14:34:01 +01:00
return gulp.src(`${srcDir}/ts/**/*.ts`)
2024-03-01 17:06:48 +00:00
.pipe(isDevelopment ? sourcemaps.init() : noop())
.pipe(ts("tsconfig.json"))
2024-03-01 17:08:23 +00:00
.pipe(isDevelopment ? sourcemaps.write("./", { includeContent: true }) : noop())
.pipe(gulp.dest(`${tmpDir}/js`));
2023-08-10 14:34:01 +01:00
}
function bundleJS(done) {
const tasks = fs.readdirSync(`${tmpDir}/js`, {withFileTypes: true})
.filter(dirent => dirent.isDirectory())
.map(d => bundleDir(d.name));
2024-03-01 17:08:23 +00:00
return gulp.parallel(...tasks, writeSourcemaps)(done);
function bundleDir(directory) {
return () => gulp.src(`${tmpDir}/js/${directory}/${directory}.js`)
2024-03-01 17:06:48 +00:00
.pipe(isDevelopment ? sourcemaps.init() : noop())
2024-03-01 17:08:23 +00:00
.pipe(webpack({mode: "production", output: {filename: `${directory}.min.js`}, devtool: "source-map"}))
.pipe(isDevelopment ? sourcemaps.write("./", { includeContent: true }) : noop())
.pipe(gulp.dest(`${destDir}/js`));
}
function writeSourcemaps() {
return gulp.src(`${destDir}/js/**/*.js`)
.pipe(isDevelopment ? sourcemaps.init({ loadMaps: true }) : noop())
.pipe(isDevelopment ? sourcemaps.write("./", { includeContent: true }) : noop())
.pipe(gulp.dest(`${destDir}/js`));
}
2023-08-04 12:45:10 +01:00
}
2023-08-08 01:20:11 +01:00
function copyJS() {
return gulp.src(`${srcDir}/ts/**/*.js`)
2024-02-26 00:43:57 +00:00
.pipe(rename({suffix: ".min"}))
2023-08-08 01:20:11 +01:00
.pipe(gulp.dest(`${destDir}/js`));
}
function copyCSS() {
return gulp.src(`${srcDir}/scss/**/*.css`)
2024-02-26 00:43:57 +00:00
.pipe(rename({suffix: ".min"}))
2023-08-08 01:20:11 +01:00
.pipe(gulp.dest(`${destDir}/css`));
}
2023-08-06 16:05:44 +01:00
function copyImages() {
return gulp.src(`${srcDir}/img/**/*.*`)
.pipe(gulp.dest(`${destDir}/img`));
}
function exists(path) {
try {
return fs.existsSync(path);
} catch (err) {
return false;
}
}
gulp.task("clean", gulp.parallel(cleanTMP, cleanWWWRoot));
gulp.task("assets", copyImages);
gulp.task("styles", gulp.parallel(compileSCSS, copyCSS));
gulp.task("scripts", gulp.parallel(copyJS, gulp.series(compileTS, bundleJS)));
gulp.task("default", gulp.series("clean", gulp.parallel("styles", "scripts", "assets"), cleanTMP));