-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathangular-build-optimizer-plugin.ts
More file actions
100 lines (93 loc) · 2.68 KB
/
angular-build-optimizer-plugin.ts
File metadata and controls
100 lines (93 loc) · 2.68 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
import { optimizeAngularPackage } from '#binding'
import type { Plugin } from 'vite'
/**
* Build optimizer plugin for Angular production builds.
*
* This plugin:
* 1. Sets up Angular-specific build defines (ngDevMode, ngJitMode, etc.)
* 2. Applies the Angular build optimizer to pre-compiled packages from node_modules
*
* The optimizer enables better tree-shaking by:
* - Removing Angular metadata calls (ɵsetClassMetadata, etc.)
* - Wrapping static fields in pure IIFEs
* - Adding /* @__PURE__ *\/ annotations to top-level calls
* - Optimizing TypeScript enum patterns
*/
export function buildOptimizerPlugin({
jit,
sourcemap,
thirdPartySourcemaps,
}: {
jit: boolean
sourcemap: boolean
thirdPartySourcemaps: boolean
}): Plugin {
let isProd = false
return {
name: '@oxc-angular/vite-optimizer',
apply: 'build',
config(userConfig) {
isProd = userConfig.mode === 'production' || process.env['NODE_ENV'] === 'production'
const isSSR = !!userConfig.build?.ssr
const ngServerMode = `${isSSR}`
if (isProd) {
return {
define: {
ngJitMode: jit ? 'true' : 'false',
ngI18nClosureMode: 'false',
ngDevMode: 'false',
ngServerMode,
},
oxc: {
define: {
ngDevMode: 'false',
ngJitMode: jit ? 'true' : 'false',
ngI18nClosureMode: 'false',
ngServerMode,
},
},
}
}
// In dev SSR mode, set ngServerMode even without the full production defines
if (isSSR) {
const defines: Record<string, string> = { ngServerMode }
return {
define: defines,
oxc: {
define: defines,
},
}
}
return undefined
},
transform: {
filter: {
// Match Angular FESM packages (e.g., @angular/core/fesm2022/core.mjs, @ngrx/store/fesm2022/ngrx-store.mjs)
id: /fesm20.*\.[cm]?js$/,
},
async handler(code, id) {
// Only optimize in production builds
if (!isProd) {
return
}
try {
const result = await optimizeAngularPackage(code, id, {
sourcemap: sourcemap && thirdPartySourcemaps,
elideMetadata: true,
wrapStaticMembers: true,
markPure: true,
adjustEnums: true,
})
return {
code: result.code,
map: result.map ?? null,
}
} catch (e) {
// If optimization fails, return the original code
console.warn(`[angular-optimizer] Failed to optimize ${id}:`, e)
return undefined
}
},
},
}
}