-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export maps "convention/recommendation" for web components #7
Comments
maybe: import 'my-input/define'; // <--- side effect customElements.define(...)
// or
import 'my-input/defined'; // <--- side effect customElements.define(...) So the actual side effect is made clear in the specifier? |
uh I like 🤗 import 'my-input/define'; |
I like the "name": "my-lib",
"exports": {
".": "./index.js", // <--- side effect free
"./first-element": "./src/first-element/FirstElement.js", // <-- side effect free
"./first-element/definition": "./src/first-element/fist-element.js",
"./second-element": "./src/second-element/SecondElement.js", // <-- side effect free
"./second-element/definition": "./src/second-element/second-element.js"
} |
I think this is maybe useful to talk about once scoped custom element registries have landed, but I think it's actively bad to have non-self-defining element classes right now, and I personally would advocate for all elements to be defined in the same module as the class declaration. It's unsafe to do otherwise: https://justinfagnani.com/2019/11/01/how-to-publish-web-components-to-npm/#always-self-define-elements |
we had and we still have lots of issues with side effects (not only custom elements 😅) ... most of these issues arise because users do something like and as and as we are already making have use of a scoping mechanism (similarish to scoped custom element registries) it definitely makes sense for us... I personally believe it's a general good recommendation (e.g. side effect free main entry point and have sepearte entry points for side effects) |
The thing is that non-self-registering elements are fundamentally unsafe. Any two consumers who want to use the element must agree on a tag name, or one will fail. The only party who can choose a canonical name is the element class itself. If one consumer imports the side-effect-free module and registers the element, it'll make every other consumer who imports the self-registering module fail. |
this is not true for nested dependencies 😅. which will in such a case always result in a browser error you can not fix without forcing the same version to everyone (which might be impossible) At ING we are rather lucky as we can "enforce" usage of scoped elements by not even exporting the ComparisonSo we basically have 2 options
I'm in favor of 2 as I think it's more forward-looking and it mainly requires an info/warning while as 1 can result in situations where you basically can't use that element in your app. (e.g. in ING if you build a reusable component or feature that will be used in multiple apps then you can not use any self-registering elements as it will break as soon as 2 versions (via nesting) of that element get used) |
what are the problems with side-effects specifically?
what did you mean by this point? You can have a self-defining element and use it in a scoped registry. |
If we can't import a component without it's side effect means that we can't use different major versions of the same component at the same time because the browser will fail in the definition of the second loaded version. |
Has anyone experienced issues from Web Component Side effects when using something like in a browser? // Both calls on the same app
import { MyInput } from './my-input';
import './my-input'; I myself have never needed to do that and without testing I would expect Personally I would prefer to keep simple mapping for Export Maps (one map = one script) regardless of side effects or not. |
if you don't have nested versions then indeed there is no need for any of that... so in the example above we however need to maintain multiple major versions of the same component in one app. in short, if you somehow execute
and you then import it like this // src/app.js
import { RedEl } from 'red-el';
import { MyFeat } from 'my-feat'; it will lead to the error
if You can read more details in the Scoped Elements Motivation Section. PS: You may ask why even bother? force a single version... this is imho a bad argument as it's really important for migration phases - if you have 1000 components you not gonna upgrade them in one go from 1.x to 2.x...
let's park this topic for now - it's not 100% relevant for this discussion |
I've been following this discussion and learning a lot from it -- I was just about to do some refactoring of my library (three-elements) to allow for selectively importing individual tags and even customizing their prefixes, but will now rather strive to keep things simple and non-surprising. I do have on question though. What is your take on loading/defining custom elements in a hot reloading dev server context? In three-elements, I've had to whip up my own helper function that wraps (I'm aware that this also means that updated versions of my classes are not registered; but I'm not talking about the development of the library itself, but of projects that use it.) It feels to me that, unless I've missed something obvious (and I often do), the immediate use of Also please consider that "hot reloading" is not always just "hot module reloading" from React et al. This problem occurs whenever new code is pushed to the browser without it doing a full page reload. (For example: any Create-React-App project, or any Codesandbox sandbox.) |
Indeed HMR requires special handling (in any framework/system)... for web components there is no difference... the special handling, in this case, is all about never reregistering the component again but by "patching/proxying" function calls to the latest implementations. You can read some more details here https://open-wc.org/docs/development/hot-module-replacement/#how-it-works |
Thanks for your detailed response @daKmoR |
Thanks for the link! In the context of the discussion in this issue, I wonder if this maybe should be part of the recommendation? I'm just -- once again, naively, I'm still relatively new to all this -- guessing that if the "official" recommendation is to always |
Drive-by comment - After reading @daKmoR comment regarding my question for the need; at a high level this reminds me of sites that have the need for multiple jQuery versions due to different plugins. I have created a few production apps at work with Web Components now (and some open source ones on Github) but luckily I haven't had to work about component version yet. I'm about ready to go to bed though so need to follow up and read all the links in more detail tomorrow and again next week 🤔 |
Exports maps are now available in node & rollup & webpack.
Maybe we can have a convention/recommendation for what to define for web components? 🤔
Given the following component/package
my-input
;From a users point of view it would probably be nice to have imports like this:
This would lead to the following export map
What do you think?
The text was updated successfully, but these errors were encountered: