example | ||
src | ||
.gitignore | ||
biome.json | ||
build.js | ||
bun.lockb | ||
bunfig.toml | ||
license | ||
license.banner | ||
package.json | ||
readme.md | ||
tsconfig.json | ||
tsconfig.sourcemaps.json |
bun-handlebars-loader
bun plugin to load handlebar files
installation
The package is available from Forgejo Packages. To add the registry, follow these steps, under the Bun section.
Then, run bun add @stagenterprises/bun-handlebars-loader
.
usage
An example of this can be found in example/
Load the plugin by creating loadPlugins.ts
in your project with the following contents:
import { plugin } from "bun";
import handlebarsLoader from "@stagenterprises/bun-handlebars-loader";
await plugin(handlebarsLoader());
Then, add it to the preload by adding the following line to the top of your bunfig.toml
(top-level):
preload = [ "./loadPlugins.ts" ]
Then, import any .hbs
or .handlebars
file, and it will be processed using handlebars.precompile
. You'll have to call handlebars.template
to be able to use the template. For example:
import partial from "./partial.x.hbs";
import template from "./template.hbs";
import handlebars from "handlebars";
handlebars.registerPartial("partial", handlebars.template(partial))
const compiled = handlebars.template(template);
console.log(compiled({ name: "world" }));
If using TypeScript, you'll also want to include type definitions. Create a file named references.d.ts
anywhere in your project, and put the following in it:
declare module "*.hbs" {
export interface PrecompiledTemplate {
compiler: [number, string];
main: (...args: any[]) => string;
useData: boolean;
}
export const precompiledTemplate: PrecompiledTemplate;
export default precompiledTemplate;
}
declare module "*.handlebars" {
export * from "*.hbs";
export { default } from "*.hbs";
}
options
You can specify options in your loadPlugins.ts
by passing an argument to handlebarsLoader()
. The following options are available
Name | Type | Default | Description |
---|---|---|---|
.handlebars |
object |
{ assumeObjects: true } |
Options to pass through to handlebars.precompile . See the Handlebars doucmentation for reference |
.exts |
string[] |
[ "hbs, "handlebars" ] |
File extensions the plugin should run on |