Skip to content

Commit e698f76

Browse files
feat(vite): add templateTransform hook for template preprocessing (#150)
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) <noreply@anthropic.com>
1 parent f7254aa commit e698f76

File tree

1 file changed

+9
-0
lines changed
  • napi/angular-compiler/vite-plugin

1 file changed

+9
-0
lines changed

napi/angular-compiler/vite-plugin/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export interface PluginOptions {
8282
* ```
8383
*/
8484
angularVersion?: AngularVersion
85+
86+
/** Optional callback to transform template content before compilation. Applied during both initial build and HMR. */
87+
templateTransform?: (content: string, filePath: string) => string
8588
}
8689

8790
// Match all TypeScript files - we'll filter by @Component/@Directive decorator in the handler
@@ -160,6 +163,9 @@ export function angular(options: PluginOptions = {}): Plugin[] {
160163
if (!content) {
161164
try {
162165
content = await readFile(templatePath, 'utf-8')
166+
if (options.templateTransform) {
167+
content = options.templateTransform(content, templatePath)
168+
}
163169
resourceCache.set(templatePath, content)
164170
} catch {
165171
console.warn(`Failed to read template: ${templatePath}`)
@@ -368,6 +374,9 @@ export function angular(options: PluginOptions = {}): Plugin[] {
368374
if (templateUrls.length > 0) {
369375
const templatePath = resolve(dir, templateUrls[0])
370376
templateContent = await readFile(templatePath, 'utf-8')
377+
if (options.templateTransform) {
378+
templateContent = options.templateTransform(templateContent, templatePath)
379+
}
371380
} else {
372381
templateContent = extractInlineTemplate(source)
373382
}

0 commit comments

Comments
 (0)