Re: [WICG/webcomponents] Cleaning up (undefining) custom elements? (#754)

@MattiasMartens 
> use case where this is helpful: if i download two dependencies that both try to define the same custom element (possibly because they each separately invoke that dependency), and those dependencies don't check if the element is already defined, i could use "undefine" to prevent the second one from failing.

Libraries should not be automatically registering their custom elements in this way without providing a non-automatic route too. While it can be useful to just include the script in a plug and play manner, it is too rigid and, if you want to extend or customise the element before registration, the registered element may never even be used. I'd also find it strange for a library to register custom elements from its dependencies or allow its dependencies to register custom elements without any attempt at scoping or isolation, that's already a recipe for problems. It's akin to using a fixed global variable to store information or defining a global function as the main entrypoint for your library (a problem that modules were meant to solve).

I try to ensure the output for my custom element libraries is either the custom element class, which the page author can register themselves, or a function that will register my custom elements and any dependency custom elements under a provided tag name/prefix. Even if the author has not provided an official approach to importing the element without registration, you might be able to import it from the dependency source code directly:

```javascript
// Automatically defines <awesome-widget>
// import AwesomeWidgetElement from 'awesome-widget';
import AwesomeWidgetElement from 'awesome-widget/src/AwesomeWidgetElement.js';

customElements.define('my-awesome-widget', AwesomeWidgetElement);
```

-- 
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/754#issuecomment-1369561215
You are receiving this because you are subscribed to this thread.

Message ID: <WICG/webcomponents/issues/754/1369561215@github.com>

Received on Tuesday, 3 January 2023 09:41:39 UTC