In November 2015, I was writing that decorators had been ripped out of Babel 6.
Now, they are back.
Install babel-plugin-transform-decorators-legacy
and update .babelrc
to include the legacy decorators transform:
{
"presets": ["stage-0", "es2015", "react"],
"plugins": ["transform-react-display-name", "transform-decorators-legacy"]
}
I can now use an @foo
decorator to wrap a class and add
a bar()
method to it:
// The @foo decorator is just a function
function foo (x) {
return class Bar extends x {
name () {
return 'bar';
}
};
}
// Apply the decorator to class Foo...
@foo
class Foo {
name () {
return 'foo';
}
}
const foo = new Foo ();
// Foo is in fact Bar
expect (foo.name ()).to.equal ('bar');