0

I'm using babel and trying to export some functions under exports. This is how I export my function

 export function test (param1, param2) { ... }

currently, it does something like this:

 exports.test = test;
 function test(param1, param2) { ... }

is there a way to make babel create exports like this:

 exports.test = function(param1, param2) { ... }
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Madd0g
  • 3,841
  • 5
  • 37
  • 59
  • I don't think it's possible. Why would you want to do that anyway? – Felix Kling Jun 23 '15 at 07:54
  • @FelixKling - I use code-analysis tools on the output and don't like that the jsdoc comments get attached to a free-floating named function declaration. – Madd0g Jun 23 '15 at 09:13
  • Ah ok. I guess running tools on generate code is usually not that useful. What does the tool do? Can you run it on the original code? – Felix Kling Jun 23 '15 at 14:35
  • @FelixKling - Run it on the ES6 code? Nah, it's totally not ready for that. I'm just wondering about the reason for babel doing it in such an indirect way. I'd have thought they'd try to be as close as possible to the original code intentions. The jsdoc comment (as I see it) belonged to `exports.test = function()`, not to the `function()`. – Madd0g Jun 23 '15 at 14:58
  • One of the reasons is hosting. Since this is actually a function declaration, only creating a real function declaration ensures that the generated code has the same behavior. I guess the comment attachment could be better. If you are doing this for documentation generation, I actually implemented my own solution for one of my projects. You could at least have a look at it if you think it might help. – Felix Kling Jun 23 '15 at 15:00
  • 1
    https://github.com/fkling/jsnetworkx-docgen/blob/master/extractDocs.js ... this basically looks at all exported functions and classes in a file and parser their docblock and a couple of other things. – Felix Kling Jun 23 '15 at 15:06
  • @FelixKling - good stuff in that link, thanks! I guess I don't know enough about babel to yet. I thought `export function name` would work exactly like `exports.name = function` but if it's changed into a standalone named function declaration, it differs in behavior - it's hoisted and not anonymous anymore. I mean, aren't there important differences between `x = function()` and `function x()`? But maybe I don't know enough about babel and there are other considerations – Madd0g Jun 23 '15 at 15:42
  • 1
    You are exactly right, however your assumption is wrong. There are differences, but `exports.name = function` maps to `export var name = function`, not `export function name`. If you don't need the hoisting your code try whether `export var x = function` solves the issue for you. – Felix Kling Jun 23 '15 at 16:42
  • Ah, interesting feature (but super ugly syntax), now it is still split into two statements, only the function is now private because it's stored in a var. Thanks, you've been very helpful and it was cool to see how your extractDocs does very similar things to what mine does, but with completely different libraries and approach - which is always fun because I thought the use-case was pretty rare and here we were doing the _exact_ same thing :) – Madd0g Jun 23 '15 at 17:02
  • :) FWIW, this is what I'm making out if the data: http://jsnetworkx.org/api/#/v/v0.3.0 – Felix Kling Jun 23 '15 at 17:34

0 Answers0