mirror of
https://github.com/movie-web/movie-web.git
synced 2024-11-11 01:35:08 +01:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 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);
|
||
|
}
|
||
|
}
|
||
|
}))
|
||
|
})
|
||
|
]
|
||
|
}
|