kimroen / ember-cli-coffeescript

Adds precompilation of CoffeeScript files and all the basic generation types to the ember generate command.
MIT License
72 stars 49 forks source link

Make `utils` generated files export named function #138

Closed mriska closed 7 years ago

mriska commented 7 years ago

@kimroen:

In the original blueprint this is a named function, but I haven't found a way to export a named function with the CoffeeScript syntax, so I'm not sure if it can be done.

https://github.com/kimroen/ember-cli-coffeescript/pull/137/files/f51b2479cfe013389a766b2a8bedd1ecf801db59#diff-0cf817951560f1b4dff974e60873ea9bR1

kimroen commented 7 years ago

Initializers and helpers do something similar where there's a default export, and then the function is also exported, but named.

The helper is done like this:

export fooBar = () ->

export default Ember.Helper.helper fooBar

Which generates

export var fooBar = function() {};

export default Ember.Helper.helper(fooBar);

Utils are different though, because they use a default export, which are not named. Some alternatives I can think of:

1. Unnamed (what we have now)

Coffee

export default () ->

JavaScript

export default function() {};

2. Assigned to variable

Coffee

export default fooBar = () ->

JavaScript

var fooBar;

export default fooBar = function() {};

3. Declared separately

Coffee

fooBar = () ->
export default fooBar

JavaScript

var fooBar;

fooBar = function() {};

export default fooBar;

If number 2 works, then I think that's the one we want, probably.

mriska commented 7 years ago

I did a small test by creating a util barCoffee.coffee:

export default barCoffee = () ->
  42

And the following test passes:

import barCoffee from 'foo/utils/bar-coffee'
import { module, test } from 'qunit'

module 'Unit | Utility | bar coffee'

test 'it works', (assert) ->
  result = barCoffee()
  assert.equal result, 42

I am unsure though in what way export default barCoffee = () -> would differ from export default () ->, since they both only export the function as a default and not a named export that can be imported with import { barCoffee } from ...

But number 2 on your list definitely works, I just don't understand the benefit from it.

kimroen commented 7 years ago

I am unsure though in what way export default barCoffee = () -> would differ from export default () ->, since they both only export the function as a default and not a named export that can be imported with import { barCoffee } from ...

But number 2 on your list definitely works, I just don't understand the benefit from it.

I don't completely understand what (if any) benefit it has either. It could be completely irrelevant in the context of an export/import, but in general if an anonymous function is assigned to a variable, then the browser will figure out the name and show it in the stack trace. It could be that I am mixing up variable assignment with objects with a function value, though.

The most pragmatic approach here is probably to leave the code as is, because I'm not sure how I would go about verifying or debunking the idea, and either way the potential value of figuring it out isn't that great.

I'll close the issue, but if you have any thoughts then please share them.

mriska commented 7 years ago

@kimroen Sounds good!