Skip to content

Commit 66c2e6c

Browse files
authored
feat: report custom element name collisions (#2053)
custom element name collisions are now detected before calling define() logging is added to the console.error to help developers identify the collisions includes version information to assist in troubleshooting
1 parent e105885 commit 66c2e6c

File tree

3 files changed

+1511
-2154
lines changed

3 files changed

+1511
-2154
lines changed

packages/mgt-element/src/components/baseComponent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { state } from 'lit/decorators.js';
1010
import { ProviderState } from '../providers/IProvider';
1111
import { Providers } from '../providers/Providers';
1212
import { LocalizationHelper } from '../utils/LocalizationHelper';
13+
import { PACKAGE_VERSION } from '../utils/version';
1314

1415
/**
1516
* Defines media query based on component width
@@ -43,6 +44,10 @@ export enum ComponentMediaQuery {
4344
* @extends {LitElement}
4445
*/
4546
export abstract class MgtBaseComponent extends LitElement {
47+
public static get version() {
48+
return PACKAGE_VERSION;
49+
}
50+
4651
/**
4752
* Gets or sets the direction of the component
4853
*

packages/mgt-element/src/utils/CustomElement.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,20 @@ import { customElementHelper } from '../components/customElementHelper';
1616
* @param tagName the base name for the custom element tag
1717
*/
1818
export const customElement = (tagName: string): ((classOrDescriptor: unknown) => any) => {
19-
return litElement(`${customElementHelper.prefix}-${tagName}`);
19+
const mgtTagName = `${customElementHelper.prefix}-${tagName}`;
20+
const mgtElement = customElements.get(mgtTagName);
21+
const unknownVersion = ' Unknown likely <3.0.0';
22+
const version = element => (element as any).version || unknownVersion;
23+
if (mgtElement) {
24+
return (classOrDescriptor: CustomElementConstructor) => {
25+
// tslint:disable-next-line: no-console
26+
console.error(
27+
`Tag name ${mgtTagName} is already defined using class ${mgtElement.name} version ${version(mgtElement)}\n`,
28+
`Currently registering class ${classOrDescriptor.name} with version ${version(classOrDescriptor)}\n`,
29+
'Please use the disambiguation feature to define a unique tag name for this component see: https://github.com/microsoftgraph/microsoft-graph-toolkit/tree/main/packages/mgt-components#disambiguation'
30+
);
31+
return classOrDescriptor;
32+
};
33+
}
34+
return litElement(mgtTagName);
2035
};

0 commit comments

Comments
 (0)