Skip to content

Feature Request: "module" context allowing access to component Constructor #3010

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

Closed
tivac opened this issue Jun 11, 2019 · 4 comments
Closed

Comments

@tivac
Copy link
Contributor

tivac commented Jun 11, 2019

In svelte v2 our testing setup uses jest with puppeteer, and takes advantage of the setup hook to be able to have a reference to the component constructor so we can register them into a globally-available repository to ease accessing from jest.

Here's a brief example, it's a bit spread out but hopefully gets the idea across.

https://gist.github.com/tivac/38fe7fd0f8eb8335dfc05f8f5d375185

In v3 there's <script context="module"> but it doesn't have any access to the component's constructor like setup did. That makes me sad, because it means we don't have a great way to test our v3 components from within puppeteer beyond creating a manual central repository that includes them all or something.

I'm not totally sure how I'd expect accessing the component constructor from <script context="module"> to work. Maybe it could rewrite this or some sort of other special named thing? I know Rich has avoided special functions in the past so importing it from svelte/constructor or something seems right out.

@mrkishi
Copy link
Member

mrkishi commented Jun 11, 2019

I'd definitely not rewrite this since it's supposed to be undefined in es modules, and that'd be surprising in a subtle way (as opposed to 'in your face' like the rest of svelte's unconventional features).

But I don't see much of a problem with providing access to the component class through a non-exported const alias like Component, ComponentClass, Self or something in those lines.

If someone created their own variables with a clashing name, we could simply not emit the alias. But instead of mangling internal names like we do for everything else, if they just referenced the alias we wouldn't do anything, giving users direct access to the generated class {name} extends SvelteComponent class.

I'd definitely back a feature like that. When not in use the alias would be tree-shakeable anyway...

But yeah, I don't think Rich likes the idea of giving access to a component's instance/class from within the component's context in general... In the meanwhile, if you're okay with some dirtiness, have a look at this hacky async registry implementation that works in current-svelte.

@stalkerg
Copy link
Contributor

Yes, Svelte3 think what you should do everything outside component, but sometimes you can't control this outside process very well.
(a similar problem was with #3089)

Also, this flexibility very important for share logic between components: #779 .

@egargan
Copy link

egargan commented Jan 19, 2021

Any update on this proposal? I've run into a situation where this would be particularly useful, where I'd like to be able to export the component itself, as well as some complementary config, from module context.

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants