Skip to content

Commit

Permalink
fix: always define custom element class version getter (#7813) (#7815)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Sep 18, 2024
1 parent 307487f commit 3d488c5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions integration/tests/define.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('define', () => {
});

it('should warn when component with same version is loaded twice', () => {
defineCustomElement({ is: 'vaadin-button', version: Button.version });
defineCustomElement({ is: 'vaadin-button' });
expect(console.warn.calledOnce).to.be.true;
expect(console.warn.firstCall.args[0]).to.equal('The component vaadin-button has been loaded twice');
});
Expand All @@ -31,7 +31,7 @@ describe('define', () => {
});

it('should log an error when two components with different versions are loaded', () => {
defineCustomElement({ is: 'vaadin-button', version: '0.0.1' });
defineCustomElement({ is: 'vaadin-button' }, '0.0.1');
expect(console.error.calledOnce).to.be.true;
expect(console.error.firstCall.args[0]).to.equal(
`Tried to define vaadin-button version 0.0.1 when version ${Button.version} is already in use. Something will probably break.`,
Expand Down
2 changes: 1 addition & 1 deletion packages/component-base/src/define.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export interface CustomElementType extends CustomElementConstructor {
is: string;
}

export declare function defineCustomElement(CustomElement: CustomElementConstructor): void;
export declare function defineCustomElement(CustomElement: CustomElementConstructor, version?: string): void;
14 changes: 7 additions & 7 deletions packages/component-base/src/define.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/

export function defineCustomElement(CustomElement) {
export function defineCustomElement(CustomElement, version = '24.4.9') {
Object.defineProperty(CustomElement, 'version', {
get() {
return version;
},
});

const defined = customElements.get(CustomElement.is);
if (!defined) {
Object.defineProperty(CustomElement, 'version', {
get() {
return '24.4.9';
},
});

customElements.define(CustomElement.is, CustomElement);
} else {
const definedVersion = defined.version;
Expand Down

0 comments on commit 3d488c5

Please sign in to comment.