mirror of
https://github.com/sussy-code/smov.git
synced 2024-12-20 14:37:43 +01:00
41 lines
1 KiB
TypeScript
41 lines
1 KiB
TypeScript
import { globSync } from "glob";
|
|
import { viteStaticCopy } from 'vite-plugin-static-copy'
|
|
import { PluginOption } from "vite";
|
|
import Handlebars from "handlebars";
|
|
import path from "path";
|
|
|
|
export const handlebars = (options: { vars?: Record<string, any> } = {}): PluginOption[] => {
|
|
const files = globSync("src/assets/**/**.hbs");
|
|
|
|
function render(content: string): string {
|
|
const template = Handlebars.compile(content);
|
|
return template(options?.vars ?? {});
|
|
}
|
|
|
|
return [
|
|
{
|
|
name: 'hbs-templating',
|
|
enforce: "pre",
|
|
transformIndexHtml: {
|
|
order: 'pre',
|
|
handler(html) {
|
|
return render(html);
|
|
}
|
|
},
|
|
},
|
|
viteStaticCopy({
|
|
silent: true,
|
|
targets: files.map(file => ({
|
|
src: file,
|
|
dest: '',
|
|
rename: path.basename(file).slice(0, -4), // remove .hbs file extension
|
|
transform: {
|
|
encoding: 'utf8',
|
|
handler(content: string) {
|
|
return render(content);
|
|
}
|
|
}
|
|
}))
|
|
})
|
|
]
|
|
}
|