I'm new to "ES7" decorators (by which I really mean decorators enabled by the Babel babel-plugin-transform-decorators-legacy
plug-in that was originally based on the ES7 decorator proposal), and I'm having a little trouble with how they work. I understand that a method decorator modifies the method, but I'm unclear what "side effects" can happen in the process. Specifically I'd like to know if a decorator can generate an export
, ie. can I make a decorator like this:
class Foo {
@export
bar() { doSomething(); }
}
which generates:
export const bar = Foo.prototype.bar;
// or
export const bar = new (Foo()).bar;
or alternatively:
class Foo {
@export
static bar() { doSomething(); }
}
to:
export const bar = Foo.bar;
In short, can a decorator ever create an export
statement?
No, export declarations cannot be created dynamically. At best, you could dynamically create a module file that contains them.