MCPcopy Create free account
hub / github.com/angular/angular / compileHmrInitializer

Function compileHmrInitializer

packages/compiler/src/render3/r3_hmr_compiler.ts:55–163  ·  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 o.DYNAMIC_TYPE,
85 );
86
87 // getReplaceMetadataURL(id, timestamp, import.meta.url)
88 const url = o
89 .importExpr(R3.getReplaceMetadataURL)
90 .callFn([
91 o.variable(idName),
92 o.variable(timestampName),
93 o.variable('import').prop('meta').prop('url'),
94 ]);
95
96 // function Cmp_HmrLoad(t) {
97 // import(/* @vite-ignore */ url).then((m) => m.default && replaceMetadata(...));
98 // }
99 const importCallback = new o.DeclareFunctionStmt(
100 importCallbackName,
101 [new o.FnParam(timestampName, o.DYNAMIC_TYPE)],
102 [
103 // The vite-ignore special comment is required to prevent Vite from generating a superfluous
104 // warning for each usage within the development code. If Vite provides a method to
105 // programmatically avoid this warning in the future, this added comment can be removed here.
106 new o.DynamicImportExpr(url, null, '@vite-ignore')
107 .prop('then')
108 .callFn([replaceCallback])
109 .toStmt(),
110 ],
111 o.DYNAMIC_TYPE,
112 o.StmtModifier.Final,

Callers 4

compileFullMethod · 0.90
compilePartialMethod · 0.90
compileLocalMethod · 0.90
compileClassesFunction · 0.90

Calls 4

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

Tested by

no test coverage detected