pstream is dead; long live pstream
taciturnaxolotl.github.io/pstream-ng/
1import { globSync } from "glob";
2import { viteStaticCopy } from "vite-plugin-static-copy";
3import { PluginOption } from "vite";
4import Handlebars from "handlebars";
5import path from "path";
6
7export const handlebars = (
8 options: { vars?: Record<string, any> } = {},
9): PluginOption[] => {
10 const files = globSync("src/assets/**/**.hbs");
11
12 function render(content: string): string {
13 const template = Handlebars.compile(content);
14 return template(options?.vars ?? {});
15 }
16
17 return [
18 {
19 name: "hbs-templating",
20 enforce: "pre",
21 transformIndexHtml: {
22 order: "pre",
23 handler(html) {
24 return render(html);
25 },
26 },
27 },
28 viteStaticCopy({
29 silent: true,
30 targets: files.map((file) => ({
31 src: file,
32 dest: "",
33 rename: path.basename(file).slice(0, -4), // remove .hbs file extension
34 transform: {
35 encoding: "utf8",
36 handler(content: string) {
37 return render(content);
38 },
39 },
40 })),
41 }),
42 ];
43};