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:
1{
2 "presets": ["stage-0", "es2015", "react"],
3 "plugins": ["transform-react-display-name", "transform-decorators-legacy"]
4}
I can now use an @foo
decorator to wrap a class and add
a bar()
method to it:
1// The @foo decorator is just a function
2function foo (x) {
3 return class Bar extends x {
4 name () {
5 return 'bar';
6 }
7 };
8}
9
10
11// Apply the decorator to class Foo...
12@foo
13class Foo {
14 name () {
15 return 'foo';
16 }
17}
18
19const foo = new Foo ();
20
21// Foo is in fact Bar
22expect (foo.name ()).to.equal ('bar');