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
pkg/lib: create cockpit.ts #20417
base: main
Are you sure you want to change the base?
pkg/lib: create cockpit.ts #20417
Conversation
Rename `cockpit.js` to `cockpit-core.js`. Import it to `cockpit.ts` and re-export it as the default export. This is the traditional cockpit API up to this point, and you can still access it via: `import cockpit from 'cockpit';` Take our type annotations from `cockpit.d.ts` and move them into `cockpit.ts`, changing their format to an interface which describes the cockpit object (our default export). This is actually a bit of a reduction in readability because it means that we can no longer keep the helper interface types beside the functions that use them, but it simplifies our setup (removing a duplicate `tsc` call, speeding up `test/static-code` a bit). As a side-effect of this change, we're no longer treating `cockpit` as a namespace, which means that we need to take the types that we've been publicly exporting on that namespace and export them from `cockpit.ts`, as non-default exports. That means that instead of doing something like: ``` import cockpit from 'cockpit'; ... cockpit.JsonObject ``` it will look like ``` import cockpit, { JsonObject } from 'cockpit'; ... JsonObject ``` In general, we plan that, going forward, new API will be added in this way and made available via named exports. Eventually (in the very distant future) the default export will be deprecated.
This would be ugly, and I don't like that. It'd still be better to treat import * as cockpit from 'cockpit';
cockpit.JsonObject ... We actually use this syntax quite a lot. E.g. pkg/lib/packagekit.js is in the same boat, and we e.g. do
This is especially awkward with e.g.
from the current version of this PR, as it collides with the official JS API. That part of namespace cluttering gets my 👎. So most of the "noise" of this PR should/can then be reverted if we just fix the imports/exports hopefully? Other than that, this approach seems fine to me. Thanks! |
Cluttering as we currently do? Like not having
Being available, even worse is that some folks might depend on |
If I understand you correctly, you're advocating changing nothing in In that case, if I understand you correctly, the main symbol would be called |
Ah, I see what you mean. I was refering to importing particular things like Maybe two imports like // default export, backwards compatible
import cockpit from 'cockpit'
// other exports, new API
import * as cockpit_api from 'cockpit' ? |
So, there's 51 things that are hung on the main
(with the event mixin methods suspiciously absent for some reason...) We could export each of those as a separate thing from |
So there's one good example of somewhere I'd not like to have an existing API directly exported on the module: import cockpit from 'cockpit';
cockpit.gettext() and import { gettext } from 'cockpit';
gettext() to be different. |
Would like to add my 2 cents - I think you'll be hard-pressed to find lots of implementations where the destructuring syntax isn't used. Importing the default export, and then subsequently importing specific types / modules seems to be the 'de-facto' way of doing things. TLDR; I support the following syntax: import cockpit, { JsonObject } from 'cockpit'; |
Rename
cockpit.js
tocockpit-core.js
. Import it tocockpit.ts
and re-export it as the default export. This is the traditional cockpit API up to this point, and you can still access it via:import cockpit from 'cockpit';
Take our type annotations from
cockpit.d.ts
and move them intocockpit.ts
, changing their format to an interface which describes the cockpit object (our default export). This is actually a bit of a reduction in readability because it means that we can no longer keep the helper interface types beside the functions that use them, but it simplifies our setup (removing a duplicatetsc
call, speeding uptest/static-code
a bit).As a side-effect of this change, we're no longer treating
cockpit
as a namespace, which means that we need to take the types that we've been publicly exporting on that namespace and export them fromcockpit.ts
, as non-default exports. That means that instead of doing something like:it will look like
In general, we plan that, going forward, new API will be added in this way and made available via named exports. Eventually (in the very distant future) the default export will be deprecated.
This is 100% pure RFC at this point, and I don't anticipate that we'll land this soon, but I wanted to get it off of my disk. I don't think I like exporting all of the interfaces (like
UserInfo
) from'cockpit'
with names likeUserInfo
but I'm not sure what a better way might look like.Everything else I'm fairly happy with....