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

Function mergeWithExistingDefinition

packages/core/src/render3/hmr.ts:127–159  ·  view source on GitHub ↗

* Merges two component definitions while preseving the original one in place. * @param currentDef Definition that should receive the new metadata. * @param newDef Source of the new metadata.

(
  currentDef: ComponentDef<unknown>,
  newDef: ComponentDef<unknown>,
)

Source from the content-addressed store, hash-verified

125 * @param newDef Source of the new metadata.
126 */
127function mergeWithExistingDefinition(
128 currentDef: ComponentDef<unknown>,
129 newDef: ComponentDef<unknown>,
130) {
131 // Clone the current definition since we reference its original data further
132 // down in the replacement process (e.g. when destroying the renderer).
133 const clone = {...currentDef};
134
135 // Assign the new metadata in place while preserving the object literal. It's important to
136 // Keep the object in place, because there can be references to it, for example in the
137 // `directiveDefs` of another definition.
138 const replacement = Object.assign(currentDef, newDef, {
139 // For standalone components, `setComponentScope` is never used (imports are always inlined on
140 // the component definition), so always take the new directive and pipe defs, including `null`
141 // when the last import is removed. For non-standalone components, keep the existing defs
142 // unconditionally, because `setComponentScope` in the module file won't run again during an
143 // HMR replacement of the component file.
144 directiveDefs: newDef.standalone ? newDef.directiveDefs : clone.directiveDefs,
145 pipeDefs: newDef.standalone ? newDef.pipeDefs : clone.pipeDefs,
146
147 // Preserve the old `setInput` function, because it has some state.
148 // This is fine, because the component instance is preserved as well.
149 setInput: clone.setInput,
150
151 // Externally this is redundant since we redeclare the definition using the original type.
152 // Internally we may receive a definition with an alternate, but identical, type so we have
153 // to ensure that the original one is preserved.
154 type: clone.type,
155 });
156
157 ngDevMode && assertEqual(replacement, currentDef, 'Expected definition to be merged in place');
158 return {newDef: replacement, oldDef: clone};
159}
160
161/**
162 * Finds all LViews matching a specific component definition and recreates them.

Callers 1

ɵɵreplaceMetadataFunction · 0.85

Calls 1

assertEqualFunction · 0.90

Tested by

no test coverage detected