-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompiler-Build-Tools-Examples
184 lines (146 loc) · 4.54 KB
/
Compiler-Build-Tools-Examples
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
// Compiler & Build Tools Examples
//Example 1: Mock Service for Data
// app.ts
import { Component, NgModule, ChangeDetectionStrategy } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { RouterModule, Routes } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
// Mock Service for Data
@Injectable({ providedIn: 'root' })
export class DataService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
return this.http.get('https://api.example.com/data'); // Mocked for demo
}
}
// Example 1: Basic Angular Compiler Usage
@Component({
selector: 'app-hello',
template: '<h1>Hello World</h1>' // Example 1
})
export class HelloComponent {}
// Example 2: JIT Compilation (Development Mode Implicitly Shown)
@Component({
selector: 'app-jit-demo',
template: `
<h2>JIT Demo</h2>
<p>Rendered in browser at runtime</p>
`
})
export class JITDemoComponent {}
// Example 3: Ivy Compiler Optimization with Templates
@Component({
selector: 'app-ivy-optimize',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<h2>Ivy Optimized Component</h2>
<div>{{ user.name.toUpperCase() }}</div>
`
})
export class IvyOptimizeComponent {
user = { name: 'Angular' };
}
// Example 4: Entry Component (Mocked as Standalone for Simplicity)
@Component({
selector: 'app-modal',
standalone: true,
template: `
<h2>Modal Component</h2>
<div>Modal content</div>
`
})
export class ModalComponent {}
// Example 5: Lazy Loading Demo
@Component({
selector: 'app-lazy-demo',
template: `
<h2>Lazy Loading Demo</h2>
<p>This would typically be in a lazy-loaded module</p>
`
})
export class LazyDemoComponent {}
// Root NgModule (AppModule)
@NgModule({
declarations: [
HelloComponent,
JITDemoComponent,
IvyOptimizeComponent,
LazyDemoComponent
],
imports: [
BrowserModule,
CommonModule,
HttpClientModule,
RouterModule.forRoot([
{ path: 'hello', component: HelloComponent },
{ path: 'jit', component: JITDemoComponent },
{ path: 'ivy', component: IvyOptimizeComponent },
// Example 24: Simulated lazy loading (real lazy loading requires separate module)
{ path: 'lazy', component: LazyDemoComponent },
{ path: '', redirectTo: '/hello', pathMatch: 'full' }
])
],
providers: [
DataService,
{ provide: HttpClient, useValue: { get: () => of('Mock Data') } } // Mock HttpClient
],
bootstrap: [HelloComponent]
})
export class AppModule {}
// Bootstrap the Application
bootstrapApplication(HelloComponent, {
providers: [
provideRouter([]), // Router is already provided by AppModule
DataService
]
}).catch(err => console.error(err));
// Mock Build Configurations (Not Executable Here, For Reference)
// Example 6: AOT Compilation and ng build --prod
// Terminal command: ng build --prod
// This would use AOT to compile templates into efficient JavaScript.
// Example 7: ngc Command
// Terminal command: ngc --project tsconfig.app.json
// This compiles the app with AOT.
// Example 8: Production vs Development Builds
// Terminal commands:
// ng build --prod // Production build with AOT and optimizations
// ng serve // Development build with JIT
// Example 9: Build Size Analysis
// Terminal commands:
// ng build --prod --stats-json
// webpack-bundle-analyzer dist/your-app/stats.json
// Example 10: angular.json Configuration (Simplified Mock)
const mockAngularJson = {
"projects": {
"your-app": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
}
}
}
}
}
}
};
// Example 11: Lazy Loading Configuration (Real Lazy Loading Mocked in Routes)
// const routes: Routes = [
// { path: 'feature', loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule) }
// ];
// Example 12: WebAssembly Integration (Mocked Function)
async function loadWasm() {
const wasmModule = await fetch('path/to/module.wasm');
const wasmArrayBuffer = await wasmModule.arrayBuffer();
const wasmInstance = await WebAssembly.instantiate(wasmArrayBuffer);
console.log(wasmInstance.exports);
}