All Projects → alexlafroscia → vite-plugin-handlebars

alexlafroscia / vite-plugin-handlebars

Licence: other
Vite support for Handlebars

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to vite-plugin-handlebars

vite-react-ts-tailwind-firebase-starter
Starter using Vite + React + TypeScript + Tailwind CSS. And already set up Firebase(v9), Prettier and ESLint.
Stars: ✭ 108 (+111.76%)
Mutual labels:  vite
vitawind
Install and Setting Tailwindcss automatically for Vite
Stars: ✭ 46 (-9.8%)
Mutual labels:  vite
supabase-schema
Secured & Simple Supabase Schema Visualizer
Stars: ✭ 216 (+323.53%)
Mutual labels:  vite
electron-vite-template
👻 A fast Simple Vite 2, Vue 3 and Electron 13.x template.
Stars: ✭ 56 (+9.8%)
Mutual labels:  vite
vue3.0-template-admin
本项目基于vue3+ElementPlus+Typescript+Vite搭建一套通用的后台管理模板;并基于常见业务场景,抽象出常见功能组件;包括动态菜单,菜单权限、登录、主题切换、国际化、个人中心、表单页、列表页、复制文本、二维码分享等等
Stars: ✭ 500 (+880.39%)
Mutual labels:  vite
escpos-xml
JavaScript library that implements the thermal printer ESC / POS protocol and provides an XML interface for preparing templates for printing.
Stars: ✭ 37 (-27.45%)
Mutual labels:  handlebars
react-antd-low-code
简易版 react 低代码平台
Stars: ✭ 45 (-11.76%)
Mutual labels:  vite
electron-vue-template
A very simplistic Electron + Vue 3 template including ViteJS and Electron Builder
Stars: ✭ 60 (+17.65%)
Mutual labels:  vite
theme-generator
CSS Color Theme Generator based on Numl.Design theme generator & HSLuv color space 🌈
Stars: ✭ 17 (-66.67%)
Mutual labels:  vite
vite-wallet
The Desktop Wallet of Vite
Stars: ✭ 31 (-39.22%)
Mutual labels:  vite
learn-wasm
🎲 Learning WebAssembly
Stars: ✭ 57 (+11.76%)
Mutual labels:  vite
vue-3-stackter
A Vue3 starter project setup with Vite, Vue-meta, Router, Vuex, Eslint, Prettier, Tailwind CSS, and some custom preferences. Also, there is a TypeScript branch of this same setup.
Stars: ✭ 93 (+82.35%)
Mutual labels:  vite
vite-svg-loader
Vite 2.x plugin to load SVG files as Vue components
Stars: ✭ 124 (+143.14%)
Mutual labels:  vite
giraffe
📰 Free news, blogs or magazines application theme, built with Vite & Vuetify created by HeroUI.
Stars: ✭ 46 (-9.8%)
Mutual labels:  vite
passport-examples
A variety of examples using PassportJS with ExpressJS and ReactJS applications
Stars: ✭ 44 (-13.73%)
Mutual labels:  handlebars
node-backend-template
A template for NodeJS backend projects
Stars: ✭ 19 (-62.75%)
Mutual labels:  handlebars
unplugin
Unified plugin system for Vite, Rollup, Webpack, and more
Stars: ✭ 998 (+1856.86%)
Mutual labels:  vite
vite.blog
Vite Tech Blog
Stars: ✭ 13 (-74.51%)
Mutual labels:  vite
vui-vc-next
Vue 3 with Vite Playground - Mobile web UI components - (vue3+vite2).
Stars: ✭ 15 (-70.59%)
Mutual labels:  vite
vite-plugin-sass-dts
This is a plugin that automatically creates a type file when using the CSS module type-safely.
Stars: ✭ 20 (-60.78%)
Mutual labels:  vite

vite-plugin-handlebars

Vite support for Handlebars

Why?

I really like Vite as a simple static site bundler. It can handle bundling multiple HTML files, which is great, but lacks the ability out-of-the-box to share parts of those HTML files.

