-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.ts
More file actions
666 lines (578 loc) · 23.2 KB
/
index.ts
File metadata and controls
666 lines (578 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**
* Oxc Angular Vite Plugin
*
* A simplified Vite plugin for Angular that uses Oxc's Rust-based compiler.
* This plugin handles:
* - Template compilation
* - Style processing
* - Hot Module Replacement (HMR)
*/
import { watch } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { ServerResponse } from 'node:http'
import { dirname, resolve } from 'node:path'
import { createDebug } from 'obug'
import type { Plugin, ResolvedConfig, ViteDevServer, Connect } from 'vite'
import { preprocessCSS, normalizePath } from 'vite'
// Debug loggers - enable with DEBUG=vite:oxc-angular:*
const debugHmr = createDebug('vite:oxc-angular:hmr')
const debugTransform = createDebug('vite:oxc-angular:transform')
import {
transformAngularFile,
extractComponentUrls,
encapsulateStyle,
compileForHmrSync,
type TransformOptions,
type ResolvedResources,
type AngularVersion,
} from '#binding'
import { buildOptimizerPlugin } from './angular-build-optimizer-plugin.js'
import { jitPlugin } from './angular-jit-plugin.js'
import { angularLinkerPlugin } from './angular-linker-plugin.js'
import { ssrManifestPlugin } from './angular-ssr-manifest-plugin.js'
/**
* Plugin options for the Angular Vite plugin.
*/
export interface PluginOptions {
/** Path to tsconfig.json (used for file discovery, not TypeScript compilation). */
tsconfig?: string
/** Workspace root directory. */
workspaceRoot?: string
/** Extension for inline styles (css, scss, etc.). */
inlineStylesExtension?: string
/** Enable JIT compilation mode. */
jit?: boolean
/** Enable live reload / HMR. */
liveReload?: boolean
/** Enable source map generation. */
sourceMap?: boolean | { scripts?: boolean; vendor?: boolean }
/** Enable zoneless mode. */
zoneless?: boolean
/** File replacements (for environment files). */
fileReplacements?: Array<{ replace: string; with: string }>
/** Path to main.server.ts for SSR manifest generation. Auto-detected from src/main.server.ts if not specified. */
ssrEntry?: string
/**
* Angular version to target.
*
* Controls which runtime instructions are emitted. For example, Angular 19
* uses `ɵɵtemplate` for `@if`/`@switch` blocks, while Angular 20+ uses
* `ɵɵconditionalCreate`/`ɵɵconditionalBranchCreate`.
*
* When not set, assumes latest Angular version (v20+ behavior).
*
* @example
* ```ts
* angular({ angularVersion: { major: 19, minor: 0, patch: 0 } })
* ```
*/
angularVersion?: AngularVersion
}
// Match all TypeScript files - we'll filter by @Component/@Directive decorator in the handler
const ANGULAR_TS_REGEX = /\.tsx?$/
const ANGULAR_COMPONENT_PREFIX = '@ng/component'
/**
* Create the Angular Vite plugin.
*/
export function angular(options: PluginOptions = {}): Plugin[] {
const workspaceRoot = options.workspaceRoot ?? process.cwd()
// Process file replacements
let fileReplacements: Record<string, string> | undefined
if (options.fileReplacements) {
fileReplacements = {}
for (const replacement of options.fileReplacements) {
const from = resolve(workspaceRoot, replacement.replace)
const to = resolve(workspaceRoot, replacement.with)
fileReplacements[from] = to
}
}
// Resolve options
const pluginOptions = {
workspaceRoot,
inlineStylesExtension: options.inlineStylesExtension ?? 'css',
jit: options.jit ?? false,
liveReload: options.liveReload ?? true,
sourceMap:
typeof options.sourceMap === 'boolean'
? options.sourceMap
: (options.sourceMap?.scripts ?? true),
zoneless: options.zoneless ?? false,
fileReplacements,
angularVersion: options.angularVersion,
}
let resolvedConfig: ResolvedConfig
let viteServer: ViteDevServer | undefined
let watchMode = false
// Track component IDs for HMR
const componentIds = new Map<string, string>()
// Reverse mapping: resource file path → component file path
const resourceToComponent = new Map<string, string>()
// Cache for resolved resources
const resourceCache = new Map<string, string>()
// Track component files with pending HMR updates (set by fs.watch, checked by HMR endpoint)
const pendingHmrUpdates = new Set<string>()
/**
* Resolve external template/style URLs and read their contents.
*/
async function resolveResources(
code: string,
id: string,
): Promise<{ resources: ResolvedResources; dependencies: string[] }> {
const { templateUrls, styleUrls } = await extractComponentUrls(code, id)
const dir = dirname(id)
const dependencies: string[] = []
// NAPI-RS expects plain objects for HashMap, not JavaScript Maps
const templates: Record<string, string> = {}
const styles: Record<string, string[]> = {}
// Resolve templates
for (const templateUrl of templateUrls) {
const templatePath = resolve(dir, templateUrl)
dependencies.push(templatePath)
let content = resourceCache.get(templatePath)
if (!content) {
try {
content = await readFile(templatePath, 'utf-8')
resourceCache.set(templatePath, content)
} catch {
console.warn(`Failed to read template: ${templatePath}`)
continue
}
}
templates[templateUrl] = content
}
// Resolve styles
for (const styleUrl of styleUrls) {
const stylePath = resolve(dir, styleUrl)
dependencies.push(stylePath)
let content = resourceCache.get(stylePath)
if (!content) {
try {
content = await readFile(stylePath, 'utf-8')
// Preprocess styles (SCSS, Less, etc.)
if (resolvedConfig) {
try {
const processed = await preprocessCSS(content, stylePath, resolvedConfig as any)
content = processed.code
} catch (e) {
console.warn(`Failed to preprocess style: ${stylePath}`, e)
}
}
resourceCache.set(stylePath, content)
} catch {
console.warn(`Failed to read style: ${stylePath}`)
continue
}
}
styles[styleUrl] = [content]
}
// Note: NAPI-RS HashMap binds to plain objects at runtime, despite Map type in index.d.ts
return { resources: { templates, styles } as unknown as ResolvedResources, dependencies }
}
/**
* Main Angular plugin for file transformation.
*/
function angularPlugin(): Plugin {
return {
name: '@oxc-angular/vite',
async config(_, { command }) {
watchMode = command === 'serve'
return {
optimizeDeps: {
include: ['rxjs/operators', 'rxjs'],
exclude: ['@angular/platform-server'],
},
...(options.tsconfig && {
build: {
rolldownOptions: {
tsconfig: options.tsconfig,
},
},
}),
}
},
configResolved(config) {
resolvedConfig = config
},
// Safety net: resolve @ng/component virtual modules in SSR context.
// The browser serves these via HTTP middleware, but Vite's module runner
// (used by Nitro/SSR) resolves through plugin hooks instead.
resolveId(source, _importer, options) {
if (options?.ssr && source.includes(ANGULAR_COMPONENT_PREFIX)) {
// Return as virtual module (with \0 prefix per Vite convention)
return `\0${source}`
}
},
load(id, options) {
if (options?.ssr && id.startsWith('\0') && id.includes(ANGULAR_COMPONENT_PREFIX)) {
// Return empty module — SSR doesn't need HMR update modules
return 'export default undefined;'
}
},
configureServer(server) {
viteServer = server
// Track watched template files
const watchedTemplates = new Set<string>()
// Use fs.watch for template files instead of Vite's watcher
// This bypasses Vite's internal handling which causes full reloads
const watchTemplateFile = (file: string) => {
if (watchedTemplates.has(file)) return
watchedTemplates.add(file)
// Dynamically unwatch from Vite's watcher - this is more precise than static glob patterns
// and handles any file naming convention (app.css, app.component.css, styles.scss, etc.)
server.watcher.unwatch(file)
debugHmr('unwatched from Vite, adding custom watch: %s', file)
watch(file, { persistent: true }, async (eventType) => {
if (eventType === 'change') {
const normalizedFile = normalizePath(file)
debugHmr('resource file change: %s', normalizedFile)
// Invalidate resource cache
resourceCache.delete(normalizedFile)
// Handle template/style file changes for HMR
if (pluginOptions.liveReload) {
const componentFile = resourceToComponent.get(normalizedFile)
if (componentFile && componentIds.has(componentFile)) {
debugHmr('resource change triggers HMR: %s -> %s', normalizedFile, componentFile)
// Mark this component as having a pending HMR update so the
// HMR endpoint serves the update module instead of an empty response.
pendingHmrUpdates.add(componentFile)
// Send HMR update event
const componentId = `${componentFile}@${componentIds.get(componentFile)}`
const encodedId = encodeURIComponent(componentId)
debugHmr('sending WS event: id=%s', encodedId)
// Vite expects { type: "custom", event, data } format for custom HMR events
const eventData = { id: encodedId, timestamp: Date.now() }
server.ws.send({
type: 'custom',
event: 'angular:component-update',
data: eventData,
})
}
}
}
debugHmr('added custom fs.watch for resource: %s', file)
})
}
// Expose the function so transform can call it
;(server as any).__angularWatchTemplate = watchTemplateFile
// Listen for angular:invalidate events from client
// When Angular's runtime HMR update fails, it sends this event to trigger a full reload
server.ws.on(
'angular:invalidate',
(data: { id: string; message: string; error: boolean }) => {
console.warn(`[Angular HMR] Runtime update failed for ${data.id}: ${data.message}`)
server.ws.send({
type: 'full-reload',
path: '*',
})
},
)
// HMR component update endpoint
if (pluginOptions.liveReload) {
const angularComponentMiddleware: Connect.HandleFunction = async (
req: Connect.IncomingMessage,
res: ServerResponse<Connect.IncomingMessage>,
next: Connect.NextFunction,
) => {
if (!req.url?.includes(ANGULAR_COMPONENT_PREFIX)) {
next()
return
}
const requestUrl = new URL(req.url, 'http://localhost')
const componentId = requestUrl.searchParams.get('c')
if (!componentId) {
res.statusCode = 400
res.end()
return
}
const decodedComponentId = decodeURIComponent(componentId)
const atIndex = decodedComponentId.indexOf('@')
// Validate component ID format: should be "filePath@ClassName"
if (atIndex === -1) {
console.error(`[Angular HMR] Invalid component ID format: ${componentId}`)
res.statusCode = 400
res.end()
return
}
const fileId = decodedComponentId.slice(0, atIndex)
const resolvedId = resolve(process.cwd(), fileId)
// Only return HMR update module if there's a pending update from our
// custom fs.watch handler. On initial page load, there are no pending
// updates, so we return an empty response. This prevents ɵɵreplaceMetadata
// from being called unnecessarily during initial load, which would
// re-create views and cause errors with @Required() decorators.
if (!pendingHmrUpdates.has(fileId)) {
res.setHeader('Content-Type', 'text/javascript')
res.setHeader('Cache-Control', 'no-cache')
res.end('')
return
}
pendingHmrUpdates.delete(fileId)
try {
const source = await readFile(resolvedId, 'utf-8')
const { templateUrls, styleUrls } = await extractComponentUrls(source, resolvedId)
const dir = dirname(resolvedId)
// Read fresh template content (bypass cache for HMR)
let templateContent: string | null = null
if (templateUrls.length > 0) {
const templatePath = resolve(dir, templateUrls[0])
templateContent = await readFile(templatePath, 'utf-8')
} else {
templateContent = extractInlineTemplate(source)
}
if (templateContent) {
const className = componentIds.get(resolvedId) ?? 'Component'
// Read fresh style content for all style URLs
let styles: string[] | null = null
if (styleUrls.length > 0) {
const styleContents: string[] = []
for (const styleUrl of styleUrls) {
const stylePath = resolve(dir, styleUrl)
try {
let styleContent = await readFile(stylePath, 'utf-8')
if (resolvedConfig) {
const processed = await preprocessCSS(
styleContent,
stylePath,
resolvedConfig as any,
)
styleContent = processed.code
}
styleContents.push(styleContent)
} catch {
// Style file not found, continue without this style
}
}
if (styleContents.length > 0) {
styles = styleContents
}
}
const result = compileForHmrSync(templateContent, className, resolvedId, styles)
res.setHeader('Content-Type', 'text/javascript')
res.setHeader('Cache-Control', 'no-cache')
res.end(result.hmrModule)
return
}
} catch (e) {
const error = e as Error
const errorMessage = error.message + (error.stack ? '\n' + error.stack : '')
console.error('[Angular HMR] Update failed:', errorMessage)
// Send angular:invalidate event to trigger graceful full reload
// This matches Angular's HMR error fallback pattern
server.ws.send({
type: 'custom',
event: 'angular:invalidate',
data: { id: componentId, message: errorMessage, error: true },
})
res.setHeader('Content-Type', 'text/javascript')
res.setHeader('Cache-Control', 'no-cache')
res.end('')
return
}
// No template content found
res.setHeader('Content-Type', 'text/javascript')
res.setHeader('Cache-Control', 'no-cache')
res.end('')
}
server.middlewares.use(angularComponentMiddleware)
}
},
transform: {
order: 'pre',
filter: {
id: ANGULAR_TS_REGEX,
},
async handler(code, id, options) {
// Skip node_modules
if (id.includes('node_modules')) {
return
}
// Quick check for Angular decorators - avoids parsing files without them
// OXC handles @Component, @Directive, @NgModule, @Injectable, and @Pipe
const hasAngularDecorator =
code.includes('@Component') ||
code.includes('@Directive') ||
code.includes('@NgModule') ||
code.includes('@Injectable') ||
code.includes('@Pipe')
if (!hasAngularDecorator) {
return
}
// Apply file replacements
const actualId = pluginOptions.fileReplacements?.[id] ?? id
// Resolve external resources
const { resources, dependencies } = await resolveResources(code, actualId)
// Disable HMR for SSR transforms. SSR bundles must not contain HMR
// initialization code that dynamically imports @ng/component virtual
// modules, as those are served via HTTP middleware only. This matches
// Angular's official behavior where _enableHmr is only set for browser
// bundles (see @angular/build application-code-bundle.js).
const isSSR = !!options?.ssr
// Track dependencies for HMR (client-side only)
// DON'T use addWatchFile - it creates modules in Vite's graph!
// Instead, use our custom watcher that doesn't create modules.
if (watchMode && viteServer && !isSSR) {
const watchFn = (viteServer as any).__angularWatchTemplate
for (const dep of dependencies) {
const normalizedDep = normalizePath(dep)
// Track reverse mapping for HMR: resource → component
resourceToComponent.set(normalizedDep, actualId)
// Add to our custom watcher
if (watchFn) {
watchFn(normalizedDep)
}
}
}
// Transform with Rust compiler
const transformOptions: TransformOptions = {
sourcemap: pluginOptions.sourceMap,
jit: pluginOptions.jit,
hmr: pluginOptions.liveReload && watchMode && !isSSR,
angularVersion: pluginOptions.angularVersion,
}
const result = await transformAngularFile(code, actualId, transformOptions, resources)
// Report errors and warnings
for (const error of result.errors) {
this.error(error.message)
}
for (const warning of result.warnings) {
this.warn(warning.message)
}
// Track component IDs for HMR
if (pluginOptions.liveReload) {
// templateUpdates is a plain object (NAPI HashMap → JS object)
const templateUpdateKeys = Object.keys(result.templateUpdates)
debugTransform(
'transform %s templateUpdates=%O deps=%O',
actualId,
templateUpdateKeys,
dependencies,
)
for (const componentId of templateUpdateKeys) {
const [, className] = componentId.split('@')
componentIds.set(actualId, className)
debugHmr('registered: %s -> %s', actualId, className)
}
}
return {
code: result.code,
map: result.map ?? null,
}
},
},
async handleHotUpdate(ctx) {
if (!pluginOptions.liveReload) return
debugHmr('handleHotUpdate file=%s', ctx.file)
debugHmr(
'ctx.modules=%d ids=%s',
ctx.modules.length,
ctx.modules.map((m) => m.id).join(', '),
)
// Template/style files are handled by our custom fs.watch in configureServer.
// We dynamically unwatch them from Vite's watcher during transform, so they shouldn't
// normally trigger handleHotUpdate. If they do appear here (e.g., file not yet transformed
// or from another plugin), return [] to prevent Vite's default handling.
if (/\.(html?|css|scss|sass|less)$/.test(ctx.file)) {
debugHmr('ignoring resource file in handleHotUpdate (handled by custom watcher)')
return []
}
// Handle component file changes
const isComponent = ANGULAR_TS_REGEX.test(ctx.file)
const hasComponentId = componentIds.has(ctx.file)
debugHmr(
'component check: isComponent=%s hasComponentId=%s file=%s',
isComponent,
hasComponentId,
ctx.file,
)
debugHmr('componentIds keys: %O', Array.from(componentIds.keys()))
if (isComponent && hasComponentId) {
debugHmr('triggering full reload for component file change')
// Component FILE changes require a full reload because:
// - Class definition changes can't be hot-swapped safely
// - Constructor, methods, signals, and state changes need a fresh start
// - Only template/style changes support HMR (handled by fs.watch separately)
//
// This matches Angular's official behavior - they only support HMR for
// template and style changes, not component class changes.
// Invalidate the component module
const componentModule = ctx.server.moduleGraph.getModuleById(ctx.file)
if (componentModule) {
ctx.server.moduleGraph.invalidateModule(componentModule)
}
// Clear any cached resources
resourceCache.delete(normalizePath(ctx.file))
// Trigger full reload
debugHmr('sending full-reload WebSocket message for %s', ctx.file)
ctx.server.ws.send({
type: 'full-reload',
path: ctx.file,
})
debugHmr('full-reload message sent')
return []
}
return ctx.modules
},
}
}
/**
* Plugin to encapsulate component styles.
*/
function stylesPlugin(): Plugin {
return {
name: '@oxc-angular/vite-styles',
transform: {
filter: {
id: /ngcomp/,
},
handler(code, id) {
if (!pluginOptions.liveReload) return
const params = new URL(id, 'http://localhost').searchParams
const componentId = params.get('ngcomp')
const encapsulation = params.get('e')
// Only encapsulate for emulated encapsulation (e=0)
if (encapsulation === '0' && componentId) {
const encapsulated = encapsulateStyle(code, componentId)
return {
code: encapsulated,
map: null,
}
}
return undefined
},
},
}
}
return [
angularPlugin(),
stylesPlugin(),
angularLinkerPlugin(),
pluginOptions.jit &&
jitPlugin({
inlineStylesExtension: pluginOptions.inlineStylesExtension,
}),
buildOptimizerPlugin({
jit: pluginOptions.jit,
sourcemap: pluginOptions.sourceMap,
thirdPartySourcemaps: false,
}),
ssrManifestPlugin({
ssrEntry: options.ssrEntry,
}),
].filter(Boolean) as Plugin[]
}
/**
* Extract inline template from @Component decorator.
*/
function extractInlineTemplate(code: string): string | null {
// Simple regex to extract inline template
const templateMatch = code.match(/template\s*:\s*`([^`]*)`/s)
if (templateMatch) {
return templateMatch[1]
}
const templateQuoteMatch = code.match(/template\s*:\s*['"]([^'"]*)['"]/)
if (templateQuoteMatch) {
return templateQuoteMatch[1]
}
return null
}
export { angular as default }