perf(bundle): lazy-load the nunjucks template engine (#2535)
Closes #2531. ## Summary The full `nunjucks` templating engine (~226KB) plus `ha-nunjucks` (~46KB) — roughly **272KB, ~13% of the eager entry chunk** — was statically imported and downloaded by every card on initial load, even though templates only apply when a config value contains a `{{ … }}` / `{% … %}` delimiter. Most cards use no templates and never need the engine. This defers the engine behind a dynamic `import('ha-nunjucks/dist')` so it ships in a separate, on-demand chunk instead of the eager `card-*.js`. ## Approach The render path (`TemplateRenderer.renderRecursively`) is **kept synchronous** — it is called from many synchronous hot paths (condition/trigger evaluators, picture-elements rendering, actions, folder matchers), and making it async would be a large, high-risk refactor of the evaluation core. Instead: - **New `src/card-controller/templates/engine.ts`** — a module-level singleton lazy loader (`loadTemplateEngine()` / `getTemplateEngine()`) shared across all `TemplateRenderer` instances, plus a `containsTemplate()` delimiter helper. - **Delimiter gating** — strings without a delimiter never touch the engine (the overwhelming majority of renders). - **Pre-warm at config time** — because every template string originates in the config, a new mandatory `TEMPLATE_ENGINE` initialization aspect loads the engine before first render whenever the config contains a delimiter. This **guarantees no raw `{{ … }}` flash**: content/condition rendering is blocked until the engine is present for template-using cards. Cards without templates never load it. ## Result - nunjucks + ha-nunjucks move out of the eager chunk into a separate chunk fetched only when a card actually uses templates. - No change to the synchronous public render API; condition/trigger evaluation core untouched. ## Tests - New `engine.ts` loader coverage (concurrent load, cached reuse, not-loaded fallback). - The 11 existing test files that render real (delimiter-bearing) templates declare their dependency explicitly via `beforeAll(loadTemplateEngine)` — no global/implicit setup hook. - Full suite green (4764 tests), lint and ts-prune clean, per-file 100% coverage maintained for the affected directories. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016ykWemdkZrgywvC71pHc6c --- _Generated by [Claude Code](https://claude.ai/code/session_016ykWemdkZrgywvC71pHc6c)_
This commit is contained in:
committed by
dermotduffy
parent
b33c034810
commit
ea251ca988
@@ -4,7 +4,6 @@ import { TriggersManager } from '../condition-trigger/triggers/manager.js';
|
||||
import type { TriggerData } from '../condition-trigger/triggers/types.js';
|
||||
import type { Automation, AutomationActions } from '../config/schema/automations.js';
|
||||
import { localize } from '../localize/localize.js';
|
||||
import { TemplateRenderer } from './templates/index.js';
|
||||
import type { CardAutomationsAPI, TaggedAutomation } from './types.js';
|
||||
|
||||
const MAX_NESTED_AUTOMATION_EXECUTIONS = 10;
|
||||
@@ -32,12 +31,13 @@ export class AutomationsManager {
|
||||
}
|
||||
|
||||
public addAutomations(automations: TaggedAutomation[]): void {
|
||||
const context = { templateRenderer: new TemplateRenderer() };
|
||||
const context = { templateRenderer: this._api.getTemplateManager() };
|
||||
for (const automation of automations) {
|
||||
const triggers = new TriggersManager(
|
||||
automation.triggers,
|
||||
this._api.getConditionStateManager(),
|
||||
this._api.getHASSManager(),
|
||||
this._api.getTemplateManager(),
|
||||
);
|
||||
|
||||
// The ongoing `conditions:` block is pull-evaluated at trigger time, so
|
||||
@@ -49,6 +49,27 @@ export class AutomationsManager {
|
||||
);
|
||||
triggers.addListener((data) => this._execute(automation, conditions, data));
|
||||
this._automations.set(automation, triggers);
|
||||
|
||||
// When the card is already initialized (e.g. a runtime automation
|
||||
// addition via configuration override), subscribe immediately. For
|
||||
// "static" automations the InitializationManager subscribes every
|
||||
// automation once initialization completes so that the trigger evaluators
|
||||
// baseline their initial pre-trigger value against a card whose template
|
||||
// renderer has loaded.
|
||||
if (this._api.getInitializationManager().isInitializedMandatory()) {
|
||||
triggers.subscribe();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe every registered automation's triggers. Called by the
|
||||
* InitializationManager when initialization completes (idempotent, so
|
||||
* automations subscribed eagerly on a runtime config change are unaffected).
|
||||
*/
|
||||
public subscribe(): void {
|
||||
for (const triggers of this._automations.values()) {
|
||||
triggers.subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user