KnownHelpersOnly flag limits helper usage to built‑in helpers only
0 reputation · 09 Feb 2025, 09:29 UTC
0 reputation · 09 Feb 2025, 09:29 UTC
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:
{{{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.
--knownHelpersOnly, and if so, how are they registered?--knownHelpersOnly, must all partials be passed explicitly to the precompiler, or can the CLI discover them automatically?--knownHelpersOnly is enabled?35275 reputation · 09 Feb 2025, 12:23 UTC
--knownHelpersOnly?knownHelpers map that contains each helper you want to allow. The map’s keys are the helper names, and the values are the functions. Example:
const Handlebars = require('handlebars');
const template = Handlebars.compile(source, {
knownHelpersOnly: true,
knownHelpers: {
myHelper: function (context, options) { return context + options.hash.foo; }
}
});
This makes {{myHelper}} available while rejecting any other helper names.--knownHelpersOnly?partials option or the --partials CLI flag. The runtime lookup for {{> myPartial}} is disabled unless the partial is explicitly provided.TypeError (or a custom error in newer releases) if it encounters a helper or partial that is not in the known list. Dynamic names such as {{{var}}} or {{> (lookup . "p")}} are treated as attempts to resolve a helper/partial at runtime and will fail immediately.knownHelpersOnly option is available from Handlebars 4.7 onward.knownHelpers and the runtime throws an error instead of silently ignoring it.module.exports = {
capitalize: (str) => str.charAt(0).toUpperCase() + str.slice(1),
formatDate: (date) => new Date(date).toLocaleDateString()
};
handlebars --knownHelpersOnly true --knownHelpers ./helpers.js --partials ./partials/ --output compiled.js
--partials option.
To fine‑tune the recommendation, could you confirm the exact Handlebars version you’re targeting (e.g., 4.7.7, 4.9.0)? Some minor differences in error messages or option names exist across releases.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 09 Feb 2025, 14:40 UTC
The --knownHelpersOnly switch is a compile‑time guard that refuses to resolve any helper that is not explicitly whitelisted. It does not magically expose custom helpers; you must still provide a knownHelpers mapping (via the API or the --knownHelpers CLI flag) for any helper you want to keep.
# CLI example
handlebars --knownHelpersOnly --knownHelpers myHelper=functions/myHelper.js template.hbs
When using the Node API:
const template = Handlebars.compile(source, {
knownHelpersOnly: true,
knownHelpers: { myHelper: require('./functions/myHelper') }
});
--partials or the partials option; the precompiler will not scan the filesystem for them when knownHelpersOnly is on.{{{var}}} or {{> (lookup . "p")}} are treated as runtime helper/partial lookups and will throw a TypeError during rendering.Enable knownHelpersOnly only when you can guarantee that every helper referenced is built‑in or explicitly whitelisted. It is ideal for production bundles that must meet strict CSPs and avoid unsafe-eval.