bun plugin to load handlebar files
Find a file
2024-11-23 16:08:28 -05:00
example feat: initial commit 2024-11-04 19:30:23 -05:00
src feat: initial commit 2024-11-04 19:30:23 -05:00
.gitignore Initial commit 2024-10-31 21:00:16 +01:00
biome.json feat: initial commit 2024-11-04 19:30:23 -05:00
build.js fix~cfg: build script - Bun.build({external}) must be an array not a single string value 2024-11-04 19:51:10 -05:00
bun.lockb feat: initial commit 2024-11-04 19:30:23 -05:00
bunfig.toml feat~cfg: bunfig.toml - token authorization from env var 2024-11-04 20:04:58 -05:00
license Initial commit 2024-10-31 21:00:16 +01:00
license.banner Initial commit 2024-10-31 21:00:16 +01:00
package.json fix~dep: @types/node should be devdep not dep 2024-11-11 07:10:29 +01:00
readme.md feat(doc/readme): wiki link 2024-11-23 16:08:28 -05:00
tsconfig.json feat: initial commit 2024-11-04 19:30:23 -05:00
tsconfig.sourcemaps.json Initial commit 2024-10-31 21:00:16 +01:00

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