Support the new Babel 7 decorator implementation
Example config for babel 7, in decorators legacy mode:
//.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
Please note that plugin ordering is important, and plugin-proposal-decorators should be the first plugin in your plugin list
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-decorators": "^7.1.0",
"@babel/preset-env": "^7.1.0"
}
Non-legacy mode decorators (stage 2) is work in progress, see #1732
Edit: updated config to show the non-beta configuration for babel 7
Just wanted to share my requirements for setting up mobx with babel 7.0.0-beta.40. Mainly, I wanted to highlight the necessary inclusion of babel-plugin-transform-decorators-legacy:
// package.json
"devDependencies": {
"@babel/core": "^7.0.0-beta.40",
"@babel/preset-env": "^7.0.0-beta.40",
"@babel/preset-react": "^7.0.0-beta.40",
"@babel/preset-stage-1": "^7.0.0-beta.40",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
....
}
// using webpack.config.js instead of babel.rc
presets: [
'@babel/preset-react',
'@babel/preset-env',
'@babel/preset-stage-1',
],
plugins: [
'react-hot-loader/babel',
'transform-decorators-legacy',
[
'transform-class-properties', { loose: true, },
],
],
I really like decorators, any way to keep it?
My config. It work well
// package.json
"@babel/core": "^7.0.0-beta.42",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.42",
"@babel/plugin-proposal-decorators": "^7.0.0-beta.42",
"@babel/plugin-transform-runtime": "^7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-react": "^7.0.0-beta.42",
"@babel/preset-stage-2": "^7.0.0-beta.42",
// .babelrc
{
"presets": [
["@babel/preset-env", { "modules": false }],
"@babel/preset-react",
"@babel/preset-stage-2"
],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-decorators",
["@babel/plugin-proposal-class-properties", { "loose": true }]
],
"env": {
"development": {
"plugins": ["react-hot-loader/babel"]
}
}
}
@und3fined, thanks, it's works!
When using the new Babel 7.00-beta.46 it no longer works, as described here
UPDATE
Issue I was having below was caused by having "@babel/plugin-transform-flow-strip-types", as my first plugin. It needs to come after decorators and class-properties.
Builds work fine for me but I'm getting runtime errors like:
Error: [mobx] no observable property 'someObservableClassProperty' found on the observable object 'MyStoreObject'
at invariant (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:2704)
at fail$1 (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:2699)
at getAtom (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:1990)
at getAdministration (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:2010)
at observeObservableProperty (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:3651)
at observe (webpack-internal:///./node_modules/mobx/lib/mobx.module.js:3643)
at new MyStoreObject (webpack-internal:///./src/stores/MyStoreObject.js:198)
for things like
class MyStoreObject {
@observable someObservableClassProperty;
}
It appears that at the time that getAtom is called, thing.$mobx.values does not include property.

thing.__mobxDecorators contains only computed properties -- no observable properties.
My package.json has
"@babel/cli": "7.0.0-beta.46",
"@babel/core": "7.0.0-beta.46",
"@babel/plugin-proposal-class-properties": "7.0.0-beta.46",
"@babel/plugin-proposal-decorators": "7.0.0-beta.46",
"@babel/polyfill": "7.0.0-beta.46",
"@babel/preset-env": "7.0.0-beta.46",
"@babel/preset-react": "7.0.0-beta.46",
"@babel/preset-stage-3": "7.0.0-beta.46",
"babel-loader": "8.0.0-beta.2",
"mobx": "4.2.0",
.babelrc
{
"presets": [
"@babel/react",
"@babel/stage-3",
"@babel/preset-env",
],
"plugins": [
// NOTE: flow-strip-types MUST come after decorators and class-properties
"@babel/plugin-transform-flow-strip-types",
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
],
]
}
@Tsury please try.
update for [email protected]
My config work well.
// package.json
"@babel/core": "^7.0.0-beta.46",
"@babel/plugin-transform-runtime": "^7.0.0-beta.46",
"@babel/preset-env": "^7.0.0-beta.46",
"@babel/preset-react": "^7.0.0-beta.46",
"@babel/preset-stage-2": "^7.0.0-beta.46",
"@babel/runtime": "^7.0.0-beta.46",
Diff with 7.0.0-beta42
@babel/plugin-proposal-decoratorsinclude in stage-2 (https://github.com/babel/babel/tree/v7.0.0-beta.46/packages/babel-preset-stage-2)@babel/plugin-proposal-class-propertiesinclude in stage-3
// .babelrc
{
"presets": [
["@babel/preset-env", { "modules": false }],
"@babel/preset-react",
["@babel/preset-stage-2", { "loose": true, "decoratorsLegacy": true }]
],
"plugins": [
"@babel/plugin-transform-runtime"
],
"env": {
"development": {
"plugins": ["react-hot-loader/babel"]
}
}
}
New Babel PR: https://github.com/babel/babel/pull/7976
This stopped working for me when I was using beta-56 of the proposal-decorators. beta-49 works fine for me. Also, I was using beta-49 of core while using beta-56 of the proposal-decorators, so that could have done it too.
I am sharing the best solution for so far from my sight of view.
{
"presets": [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-flow"
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
"@babel/plugin-proposal-function-sent",
"@babel/plugin-proposal-export-namespace-from",
"@babel/plugin-proposal-numeric-separator",
"@babel/plugin-proposal-throw-expressions",
["@babel/plugin-proposal-class-properties", { "loose": true }],
[
"@babel/plugin-transform-runtime",
{
"helpers": true,
"regenerator": false
}
]
]
}
and package.json
"@babel/cli": "7.0.0-rc.1",
"@babel/core": "7.0.0-rc.1",
"@babel/plugin-proposal-class-properties": "^7.0.0-rc.1",
"@babel/plugin-proposal-decorators": "7.0.0-rc.1",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0-rc.1",
"@babel/plugin-proposal-function-sent": "^7.0.0-rc.1",
"@babel/plugin-proposal-numeric-separator": "^7.0.0-rc.1",
"@babel/plugin-proposal-throw-expressions": "^7.0.0-rc.1",
"@babel/plugin-transform-runtime": "7.0.0-rc.1",
"@babel/preset-env": "7.0.0-rc.1",
"@babel/preset-flow": "^7.0.0-rc.1",
"@babel/preset-react": "7.0.0-rc.1",
"@babel/runtime": "7.0.0-rc.1",
"babel-loader": "^8.0.0-beta.4",
Now that babel 7 stable released, its possible to work on new decorator API?
Yes. But sadly TS implementation is not being updated yet, as the spec has not officially progressed to the next stage, although the things under consideration are only cosmetical, not semantical afaik
Op do 30 aug. 2018 om 14:11 schreef Matheus Gambati < [email protected]>:
Now that babel 7 stable released, its possible to work on new decorator API?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx/issues/1352#issuecomment-417297096, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhBJZDsaiBu96kYe9XDVY716IFM5Nks5uV9aMgaJpZM4SIcuz .
The order is important.
"plugins": [
...
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
],
...
]
Follow the order.
Updated the original comment to show the latest status, the non-beta Babel 7 version
Posting this error message here because when it took me all day to find the solution to this based on the error. The error I was getting was:
Uncaught TypeError: An element descriptor's .kind property must be either "method" or "field", but a decorator created an element descriptor with .kind "undefined"
at _toElementDescriptor (app.js:46281)
at _toElementFinisherExtras (app.js:46283)
at _decorateElement (app.js:46273)
at app.js:46269
at Array.forEach (<anonymous>)
at _decorateClass (app.js:46269)
at _decorate (app.js:46251)
at Module../src/App/stores/UserStore.js (app.js:46301)
at __webpack_require__ (bootstrap:19)
at Module../src/App/stores/index.js (index.js:1)
I was getting the error when using @computed but @actions worked fine.
The solution posted by OP works fine with Babel 7.
Babel 7 has caused me nothing but headaches so far.
I had the same issue with "@babel/core": "7.1.0" and I was able to solve the problem in two steps:
-
npm install --save-dev @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators"
-
Then add the following to babel config section on package.json as following:
"babel": {
"plugins": [
["@babel/plugin-proposal-decorators", {"legacy": true}],
["@babel/plugin-proposal-class-properties", {"loose": true}]
],
"presets": [
"react-app"
]
}
Also, make sure to put decorators plugin before the class-properties plugin in your babel config. Apparently, the ordering of plugins matter in babel. Who knew?
Agree with @JamesTheHacker, Babel 7 brought me nothing but headaches.
Update:
This isn't MobX specific issue, it's related to general Babel compilation error.
Original comment
Hi! Another interesting bug here ;) Despite of suggested plugins ordering solution decorators will conflict with static property which is called another static property of the same class e.g.
export class TestClass {
static person = {
name: 'Bazz',
familyName: 'Lighter'
};
static secondPerson = {
name: TestClass.person.name,
familyName: 'Fuzz'
};
@computed
get someGetter() {
return true;
}
}
In runtime this will throw
name: TestClass.person.name,
^
TypeError: Cannot read property 'person' of undefined
I made little repo to demonstrate it https://github.com/andriimiroshnychenko/small-repro. @mweststrate can you please take a look?
@mweststrate decorators is pretty in code, but not work well with mobx
@und3fined, are you trolling? Lots of us are using decorators with MobX in production.