MCPcopy Index your code
hub / github.com/angular/angular / compileHmrInitializer

Function compileHmrInitializer

packages/compiler/src/render3/r3_hmr_compiler.ts:55–160  ·  view source on GitHub ↗
(meta: R3HmrMetadata)

Source from the content-addressed store, hash-verified

53 * @param meta HMR metadata extracted from the class.
54 */
55export function compileHmrInitializer(meta: R3HmrMetadata): o.Expression {
56 const moduleName = 'm';
57 const dataName = 'd';
58 const timestampName = 't';
59 const idName = 'id';
60 const importCallbackName = `${meta.className}_HmrLoad`;
61 const namespaces = meta.namespaceDependencies.map((dep) => {
62 return new o.ExternalExpr({moduleName: dep.moduleName, name: null});
63 });
64
65 // m.default
66 const defaultRead = o.variable(moduleName).prop('default');
67
68 // ɵɵreplaceMetadata(Comp, m.default, [...namespaces], [...locals], import.meta, id);
69 const replaceCall = o
70 .importExpr(R3.replaceMetadata)
71 .callFn([
72 meta.type,
73 defaultRead,
74 o.literalArr(namespaces),
75 o.literalArr(meta.localDependencies.map((l) => l.runtimeRepresentation)),
76 o.variable('import').prop('meta'),
77 o.variable(idName),
78 ]);
79
80 // (m) => m.default && ɵɵreplaceMetadata(...)
81 const replaceCallback = o.arrowFn(
82 [new o.FnParam(moduleName, o.DYNAMIC_TYPE)],
83 defaultRead.and(replaceCall),
84 );
85
86 // getReplaceMetadataURL(id, timestamp, import.meta.url)
87 const url = o
88 .importExpr(R3.getReplaceMetadataURL)
89 .callFn([
90 o.variable(idName),
91 o.variable(timestampName),
92 o.variable('import').prop('meta').prop('url'),
93 ]);
94
95 // function Cmp_HmrLoad(t) {
96 // import(/* @vite-ignore */ url).then((m) => m.default && replaceMetadata(...));
97 // }
98 const importCallback = new o.DeclareFunctionStmt(
99 importCallbackName,
100 [new o.FnParam(timestampName, o.DYNAMIC_TYPE)],
101 [
102 // The vite-ignore special comment is required to prevent Vite from generating a superfluous
103 // warning for each usage within the development code. If Vite provides a method to
104 // programmatically avoid this warning in the future, this added comment can be removed here.
105 new o.DynamicImportExpr(url, null, '@vite-ignore')
106 .prop('then')
107 .callFn([replaceCallback])
108 .toStmt(),
109 ],
110 null,
111 o.StmtModifier.Final,
112 );

Callers 3

compileFullMethod · 0.90
compilePartialMethod · 0.90
compileLocalMethod · 0.90

Calls 4

devOnlyGuardedExpressionFunction · 0.90
mapMethod · 0.80
cloneMethod · 0.65
propMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…