Skip to content
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

Static properties on components #578

Closed
DylanPiercey opened this issue May 7, 2017 · 8 comments
Closed

Static properties on components #578

DylanPiercey opened this issue May 7, 2017 · 8 comments

Comments

@DylanPiercey
Copy link

Currently as far as I can tell this is not possible.

I would imagine it would be something like:

<p>Hello World</p>
<script>
export const meta = 'my meta data'
export default {...}
</script>

Then you could consume this programatically like so:

import MyComponent, { meta } from './my-component.html'

console.log(meta)

I think this would be handy to have and am curious what others think and if there are other ways to achieve this effect (perhaps using methods as getters).

@nsaunders
Copy link

Looks like #193.

@Rich-Harris
Copy link
Member

I think this is more in line with #480 (comment) — how does that proposal sound?

@DylanPiercey
Copy link
Author

DylanPiercey commented May 28, 2017

I think that proposal solves the problem. My only reservation is that it seems more frameworky than this as setup would be a runtime thing vs compiling to the proper class.

@Conduitry
Copy link
Member

One thing that's not clear to me is with this proposal whether MyComponent.meta would also be 'my meta data'. Simply passing through named exports unchanged seems like it would be really simple, if we didn't have to worry about also storing them on the component's class. This question could be precisely the difference between this ticket and something like #480.

@Rich-Harris
Copy link
Member

The only thing that gives me pause is interoperability — at the moment, export default in a Svelte component translates to module.exports = in CommonJS or return in IIFE/AMD/UMD. (This is also how Rollup treats default exports without accompanying named exports).

When you start throwing named exports into the mix, you have to start doing things like this...

exports['default'] = Component;
exports.meta = 'my meta data';

...which inevitably ends up causing all sorts of headaches that are impossible to anticipate. I may be overly sensitive to that sort of thing, having been through the trenches with Rollup, but given that there really isn't a scenario where you'd need the named export but not the default one, to me it feels simpler to just add meta as a static property of the component — works the same everywhere, with no surprises.

@DylanPiercey
Copy link
Author

DylanPiercey commented Jun 6, 2017

@Rich-Harris couldn't you could simply translate it to the following?

module.exports = exports = Component
exports['default'] = exports
exports.meta = 'my meta data'

Or even

module.exports = exports = Component['default'] = Component
exports.meta = 'my meta data'

@Rich-Harris
Copy link
Member

I seem to remember there being some reason why that also causes problems, though I can't remember for the life of me what it was. Possibly something to do with assumptions made by other tooling. Or I could be imagining it...

@DylanPiercey
Copy link
Author

@Rich-Harris I can't seem to recall anything like that. Although tools like babel will check for exports.__es_module = true before using exports.default. Although I don't think that matters in this case.

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

No branches or pull requests

4 participants