-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
123 lines (104 loc) · 3.28 KB
/
gulpfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eslint @typescript-eslint/explicit-module-boundary-types: "off"
-- Functions are only exported from this file to enable them as gulp tasks */
import path from 'path';
import browserSync from 'browser-sync';
import del from 'del';
import * as gulp from 'gulp';
import autoprefixer from 'gulp-autoprefixer';
import pug from 'gulp-pug';
import sass from 'gulp-sass';
import nodeSass from 'node-sass';
import {rollup} from 'rollup';
import {terser} from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import {PATHS, OPTIONS} from './config';
import {cleanCss} from './lib/gulp-clean-css';
import {resizeImages} from './lib/gulp-resize-images';
// The node-sass docs recommend setting the `compiler` property explicitly, but
// the types defined for the package don't define that property.
// See: https://github.com/dlmanning/gulp-sass#basic-usage
type GulpSass = typeof sass & {compiler: typeof nodeSass};
(sass as GulpSass).compiler = nodeSass;
/** Clean the output directory. */
export function clean() {
return del([PATHS.dest]);
}
/** Copy static files */
export function staticFiles() {
return gulp.src(PATHS.static)
.pipe(gulp.dest(PATHS.dest));
}
/** Compile HTML */
export function html() {
return gulp.src(PATHS.html)
.pipe(pug(OPTIONS.gulpPug))
.pipe(gulp.dest(PATHS.dest));
}
/** Compile CSS */
export function css() {
return gulp.src(PATHS.css, {base: PATHS.srcRoot})
.pipe(sass(OPTIONS.gulpSass).on('error', sass.logError.bind(sass)))
.pipe(autoprefixer(OPTIONS.autoprefixer))
.pipe(cleanCss(OPTIONS.cleanCss))
.pipe(gulp.dest(PATHS.dest));
}
/** Compile JavaScript */
export function js() {
return rollup({
input: PATHS.js,
plugins: [
typescript(OPTIONS.rollupTypescript),
terser(),
],
}).then((bundle) => bundle.write({
dir: path.join(PATHS.dest, 'js'),
format: 'iife',
}));
}
/** Process hero image */
export function hero() {
return gulp.src(PATHS.heroImage, {base: PATHS.srcRoot})
.pipe(resizeImages(OPTIONS.resizeImagesHero))
.pipe(gulp.dest(PATHS.dest));
}
/** Process featured project images */
export function featured() {
return gulp.src(PATHS.featuredImages, {base: PATHS.srcRoot})
.pipe(resizeImages(OPTIONS.resizeImagesFeatured))
.pipe(gulp.dest(PATHS.dest));
}
const imageTasks = [
hero,
featured,
];
const allBuildTasks = [
clean,
gulp.parallel(staticFiles, html, css, js, ...imageTasks),
];
function startBrowserSync() {
const server = browserSync.create();
server.init({
server: `./${PATHS.dest}`,
open: false,
});
server.watch(PATHS.dest, {}, (_event, filePath) => {
// `filePath` is incorrectly typed as `fs.Stats`
server.reload(filePath as unknown as string);
});
gulp.watch(PATHS.srcRoot).on('all', (_event, filePath: string) => {
const ext = path.extname(filePath).toLowerCase();
if (ext === '.pug') {
html();
} else if (ext === '.scss') {
css();
} else if (ext === '.ts') {
js(); // eslint-disable-line @typescript-eslint/no-floating-promises
} else {
staticFiles();
imageTasks.forEach((task) => task());
}
});
}
export const images = gulp.parallel(...imageTasks);
export const build = gulp.series(...allBuildTasks);
export const watch = gulp.series(...allBuildTasks, startBrowserSync);