-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgulpfile.ts
167 lines (144 loc) · 4.25 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Product, RubricQuestion, Contributor } from "./src/parsing/types";
import {
loadRubric,
loadProducts,
loadContributors,
} from "./src/parsing/index";
import {
hbsFactory,
getProductPageBuildTasks,
getDirectoryPagesTasks,
getExtensionAPI,
} from "./src/build/utils";
import * as purgecss from "@fullhuman/postcss-purgecss";
const gulp = require("gulp");
const postcss = require("gulp-postcss");
const fs = require("node:fs");
const path = require("node:path");
const through = require("through2");
const rubric: RubricQuestion[] = loadRubric();
const contributors: Contributor[] = loadContributors();
const products: Product[] = loadProducts(rubric, contributors);
gulp.task("clean", async () => {
return fs.rmSync(path.join(__dirname, "dist"), {
recursive: true,
force: true,
});
});
gulp.task("build api", async () => {
const apiVersion = "v2";
if (!fs.existsSync(`./dist/api/${apiVersion}/products`)) {
fs.mkdirSync(`./dist/api/${apiVersion}/products`, { recursive: true });
}
const resolvedDates = await Promise.all(
products.map((product) => product.lastUpdated)
);
const resolvedProducts = products.map((product, i) => {
return {
...product,
lastUpdated: resolvedDates[i],
};
});
resolvedProducts.forEach((product) => {
fs.writeFileSync(
`./dist/api/${apiVersion}/products/${product.slug}.json`,
JSON.stringify(product)
);
});
const api = getExtensionAPI(resolvedProducts);
return gulp
.src(["./src/templates/pages/api/**/*.json"])
.pipe(hbsFactory({ rubric, contributors, resolvedProducts, api }))
.pipe(gulp.dest("./dist/api/"));
});
gulp.task("build general pages", () => {
return gulp
.src(["./src/templates/pages/**/*.hbs", "./src/templates/pages/*.hbs"], {
ignore: [
"./src/templates/pages/product.hbs",
"./src/templates/pages/directory.hbs",
],
})
.pipe(through.obj((file, _, cb) => { // change to .html extension
if (file.isBuffer()) {
file.path = file.path.replace(".hbs", ".html")
}
cb(null, file);
}))
.pipe(hbsFactory({ rubric, contributors, products }))
.pipe(gulp.dest("./dist/"));
});
gulp.task(
"build pages",
gulp.parallel(
...getProductPageBuildTasks(products),
...getDirectoryPagesTasks(products),
"build general pages",
"build api"
)
);
gulp.task("collect dependencies", () => {
return gulp
.src(["./node_modules/lunr/lunr.min.js"])
.pipe(gulp.dest("./dist/static/deps/"));
});
gulp.task("collect static", () => {
return gulp
.src([
"./src/static/**/*",
"./node_modules/@fortawesome/fontawesome-free/**/*.{woff2,woff}",
"!./src/static/**/*.{css,scss}",
])
.pipe(gulp.dest("./dist/static/"));
});
gulp.task("collect root favicon", () => {
return gulp.src(["./src/static/img/*.ico"]).pipe(gulp.dest("./dist/"));
});
gulp.task("collect product icons", () => {
return gulp.src(["./icons/**/*"]).pipe(gulp.dest("./dist/static/icons/"));
});
gulp.task("build css", async () => {
const purgecssConf = purgecss.default({
content: ["./src/**/*.hbs", "./src/**/*.js", "./src/**/*.ts", "./gulpfile.ts"],
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
});
const plugins = [
require("postcss-import"),
require("tailwindcss")("tailwind.config.js"),
require("autoprefixer"),
...(process.env.NODE_ENV === "production" ? [purgecssConf] : []),
];
return gulp
.src(["./src/static/css/base.scss"])
.pipe(postcss(plugins, {
syntax: require("postcss-scss")
}))
.on("error", (err) => console.error(err))
.pipe(through.obj((file, _, cb) => { // change to .css extension
if (file.isBuffer()) {
file.path = file.path.replace(".scss", ".css")
}
cb(null, file);
}))
.on("error", (err) => console.error(err))
.pipe(gulp.dest("./dist/static/css/"));
});
gulp.task(
"default",
gulp.series([
"clean",
"build pages",
"collect dependencies",
"collect static",
"collect product icons",
"collect root favicon",
"build css"
])
);
if (process.env.NODE_ENV === "debug") {
gulp.watch(
["./src/templates/**/*"],
gulp.series("build pages", "collect static")
);
gulp.watch(["./src/**/*.{css,scss}", "build css"]);
}