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

Function getComponentId

packages/core/src/render3/definition.ts:678–768  ·  view source on GitHub ↗

* A method can returns a component ID from the component definition using a variant of DJB2 hash * algorithm.

(componentDef: ComponentDef<T>)

Source from the content-addressed store, hash-verified

676 * algorithm.
677 */
678function getComponentId<T>(componentDef: ComponentDef<T>): string {
679 let hash = 0;
680
681 // For components with i18n in templates, the `consts` array is generated by the compiler
682 // as a function. If client and server bundles were produced with different minification
683 // configurations, the serializable contents of the function body would be different on
684 // the client and on the server. This might result in different ids generated. To avoid this
685 // issue, we do not take the `consts` contents into account if it's a function.
686 // See https://github.com/angular/angular/issues/58713.
687 const componentDefConsts = typeof componentDef.consts === 'function' ? '' : componentDef.consts;
688
689 // We cannot rely solely on the component selector as the same selector can be used in different
690 // modules.
691 //
692 // `componentDef.style` is not used, due to it causing inconsistencies. Ex: when server
693 // component styles has no sourcemaps and browsers do.
694 //
695 // Example:
696 // https://github.com/angular/components/blob/d9f82c8f95309e77a6d82fd574c65871e91354c2/src/material/core/option/option.ts#L248
697 // https://github.com/angular/components/blob/285f46dc2b4c5b127d356cb7c4714b221f03ce50/src/material/legacy-core/option/option.ts#L32
698 const hashSelectors = [
699 componentDef.selectors,
700 componentDef.ngContentSelectors,
701 componentDef.hostVars,
702 componentDef.hostAttrs,
703 componentDefConsts,
704 componentDef.vars,
705 componentDef.decls,
706 componentDef.encapsulation,
707 componentDef.standalone,
708 componentDef.exportAs,
709 JSON.stringify(componentDef.inputs),
710 JSON.stringify(componentDef.outputs),
711 // We cannot use 'componentDef.type.name' as the name of the symbol will change and will not
712 // match in the server and browser bundles.
713 Object.getOwnPropertyNames(componentDef.type.prototype),
714 !!componentDef.contentQueries,
715 !!componentDef.viewQuery,
716 ];
717
718 if (typeof ngDevMode === 'undefined' || ngDevMode) {
719 // If client and server bundles were produced with different minification configurations,
720 // the serializable contents of the function body would be different on the client and on
721 // the server. Ensure that we do not accidentally use functions in component id computation.
722 for (const item of hashSelectors) {
723 assertNotEqual(
724 typeof item,
725 'function',
726 'Internal error: attempting to use a function in component id computation logic.',
727 );
728 }
729 }
730
731 for (const char of hashSelectors.join('|')) {
732 hash = (Math.imul(31, hash) + char.charCodeAt(0)) << 0;
733 }
734
735 // Force positive number hash.

Callers 1

ɵɵdefineComponentFunction · 0.85

Calls 8

assertNotEqualFunction · 0.90
formatRuntimeErrorFunction · 0.90
stringifyCSSSelectorListFunction · 0.90
joinMethod · 0.65
hasMethod · 0.65
getMethod · 0.65
warnMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected