Question
KnownHelpersOnly flag limits helper usage to built‑in helpers only
Ari Moss
0 reputation · 09 Feb 2025, 09:29 UTC
130.6K views0
KnownHelpersOnly flag limits helper usage to built‑in helpers only
In a repeatable CI/CD pipeline I precompile Handlebars templates with the --knownHelpersOnly flag to eliminate the compiler from production bundles and enable aggressive dead‑code elimination. The goal is a deterministic, minimal runtime bundle that satisfies a strict CSP (no unsafe-eval) and keeps template size under 5 KB gzipped.
Constraints:
- Only built‑in helpers (if, unless, each, with, lookup, log, blockHelperMissing, helperMissing) may be referenced in the templates.
- Partials must be inlined at compile time; runtime partial lookup is disallowed.
- Dynamic helper or partial names (e.g.,
{{{var}}}or{{> (lookup . "p")}}) are incompatible with--knownHelpersOnly.
Uncertainty arises when a template requires custom logic or dynamic references while still needing the minimal bundle.
Specific questions
- Does Handlebars provide a way to declare custom helpers for use with
--knownHelpersOnly, and if so, how are they registered? - When using
--knownHelpersOnly, must all partials be passed explicitly to the precompiler, or can the CLI discover them automatically? - What is the runtime behavior if a template contains a dynamic helper name or partial name while
--knownHelpersOnlyis enabled?