forked from Sigmmma/c20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
103 lines (93 loc) · 2.96 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
import gulp from "gulp";
import del from "del";
import sass from "sass";
import fs from "fs";
import path from "path";
import {paths, baseUrl} from "./build-config.json";
import buildContent from "./src/build";
import runServer from "./src/server";
import esbuild from "esbuild";
//the dist directory may contain outdated content, so start clean
function clean() {
return del(paths.dist);
}
//build the stylesheet from SASS
function assetStyles() {
return new Promise((resolve, reject) => {
sass.render({file: paths.srcStyleEntry, outputStyle: "compressed"}, (err, res) => {
if (err) {
reject(err);
} else {
fs.mkdirSync(paths.distAssets, {recursive: true});
fs.writeFileSync(path.join(paths.distAssets, "style.css"), res.css, "utf8");
resolve(undefined);
}
});
});
}
function scriptBundle(watch: boolean, devMode: boolean) {
return function scriptBundle() {
return esbuild.build({
entryPoints: [paths.srcScriptEntry],
outfile: path.join(paths.distAssets, "main.js"),
bundle: true,
minify: !devMode,
sourcemap: devMode ? "inline" : false,
// todo: can we check what are our users actually using?
// target: ["edge16", "chrome58", "firefox57", "safari11", "ios"],
platform: "browser",
watch: watch ? {
onRebuild(error, result) {
if (error) console.error("JS build failed:", error);
else console.log("JS build succeeded:", result);
},
} : undefined,
});
}
}
//copies any static image or font assets over to the dist directory
function staticAssets() {
return gulp.src(paths.srcStaticAssets)
.pipe(gulp.dest(paths.dist));
}
//any assets which come from our dependencies can be copied over too
function vendorAssets() {
return gulp.src(paths.vendorAssets)
.pipe(gulp.dest(paths.distAssets));
}
//index and render all readme.md files to HTML
async function content() {
await buildContent({
baseUrl,
contentDir: paths.srcContentBase,
outputDir: paths.dist,
noThumbs: !!process.env.C20_NO_THUMBNAILS,
});
}
function watchSources() {
gulp.watch([paths.srcStaticAssets], staticAssets);
gulp.watch([paths.srcStylesAny], assetStyles);
runServer(true);
}
function runStaticServer() {
runServer(false);
}
//composite tasks
const assets = gulp.parallel(staticAssets, assetStyles, vendorAssets);
const buildAll = gulp.series(clean, gulp.parallel(assets, content, scriptBundle(false, false)));
const dev = gulp.series(clean, assets, gulp.parallel(scriptBundle(true, true), watchSources));
const buildAndServe = gulp.series(buildAll, runStaticServer);
//tasks which can be invoked from CLI with `npx gulp <taskname>`
module.exports = {
assets,
//removes the dist directory
clean,
//local development mode
dev,
//final build for publishing content
default: buildAll,
//serves built content assuming it's already been built
server: runStaticServer,
//builds all content then serves it
static: buildAndServe,
};