While a JS framework like React or Vue could be used to solve this problem, this is heavy-handed for a simple site that could be completely pre-rendered without a JS run-time of any kind.

Handlebars provides what we need to be able to stitch together multiple HTML files, interpolate variables, etc.

Installation

Start by installing the package like you would any other

yarn add -D vite-plugin-handlebars

It can then be added to your Vite configuration as a plugin:

// vite.config.js
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [handlebars()],
};

Configuring the plugin is covered later in this guide.

Requirements

  • This plugin is intended to work with Vite 2
  • This plugin requires Node 14 or higher (due to usage of fs/promises)

Configuration

Defining Context

If you want to make use of Handlebars Context to inject variables into your HTML file, you'll need to define their values in the context object passed to the handlebars plugin:

<!-- index.html -->
<h1>{{title}}</h1>
// vite.config.js
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [
    handlebars({
      context: {
        title: 'Hello, world!',
      },
    }),
  ],
};

This will result in <h1>Hello, world!</h1> in your output HTML file.

You can also provide a (asynchronous) function, either as the context key or any of the keys within the object, which will be evaluated to create the value that will be made available inside your page. This function is called with an identifier parameter based on the HTML file path which makes it possible to provide unique data to each HTML page in a multipage application setup.

// vite.config.js
import handlebars from 'vite-plugin-handlebars';

const pageData = {
  '/index.html': {
    title: 'Main Page',
  },
  '/nested/subpage.html': {
    title: 'Sub Page',
  },
};

export default {
  plugins: [
    handlebars({
      context(pagePath) {
        return pageData[pagePath];
      },
    }),
  ],
};

Partials

If you want to make use of partials in your HTML files, you must define the partialDirectory option for the handlebars plugin.

// vite.config.js
import { resolve } from 'path';
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [
    handlebars({
      partialDirectory: resolve(__dirname, 'partials'),
    }),
  ],
};

If you want to use multiple partial folders, an array can be submitted.

Each file in these directories (.html or .hbs) will become registered as a partial. The name of the file is used to invoke it. So, with the above configuration and the following files:

<!-- partials/header.hbs -->
<header><a href="/">My Website</a></header>
<!-- index.html -->
{{> header }}

<h1>The Main Page</h1>

Your output website content would become:

<header><a href="/">My Website</a></header>

<h1>The Main Page</h1>

Make sure to review the quirks section for information on potentially-unexpected behavior.

Helpers

Custom helpers can be registered using the helpers configuration option:

// vite.config.js
import { resolve } from 'path';
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [
    handlebars({
      helpers: {
        capitalize: (value) => value.toUpperCase(),
      },
    }),
  ],
};

For more information on helpers, see the Handlebars documentation.

Other Handlebars Options

All other Handlebars configuration options can also be passed through.

Each of these can also be passed through to the handlebars plugin:

// vite.config.js
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [
    handlebars({
      compileOptions: {
        // Example config option: avoid auto-indenting partials
        preventIndent: true,
      },
      runtimeOptions: {
        // Example config option: define custom private @variables
        data: {
          foo: 'bar',
        },
      },
    }),
  ],
};

Disabling Browser Refresh on Partial Change

By default, any time a partial changes, your browser window will be full reloaded. If you want to disable this behavior, you can set reloadOnPartialChange to false:

// vite.config.js
import handlebars from 'vite-plugin-handlebars';

export default {
  plugins: [
    handlebars({
      reloadOnPartialChange: false,
    }),
  ],
};

Built-In Helpers

resolve-from-root

You can resolve a file path relative to the Vite root using the resolve-from-root helper. This assists with injecting other files, like linking to a CSS file, within a partial.

<!-- partials/head.hbs -->
<link rel="stylesheet" href="{{resolve-from-root 'css/global.css'}}" />

Quirks

  • Assets included in a partial using a relative path will probably not work how you would first expect; the relative path is left alone, making it relative to the output file, not the partial itself. It's recommended that you use the resolve-from-root helper to ensure paths are resolved from the project root, rather than relative to a particular file.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].