From f04019586105263dff40c124b33a5905074982cc Mon Sep 17 00:00:00 2001 From: Serhii Kulykov Date: Wed, 18 Sep 2024 13:30:40 +0300 Subject: [PATCH] fix: always define custom element class version getter (#7813) (#7814) --- packages/component-base/src/define.d.ts | 2 +- packages/component-base/src/define.js | 14 +++++++------- test/integration/define.test.js | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/component-base/src/define.d.ts b/packages/component-base/src/define.d.ts index efe4e20e35..8547c9992d 100644 --- a/packages/component-base/src/define.d.ts +++ b/packages/component-base/src/define.d.ts @@ -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; diff --git a/packages/component-base/src/define.js b/packages/component-base/src/define.js index 19a8d56698..9f52844a44 100644 --- a/packages/component-base/src/define.js +++ b/packages/component-base/src/define.js @@ -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.5.0-beta1') { + Object.defineProperty(CustomElement, 'version', { + get() { + return version; + }, + }); + const defined = customElements.get(CustomElement.is); if (!defined) { - Object.defineProperty(CustomElement, 'version', { - get() { - return '24.5.0-beta1'; - }, - }); - customElements.define(CustomElement.is, CustomElement); } else { const definedVersion = defined.version; diff --git a/test/integration/define.test.js b/test/integration/define.test.js index cb2e905f74..b90c0fd4e8 100644 --- a/test/integration/define.test.js +++ b/test/integration/define.test.js @@ -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'); }); @@ -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.`,