google / google/closure-compiler
@export on methods should export inplace, not the full path
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
@export on a method exports the complete path, not just the particular method on a prototype:
``` js
// Note - no @export here
my.ns.ZeClass = function() {};
/** @export */
my.ns.ZeClass.prototype.foo = function() {};
```
Gives this output:
``` js
$my$ns$ZeClass$$ = function() {};
$my$ns$ZeClass$$.prototype.$foo$$ = function() {};
goog.exportPath('my.ns.ZeClass.prototype.foo', $my$ns$ZeClass$$.prototype.$foo$$);
```
The problem is that `ZeClass` gets renamed, so `foo` does not end up on its prototype - if external code gets a reference to a `ZeClass` object, it won't have a `foo` method on its prototype.
The workaround is to put `@export` on the class, too, but that increases code size and generally should not be necessary.
If you just want the particular method to be exported, a more reasonable export would be:
``` js
goog.exportProperty($my$ns$ZeClass$$.prototype, 'foo', $my$ns$ZeClass$$.prototype.$foo$$);
```
I.e. allowing the class to be renamed, and only exporting `foo` onto its prototype.
This would be a backwards-compat breaking change, so might require a new "in place exports" mode.
Contributor guide
Assessment
This issue has not been assessed yet.