Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format

## [Unreleased]

### Changed

- Warn in development when a component is registered under a name already used by a different component, instead of silently ignoring it — registration stays first-writer-wins, mirroring `customElements.define` ([#736](https://github.com/studiometa/js-toolkit/pull/736))

## [v3.6.1](https://github.com/studiometa/js-toolkit/compare/3.6.0..3.6.1) (2026-07-13)

### Fixed
Expand Down
10 changes: 9 additions & 1 deletion packages/js-toolkit/Base/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isDefined, SmartQueue, dashCase } from '../utils/index.js';
import { isArray, isDefined, SmartQueue, dashCase, isDev } from '../utils/index.js';
import type { Base, BaseConfig, BaseConstructor } from './index.js';
import { features } from './features.js';
import { useMutation } from '../services/MutationService.js';
Expand Down Expand Up @@ -202,6 +202,14 @@ function mutationCallback() {

export function addToRegistry(nameOrSelector: string, ctor: BaseConstructor) {
if (registry().has(nameOrSelector)) {
if (isDev && registry().get(nameOrSelector) !== ctor) {
console.warn(
`[${nameOrSelector}] A different component is already registered under this name. ` +
'Registration is ignored — like `customElements.define`, a name maps to a single ' +
'component. Give this component a unique `config.name` (or use `withExtraConfig`, ' +
'which renames automatically).',
);
}
return;
}

Expand Down
19 changes: 19 additions & 0 deletions packages/tests/Base/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,25 @@ describe('The Base utils', () => {
await Promise.all(Array.from(fooInstances).map((i) => i.$destroy()));
});

it('should warn in dev mode when a name is registered with a different component', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const Foo = withName(Base, 'Foo');
const Bar = withName(Base, 'Bar');

addToRegistry('CollisionName', Foo);
// Re-registering the SAME class is idempotent and must not warn.
addToRegistry('CollisionName', Foo);
expect(warn).not.toHaveBeenCalled();

// A DIFFERENT class under the same name stays register-once (ignored) but warns.
addToRegistry('CollisionName', Bar);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn.mock.calls[0][0]).toContain('CollisionName');
expect(warn.mock.calls[0][0]).toContain('customElements.define');

warn.mockRestore();
});

it('should register new components', async () => {
const TestComponent = withName(Base, 'TestComponent');
const registryKey = '__JS_TOOLKIT_REGISTRY__';
Expand Down
Loading