From ce37dcc000947c5b1456be14376c8e5a6fb220b3 Mon Sep 17 00:00:00 2001 From: Benjamin Dobler Date: Wed, 18 Mar 2026 08:57:35 +0100 Subject: [PATCH] feat(vite): add templateTransform hook for template preprocessing Add an optional `templateTransform` callback to the plugin options, allowing external tools to transform Angular template content before compilation while preserving the full HMR pipeline. Closes #149 Co-Authored-By: Claude Opus 4.6 (1M context) --- napi/angular-compiler/vite-plugin/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/napi/angular-compiler/vite-plugin/index.ts b/napi/angular-compiler/vite-plugin/index.ts index 34c8aee52..0bdedb85d 100644 --- a/napi/angular-compiler/vite-plugin/index.ts +++ b/napi/angular-compiler/vite-plugin/index.ts @@ -82,6 +82,9 @@ export interface PluginOptions { * ``` */ angularVersion?: AngularVersion + + /** Optional callback to transform template content before compilation. Applied during both initial build and HMR. */ + templateTransform?: (content: string, filePath: string) => string } // Match all TypeScript files - we'll filter by @Component/@Directive decorator in the handler @@ -160,6 +163,9 @@ export function angular(options: PluginOptions = {}): Plugin[] { if (!content) { try { content = await readFile(templatePath, 'utf-8') + if (options.templateTransform) { + content = options.templateTransform(content, templatePath) + } resourceCache.set(templatePath, content) } catch { console.warn(`Failed to read template: ${templatePath}`) @@ -368,6 +374,9 @@ export function angular(options: PluginOptions = {}): Plugin[] { if (templateUrls.length > 0) { const templatePath = resolve(dir, templateUrls[0]) templateContent = await readFile(templatePath, 'utf-8') + if (options.templateTransform) { + templateContent = options.templateTransform(templateContent, templatePath) + } } else { templateContent = extractInlineTemplate(source) }