google / google/closure-compiler

@export on methods should export inplace, not the full path

Open
#461 1 comment 0 reactions 0 assignees View on GitHub
enhancement P3
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.